Package me.hackware.api.event
Class EventBus
java.lang.Object
me.hackware.api.event.EventBus
Annotation-driven event bus using
SubscribeEvent and bound
MethodHandle-based fast invocation (no reflective
Method.invoke() at dispatch time).
Defining a handler
@SubscribeEvent
private void onAttack(AttackEvent event) { ... }
@SubscribeEvent(priority = EventPriority.HIGH)
private void onPacket(PacketEvent event) { ... }
Handlers declared on superclasses are picked up too; if a subclass re-declares
an annotated handler with the same name and event type, the subclass wins.
Registering / un-registering
EventBus.get().register(this); // scans for @SubscribeEvent methods
EventBus.get().unregister(this); // removes all handlers owned by this
register() is idempotent: calling it on an already-registered object is a
no-op rather than a second copy of every handler, so "make sure I'm listening" is a
safe idiom (see ChestSwap / AutoDisconnect, which stay on the bus
while disabled so their keybinds keep working).
Posting
PacketEvent event = new PacketEvent(packet, Direction.SEND);
EventBus.get().post(event);
if (event.isCancelled()) return;
Threading
register(java.lang.Object)/unregister(java.lang.Object) are safe to call from any thread.
post(T) is lock-free and may run concurrently with them; a listener
registered concurrently with a post may or may not observe that post.
Listeners run on whichever thread posted the event — events posted from the
network thread run their handlers on the network thread.-
Method Summary
Modifier and TypeMethodDescriptionstatic EventBusget()<T extends Event>
Tpost(T event) Dispatch an event to all registered listeners (highest priority first).voidScanslistenerObjectand its superclasses for methods annotated withSubscribeEventand registers them.voidunregister(Object listenerObject) Removes all listeners owned bylistenerObjectacross every event type.
-
Method Details
-
get
-
register
ScanslistenerObjectand its superclasses for methods annotated withSubscribeEventand registers them. Each method must accept exactly one parameter whose type extendsEvent.Does nothing if the object is already registered.
-
unregister
Removes all listeners owned bylistenerObjectacross every event type. Typically called when a module is disabled. -
post
Dispatch an event to all registered listeners (highest priority first). If any listener cancels the event, dispatch stops immediately.Uses a cached, flattened array of listeners (including hierarchy) for maximum dispatch speed — no iterator allocation, no map lookups per superclass. A listener that throws is logged and skipped; the remaining listeners still run.
- Returns:
- the same event instance (for fluent checks like
event.isCancelled())
-