-
Notifications
You must be signed in to change notification settings - Fork 6
Registration
LambdaLib2 encourages a new way of registering in-mod stuffs by the idea of dispatching.
As you know, in forge post MC 1.8 there are two ways to register anything:
- through
@EventHandler
annotated methods in Main Mod (the old way). - through listening to
RegistryEvent.Register<...>
(which is the only way in new versions to register blocks and items)
There is one thing inherently wrong with this approach, because this make your main mod depend on everything you register. For 1 it's obvious since the method can only be put inside main mod class; For 2, you have to register your event handler, which you can only be initiated from your mod. (Same argument goes for proxy, which is essentially the same: it depends on EVERYTHING it needs to register).
LambdaLib2's registration
package offer three annotations to let you organize your registration in a clean, modular way. Basically, you annotate a static method to mark it for registration, and it will automatically be called at later registration stage. No nasty dependency is needed.
To dispatch PreInitialization
, Initialization
etc. we must execute methods on a per-mod basis. Thus for each method we have to know which mod it belongs to.
We use a package-based approach for determining methods' belongings. Annotate your mod's main class with @RegistryMod
class will mark everything in the mod package or all subpackages as belong to this mod. (You can specify your own package using rootPackage
attribute.
Example:
package testmod;
@RegistryMod
@Mod(...)
public class TestMod {
...
}
Annotate a static method with this annotation will make it called when it's beloning mod's corresponding event is triggered. The event is specified by the first argument of the method (which must extend FMLStateEvent).
Note that it can do things much more than initialization, since events like FMLServerStoppingEvent, FMLServerStartingEvent
are all subclasses of FMLStateEvent
.
Example:
package testmod.init;
public class InitMethodTest {
@StateEventCallback
private static void init(FMLInitializationEvent event) {
...
}
}
Annotate a static method with this annotation will make it called when forge triggers corresponding RegistryEvent.Register<T>
events.
package testmod.init;
public class LoadItemTest {
@RegistryCallback
private static void registerItems(RegistryEvent.Register<Item> ev) {
...
}
}