An extremely fast, flexible, lightweight, and simple event system aimed towards Minecraft utility mods.
If you have not already, add Jitpack as a repository:
repositories {
maven { url 'https://jitpack.io' }
}
Add the release of your choice in the dependencies block:
dependencies {
implementation 'com.github.therealbush:eventbus:1.0.2'
}
When creating a new EventBus, there are 3 different arguments it can accept:
- A handler type.
- A consumer for logging errors.
- A consumer for logging debug info.
- Alternatively, one consumer for both info and errors.
The default handler type is LambdaHandler
The default consumer logs to console, with the prefix [EVENTBUS]:
The following are all valid:
new EventBus();
new EventBus(ASMHandler.class);
new EventBus(MyUtilityMod.logger::error);
new EventBus(System.out::println, info -> mc.player.sendChatMessage("I love bush's event bus! " + info));
new EventBus(ReflectHandler.class, MyUtilityMod.logger::error);
new EventBus(ASMHandler.class, MyUtilityMod.logger::error, MyUtilityMod.logger::info);
Extend Event
, and implement the method isCancellable()
.
Example:
import me.bush.eventbus.event.Event;
public class MyEvent extends Event {
@Override
protected boolean isCancellable() {
return true;
}
}
Create a public void method with one parameter, which is a subclass of Event
.
Annotate the method with @EventListener
.
There are two modifiers you can add to the annotation:
priority
: Listeners with high priority will recieve events before listeners with low priority.recieveCancelled
: Listeners with recieveCancelled enabled will recieve events even after they are cancelled.
Example:
@EventListener
public void onEvent(MyEvent event) {}
Calling EventBus#subscribe
and EventBus#unsubscribe
will add and remove listeners from the EventBus.
Only listeners in subscribed objects and classes will recieve events.
- For non static listeners to recieve events, you must subscribe an object.
- For static listeners to recieve events, you must subscribe a class.
Static listeners will not recieve events if only an object is subscribed, and vice versa.
Calling EventBus#post
will post an event to every listener with an exactly matching event type.
For example, if event B extends event A, and event A is posted, B listeners will not recieve it.
This method will return true if the posted event was cancelled, and false otherwise.
I tried to make it fail, and it wouldn't.
Simple implementation, and simple listener syntax.
Nearly twice as fast as the default Forge EventBus.
3 Different Handler Types
You can also make your own by extending Handler
Kinda pointless, but it was fun to make (just use lambdahandler lol)
The same invocation style Forge uses. This is pretty fast, but a little hacky.
Uses LambdaMetaFactory to create a "function object", which is nearly as fast as direct access.
The most basic style, but also the most reliable.