Class EventBus

java.lang.Object
net.neoforged.bus.EventBus
All Implemented Interfaces:
IEventBus, IEventExceptionHandler

@Internal public class EventBus extends Object implements IEventExceptionHandler, IEventBus
  • Constructor Details

  • Method Details

    • register

      public void register(Object target)
      Description copied from interface: IEventBus
      Register an instance object or a Class, and add listeners for all SubscribeEvent annotated methods found there, or directly a Method annotated with SubscribeEvent. Depending on what is passed as an argument, different listener creation behaviour is performed.
      Object Instance
      Scanned for non-static methods annotated with SubscribeEvent and creates listeners for each method found.
      Class Instance
      Scanned for static methods annotated with SubscribeEvent and creates listeners for each method found.
      Method Instance
      Expects a static method annotated with SubscribeEvent which will be registered as a listener for the Event type it has as its sole parameter.
      Specified by:
      register in interface IEventBus
      Parameters:
      target - Either a Class instance or an arbitrary object, for scanning and event listener creation or a Method
    • addListener

      public <T extends Event> void addListener(Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener with default EventPriority.NORMAL and not recieving canceled events.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(EventPriority priority, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener with the specified EventPriority and not receiving canceled events.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      priority - EventPriority for this listener
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(EventPriority priority, boolean receiveCanceled, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener with the specified EventPriority and potentially canceled events.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      priority - EventPriority for this listener
      receiveCanceled - Indicate if this listener should receive events that have been ICancellableEvent canceled
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(EventPriority priority, Class<T> eventType, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener with the specified EventPriority and not receiving canceled events. Use this method when one of the other methods fails to determine the concrete Event subclass that is intended to be subscribed to.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      priority - EventPriority for this listener
      eventType - The concrete Event subclass to subscribe to
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(boolean receiveCanceled, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener receiving potentially canceled events.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      receiveCanceled - Indicate if this listener should receive events that have been ICancellableEvent canceled
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(boolean receiveCanceled, Class<T> eventType, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener receiving potentially canceled events. Use this method when one of the other methods fails to determine the concrete Event subclass that is intended to be subscribed to.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      receiveCanceled - Indicate if this listener should receive events that have been ICancellableEvent canceled
      eventType - The concrete Event subclass to subscribe to
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(Class<T> eventType, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener not receiving canceled events. Use this method when one of the other methods fails to determine the concrete Event subclass that is intended to be subscribed to.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      eventType - The concrete Event subclass to subscribe to
      consumer - Callback to invoke when a matching event is received
    • addListener

      public <T extends Event> void addListener(EventPriority priority, boolean receiveCanceled, Class<T> eventType, Consumer<T> consumer)
      Description copied from interface: IEventBus
      Add a consumer listener with the specified EventPriority and potentially canceled events. Use this method when one of the other methods fails to determine the concrete Event subclass that is intended to be subscribed to.
      Specified by:
      addListener in interface IEventBus
      Type Parameters:
      T - The Event subclass to listen for
      Parameters:
      priority - EventPriority for this listener
      receiveCanceled - Indicate if this listener should receive events that have been ICancellableEvent canceled
      eventType - The concrete Event subclass to subscribe to
      consumer - Callback to invoke when a matching event is received
    • unregister

      public void unregister(Object object)
      Description copied from interface: IEventBus
      Unregister the supplied listener from this EventBus. Removes all listeners from events. NOTE: Consumers can be stored in a variable if unregistration is required for the Consumer.
      Specified by:
      unregister in interface IEventBus
      Parameters:
      object - The object, Class, Method or Consumer to unsubscribe.
    • post

      public <T extends Event> T post(T event)
      Description copied from interface: IEventBus
      Submit the event for dispatch to appropriate listeners

      If this bus was not started yet, the event is returned without being dispatched.

      Specified by:
      post in interface IEventBus
      Parameters:
      event - The event to dispatch to listeners
      Returns:
      the event that was passed in
    • post

      public <T extends Event> T post(EventPriority phase, T event)
      Description copied from interface: IEventBus
      Submit the event for dispatch to listeners registered with a specific EventPriority.

      If this bus was not started yet, the event is returned without being dispatched.

      Manually posting events phase-by-phase through this method is less performant than dispatching to all phases through a IEventBus.post(Event) call. Prefer that method when per-phase dispatching is not needed.

      Specified by:
      post in interface IEventBus
      Parameters:
      phase - The priority phase to post this event for
      event - The event to dispatch to listeners
      Returns:
      the event that was passed in
      See Also:
    • handleException

      public void handleException(IEventBus bus, Event event, EventListener[] listeners, int index, Throwable throwable)
      Description copied from interface: IEventExceptionHandler
      Fired when a EventListener throws an exception for the specified event on the event bus. After this function returns, the original Throwable will be propagated upwards.
      Specified by:
      handleException in interface IEventExceptionHandler
      Parameters:
      bus - The bus the event is being fired on
      event - The event that is being fired
      listeners - All listeners that are listening for this event, in order
      index - Index for the current listener being fired.
      throwable - The throwable being thrown
    • start

      public void start()
      Description copied from interface: IEventBus
      Start this bus (if it was created shut down), making it able to post events to listeners.
      Specified by:
      start in interface IEventBus