Event Basics

Learn more about Events, how they work and their purpose

If you are familiar with Java libraries like javax.swing, or have done Forge/Fabric coding before, then don't worry - events shouldn't be a mystery to you!

Forge and Fabric already use events. However, they are not very multiplatform - they change massively version to version. That's why OneConfig has its own event system, helping OneConfig to achieve its aim of uniting all Minecraft mods.

Getting on the Bus

To get events to work, its important to register any Object/Class that uses them to the EventBus. This is very simple, and its a great idea to do this in your constructor, for example:

public MyClassConstructor() {
    EventManager.INSTANCE.register(this);
}

This will allow that class to use the Event system. Continue reading to subscribe to your first event!

Using the Bus

Once you have registered your class, you can now @Subscribe to (use) the events you would like. For example, to subscribe to a TickEvent:

@Subscribe
private void onTick(TickEvent event) { // the parameter type specifies what event you are subscribing to
    // do something every tick (once every 50ms in Minecraft)
}

All methods annotated with @Subscribe must not be static!

So, as a general rule for events:

Easy as that!

Last updated