Update 2.6: Events authored by LaDestitute's avatar LaDestitute
......@@ -8,14 +8,14 @@ The main event bus that Forge uses is called ``EVENT_BUS`` with the note that pr
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.
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.
The above image points to what registry-method you should use on 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
......@@ -27,6 +27,7 @@ public class EntityEventHandler {
@SubscribeEvent
public void onpigjump(LivingEvent.LivingJumpEvent event)
{
//An if-statement within the method using instanceof
if(event.getEntityLiving() instanceof PigEntity)
{
event.getEntityLiving().addEffect(new EffectInstance(Effects.LEVITATION, 100, 5));
......@@ -35,7 +36,7 @@ public class EntityEventHandler {
}
```
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.
What this event handler does is listen for when a living entity jumps via the entity-jump hook and if the condition is met within the if-statement in the method (is it a pig?), give them levitation to send them flying into the air.
Lastly, we must register our event handler!
......
......