The Main Mod Class and the Event Bus
Every Forge mod has one main class, annotated with @Mod, that Forge instantiates automatically when the game loads.
package com.example.examplemod;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
@Mod("examplemod")
public class ExampleMod {
public ExampleMod() {
FMLJavaModLoadingContext.get().getModEventBus()
.addListener(this::commonSetup);
}
private void commonSetup(final FMLCommonSetupEvent event) {
// runs once, early, on both client and dedicated server
}
}
The string passed to @Mod must match modId in mods.toml, that's how Forge connects the class to the mod's metadata.
The mod event bus
Forge fires setup and registration events through a mod event bus, retrieved with FMLJavaModLoadingContext.get().getModEventBus(). Registering a listener in the constructor, as above, is the standard place to hook into it, you'll register items and blocks here in the next two lessons.
FMLCommonSetupEvent fires once, after all registries have finished registering, the right place for setup logic that depends on other mods' content already existing (like inter-mod compatibility), but too early for anything requiring a running world.
WARNING
There's a second, separate bus, the game/Forge event bus (MinecraftForge.EVENT_BUS), used for gameplay events like a player breaking a block. Setup and registration events use the mod bus, gameplay events use the Forge bus, mixing them up is one of the most common beginner mistakes.