Skip to content

Entrypoints

mineLdiver edited this page Jul 25, 2023 · 1 revision

StationAPI provides 3 entrypoints:

  • stationapi:event_bus
  • stationapi:event_bus_client
  • stationapi:event_bus_server

The last two are just sided versions of the first one, meaning they'll only get discovered and instantiated on their respected side.

Event listeners

Classes under the stationapi:event_bus entrypoint will be scanned for @EventListener annotated methods to register them to StationAPI's main event bus.

fabric.mod.json

{
  "entrypoints": {
    "stationapi:event_bus": [
      "me.mymod.MyMod"
    ]
  }
}

MyMod.java

public class MyMod {
    @EventListener
    void registerBlocks(BlockRegistryEvent event) {
        // block registration code...
    }

    @EventListener
    void registerItems(ItemRegistryEvent event) {
        // item registration code...
    }
}

Event listener methods are only required to return void and the event type they're listening to be their only argument, so these are also valid listeners:

public class MyMod {
    @EventListener
    public final void onBlocksInit(BlockRegistryEvent blocksEvent) {
        // block registration code...
    }

    @EventListener
    private static void items(ItemRegistryEvent itemsInit) {
        // item registration code...
    }
}

Event bus policy

There's also a way to provide a custom event bus policy by annotating your entrypoint class with @Entrypoint and setting the eventBus field to a custom policy.

@Entrypoint(eventBus = @EventBusPolicy(registerInstance = false))
public class MyMod {
    // will be skipped, as it's an instance method
    @EventListener
    public final void onBlocksInit(BlockRegistryEvent blocksEvent) {
        // block registration code...
    }

    // will be registered, as it's a static method
    @EventListener
    private static void items(ItemRegistryEvent itemsInit) {
        // item registration code...
    }
}

This can be helpful if you want to more explicitly define your entrypoint structure or if you want to tell StationAPI to not attempt scanning for static or instance methods to give a slight registration speed up.

Utility fields

Classes under stationapi:event_bus entrypoint can declare some utility fields which will be set by StationAPI on setup.

public class MyMod {
    @Entrypoint.Instance
    public static final MyMod INSTANCE = Null.get();

    @Entrypoint.ModID
    public static final ModID MODID = Null.get();

    @Entrypoint.Logger
    public static final Logger LOGGER = Null.get();

    @EventListener
    void onInit(InitEvent event) {
        LOGGER.info("Initializing mod " + MODID + " with current instance " + INSTANCE);
    }
}

@Entrypoint.Instance annotated fields will be set to the entrypoint class's instance made by Fabric Loader on entrypoint discovery.

@Entrypoint.ModID will be set to the mod's modid defined in fabric.mod.json, allowing to avoid writing the actual modid string in code.

@Entrypoint.Logger will be set to a org.apache.logging.log4j.Logger instance with modid|Mod name. The name can be configured in the annotation itself, for example @Entrypoint.Logger("Something Else"), |Mod won't be appended.

Null.get() is merely used to evade IntelliJ's data flow control. If you set it to null, it'll constantly complain that the field will always produce a NullPointerException on access. As another workaround, you can make the field non-final and not set it to anything.