-
Notifications
You must be signed in to change notification settings - Fork 0
Events
Events are fired for many things that happen in Minecraft or in the mod. For example: the RegistryEvent is fired when you can safely register stuff on both NeoForge and Fabric.
For a collection of available events, take a look at the events package or in the client events package.
To register listeners for events you use the EventManager class, it has 3 ways of registering listeners:
- Using annotated class methods
- Using class method
- Using a Consumer
When you have a ModEvents class or something similar, you might want register all methods as a event listener. For that you need to annotate every wanted listener with @EventListener and after that you still need to register them by calling EventManager#registerListeners:
/* With static */
public class YourEvents {
@EventListener
public static void onSomeEvent(SomeEvent event) {
// Do some stuff
}
}
// In another class
EventManager.registerListeners(YourEvents.class);/* Without static */
public class YourEvents {
@EventListener
public void onSomeEvent(SomeEvent event) {
// Do some stuff
}
}
// In another class
EventManager.registerListeners(new YourEvents());If you want to register an event listener but don't want to annotate them, you can use EventManager#registerListener:
public class YourEvents {
public static void onSomeEvent(SomeEvent event) {
// Do some stuff
}
}
// In another class
EventManager.registerListener(YourEvents::onSomeEvent);When just want to listen to an event without using a method, you call still use Consumers by just calling EventManager#registerListener:
EventManager.registerListener(SomeEvent.class, (event) -> {
// Do something
});If an event implements the ICancelableEvent, you are able to cancel it using ICancelableEvent#setCanceled or ICancelableEvent#cancel
and if you want to know if an event is canceled you can just call ICancelableEvent#isCanceled.
Events are just classes (as you might have guest), so to make a custom event just extend the Event class:
public class YourCustomEvent extends Event {
// Some stuff here
// Maybe an constructor...?
}...and if you want your event to be cancelable, you need to implement the ICancelableEvent interface:
public class YourCustomEvent extends Event implements ICancelableEvent {
// Some stuff here
// Maybe a constructor...?
}After creating an event, you can fire it by using EventManager#invoke:
EventManager.invoke(new SomeEvent());
// Or
var someEvent = new SomeEvent();
EventManager.invoke(someEvent);- Installation (Development)
- Events
$\textsf{\color{#ff0000}{Registries}}$ $\textsf{\color{#ff0000}{Networking}}$ $\textsf{\color{#ff0000}{Configs}}$