Introduction to events
Events are a nifty feature of the Forge API that allows mod makers to run lines of code when a specific event takes place. Forge events can be used for many things and there are many types of events. A Forge event requires you to have two class files. the first being an @Mod or core mod file, you should already have one of these. Forge events also require a separate class file for you place the code for the event. Below is an example of an extremely basic event. If you wish to do the same event as this tutorial, create a new class in any package you wish called ItemPickupHandler.
To start things off, we will be adding a new method within this class.
@ForgeSubscribe
public void onItemPickup(EntityItemPickupEvent event)
{
// Do your code here.
}
You should notice something over the method header, this is called an Annotation. These essentially allow a method to have extra data assigned to the method, however in this case this annotation is telling Forge that it has to subscribe this method to the Event Bus, which we will be getting to later. You should also notice this method takes an argument, which is required for events. It also allows you to access certain things the event stores, in this case the item that the player picks up.
So we have a class that has the event method within it, however if you try and run the game you will quickly realise that the code is never ran, we have the ForgeSubscribe annotation over the method but we have left out the Event Bus registration, we will now add that! Place this code into any of your init methods in your core mod class:
MinecraftForge.EVENT_BUS.register(new ItemPickupHandler());
This code registers the event class with the Forge Event Bus, which is a list of all the event handlers that have been registered, which then Forge runs through and invokes (calls) the methods that are assigned to that specific event.
To start things off, we will be adding a new method within this class.
@ForgeSubscribe
public void onItemPickup(EntityItemPickupEvent event)
{
// Do your code here.
}
You should notice something over the method header, this is called an Annotation. These essentially allow a method to have extra data assigned to the method, however in this case this annotation is telling Forge that it has to subscribe this method to the Event Bus, which we will be getting to later. You should also notice this method takes an argument, which is required for events. It also allows you to access certain things the event stores, in this case the item that the player picks up.
So we have a class that has the event method within it, however if you try and run the game you will quickly realise that the code is never ran, we have the ForgeSubscribe annotation over the method but we have left out the Event Bus registration, we will now add that! Place this code into any of your init methods in your core mod class:
MinecraftForge.EVENT_BUS.register(new ItemPickupHandler());
This code registers the event class with the Forge Event Bus, which is a list of all the event handlers that have been registered, which then Forge runs through and invokes (calls) the methods that are assigned to that specific event.