Minecraft 1.14 modding with forge – 3 – How to use events

Hello everyone!
In this part of the tutorial, I’ll show what events are and how to use them.

How to define an event handler:

Forge adds a lot of events which you can use to change the way Minecraft behaves, these events range from changing the item that is crafted to registering new items.
Listening to these events is quite simple, you just need to have a method that only has 1 argument, and that argument should be the event you want, eg:

 
//Notice the RegistryEvent.Register<Item> argument, this will tell forge this is an event handler for the Register<Item> event
public static void registerItems(RegistryEvent.Register<Item> event) {}  

You also need to tell forge that there are event handlers that should be called on an event, and there are multiple ways to do this.
Depending on which way you do it your method needs to be static or not.
You can set the priority of your handler if you need to handle an event as early as possible (eg. before most mods have touched it) or as late as possible (eg. after most mods have touched it).

Different busses:

There are 2 busses that events are fired on. The forge bus and the mod bus, each bus only receives the events fired on that bus.

(This list is from forge version 26.0.50, it may have changed in later versions)

  • The mod bus has the following events:
    • RegistryEvents
    • ColorHandlerEvent
    • ModConfigEvent
    • FMLCommonSetupEvent
    • TextureStitchEvent$Pre
    • FMLClientSetupEvent
    • ModelRegistryEvent
    • InterModEnqueueEvent
    • InterModProcessEvent
    • TextureStitchEvent$Post
    • FMLLoadCompleteEvent
    • ModelBakeEvent
  • All other events are on the forge bus

How to tell forge your event handler exists:

As said earlier there are different ways to subscribe your event handlers, and depending on how you do it you may have to make your event handler a static method.
If you need to use the @SubscribeEvent annotation you can set the priority of the event by setting the priority value, and you can also set receiveCanceled to true if you need to receive events that were cancelled.

  • You can add the @Mod.EventBusSubscriber annotation above the class with your event handlers in it.
    • This requires the method to be STATIC.
    • The event handler should have the @SubscribeEvent annotation
    • You need to set the modid value to your modid
    • By setting the bus value you can choose which bus to receive events for. (Forge or Mod, default forge)
    • You can also set it to only run on the dedicated server or client by setting the “value” value.
  • The following methods can be called on MinecraftForge.EVENT_BUS for the forge bus, or FMLJavaModLoadingContext.get().getModEventBus() for the mod bus
  • Call register and pass in your class (eg MinecraftForge.EVENT_BUS.register(EventHandler.class))
    • The event handler should have the @SubscribeEvent annotation
    • This requires the event handler to be STATIC
  • Call register and pass in an instance of your class (eg MinecraftForge.EVENT_BUS.register(new EventHandler()))
    • The event handler should have the @SubscribeEvent annotation
    • The requires the method to NOT be STATIC
  • Call addListener with a reference to the event handler (eg MinecraftForge.EVENT_BUS.addListener(EventHandler::chatEvent))
    • You can also pass in the event priority as an argument and whether to receive cancelled events
  • Call addGenericListener with a reference to the event handler
    • Same as addListener except you can pass in a class to use as a “filter”.

Depending on the event you can also cancel it or set a result, which will influence what will happen in the game. Not all events have this and some events are just for you to react to.
To get a list of all events, find the Event class in net.minecraftforge.eventbus.api and let your IDE display all classes that inherit this class. (ctrl+H while in the class in IntelliJ)

Posted in Forge tutorial, Forge Tutorial 1.14.

Leave a Reply

Your email address will not be published. Required fields are marked *