Class EventBus

java.lang.Object
me.hackware.api.event.EventBus

public final class EventBus extends Object
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 Details

    • get

      public static EventBus get()
    • register

      public void register(Object listenerObject)
      Scans listenerObject and its superclasses for methods annotated with SubscribeEvent and registers them. Each method must accept exactly one parameter whose type extends Event.

      Does nothing if the object is already registered.

    • unregister

      public void unregister(Object listenerObject)
      Removes all listeners owned by listenerObject across every event type. Typically called when a module is disabled.
    • post

      public <T extends Event> T post(T event)
      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())