Create 2.6: Events authored by LaDestitute's avatar LaDestitute
# 2.6.1: Intro
Events are a type of feature in Forge that uses event buses to intercept and modify or even cancel (as in stop) vanilla behavior. An event bus is a method in java for components to be able to communicate with each other without having to register with each other.
### 2.6.2: Events In Depth
The main event bus that Forge uses is called ``EVENT_BUS`` with the note that prior world-gen exclusive event buses have been regulated into said generic Forge event bus. The reason this happened cause world-gen had changed a lot from +1.13 onward and that had invalidated the other two existing event buses for ore and world-gen.
A method that is marked with ``@SubscribeEvent`` is an event handler. An event handler's public void member methods can also be static but you must pass the class through registration if it is static like in the image down below: ``MinecraftForge.EVENT_BUS.register(MyStaticForgeEventHandler.class)``.
Event classes may also be annotated with ``@Mod.EventBusSubscriber`` above their signature instead of registering them normally and this automatically registers the event handler to the Forge event bus when the @Mod class is constructed.
Lastly, if an event is cancelable, it will be marked with the ``@Cancelable`` annotation. It is very important to take note here that not all events are cancelable, attempting to do so if they do not have said annotation above them in their actual class causes an unsupported operation exception to be thrown, which will crash Minecraft.
Also, you may want to look at this image in case of troubleshooting your own events:
![alt text](https://media.discordapp.net/attachments/665281306426474506/665605979798372392/eventhandler.png?width=824&height=464)
The above image points to what registry-method you should use how your methods in your class are styled, with the methods on the right and the registry-method on the left.
### 2.6.3: Event Handler
Below is an example of an event handler class:
```java
public class EntityEventHandler {
@SubscribeEvent
public void onpigjump(LivingEvent.LivingJumpEvent event)
{
if(event.getEntityLiving() instanceof PigEntity)
{
event.getEntityLiving().addEffect(new EffectInstance(Effects.LEVITATION, 100, 5));
}
}
}
```
What this event handler does is listen for when a living entity jumps and if it's a pig, give them levitation to send them flying into the air.
Lastly, we must register our event handler!
```java
public Forge16TutsMain() {
instance = this;
final IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();
ModLoadingContext context = ModLoadingContext.get();
//Our listener for setup, it will pick up on anything put into setup and notify Forge
modEventBus.addListener(this::setup);
//Remember to register items before blocks, problems can occur otherwise if you don't
ItemInit.ITEMS.register(modEventBus);
BlockInit.BLOCKS.register(modEventBus);
MinecraftForge.EVENT_BUS.register(new EntityEventHandler());
}
```
##### 2.6.4: Available Events
Events may change in name from Forge version to Forge version and some may even be removed in future versions due to changes or new implementations. I will explain below how to find what events are available below depending on your IDE.
**IntelliJ:**
Right click any event in Forge such as ``LivingEvent.LivingJumpEvent`` and click *Go To* then *Declaration or Usages*.
You can then click around in the path above the event class to see other events around in related packages or in the same package.
**Eclipse:**
In Eclipse, right-click any event in an event handler such as ``LivingEvent.LivingJumpEvent`` and click ``Quick Type Hierarchy`` then click the same event class name to be brought to said class.
Lastly, click the event class's name in the path above the class to be given a dropdown of events within that package or click the package to the left to see other event packages for more events.
\ No newline at end of file