Skip to content
This repository was archived by the owner on Feb 17, 2025. It is now read-only.
Command17 edited this page Feb 10, 2025 · 8 revisions

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.

Registering Listeners

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

Using Annotated Class Methods

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());

Using Class Methods

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);

Using A Consumer

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
});

Canceling Events

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.

Creating Custom Events

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...?
}

Firing Events

After creating an event, you can fire it by using EventManager#invoke:

EventManager.invoke(new SomeEvent());
// Or
var someEvent = new SomeEvent();
EventManager.invoke(someEvent);

Non Development:

Development:

Clone this wiki locally