Interface EventInvoker<T>

Type Parameters:
T - the listener type
All Known Implementing Classes:
EventInvokerImpl

@NonExtendable public interface EventInvoker<T>
One cross-loader event, identified by its listener type: a functional interface.

Listeners are kept here, in common code, and run in EventPhase order; each loader only fires the event through invoker(). Events are found by their listener type, like


 EventInvoker.lookup(ServerTickEvents.End.class).register(server -> ...);
 
or through the constant of the class that declares them (ServerTickEvents.END); both are the same event.

Mods declare their own events with create(Class):


 public static final EventInvoker<Reloaded> RELOADED = EventInvoker.create(Reloaded.class);
 RELOADED.invoker().onReloaded(server);
 
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> EventInvoker<T>
    create(Class<T> type)
    A new event, with listeners combined by the listener method's return type: void: every listener runs; EventResult: listeners run until one interrupts, whose result is returned (else EventResult.PASS); EventResultHolder: the same, with its value; boolean: listeners run until one returns false. Use create(Class, Function) for anything else, or for a hot event that shouldn't go through reflection.
    static <T> EventInvoker<T>
    create(Class<T> type, Function<List<T>,T> combiner)
    A new event whose invoker is built by combiner from the current listeners, in phase order.
    boolean
     
    boolean
     
    Fires the event: calls every listener as the event combines them.
    Fires the event for the listeners of one band only, in phase order: what a loader hook calls from each of its places (before, among and after other mods' listeners).
    static <T> EventInvoker<T>
    lookup(Class<T> type)
    The event with this listener type.
    void
    register(EventPhase phase, T listener)
    Adds a listener in the given phase.
    default void
    register(T listener)
    Adds a listener in the EventPhase.DEFAULT phase.
    The listener type identifying this event.
    boolean
    unregister(T listener)
    Removes a listener, from whichever phase it was added to.
  • Method Details

    • lookup

      static <T> EventInvoker<T> lookup(Class<T> type)
      The event with this listener type. The class declaring the listener type (for nested types) is initialised first, so its events exist.
      Throws:
      IllegalArgumentException - when no event has this listener type
    • create

      static <T> EventInvoker<T> create(Class<T> type)
      A new event, with listeners combined by the listener method's return type:
      • void: every listener runs;
      • EventResult: listeners run until one interrupts, whose result is returned (else EventResult.PASS);
      • EventResultHolder: the same, with its value;
      • boolean: listeners run until one returns false.
      Use create(Class, Function) for anything else, or for a hot event that shouldn't go through reflection.
      Throws:
      IllegalArgumentException - when the type isn't a functional interface, or already has an event
    • create

      static <T> EventInvoker<T> create(Class<T> type, Function<List<T>,T> combiner)
      A new event whose invoker is built by combiner from the current listeners, in phase order. The combiner runs again whenever the listeners change, so the invoker it returns can simply loop over the list.
      Throws:
      IllegalArgumentException - when the type already has an event
    • type

      Class<T> type()
      The listener type identifying this event.
    • register

      default void register(T listener)
      Adds a listener in the EventPhase.DEFAULT phase.
    • register

      void register(EventPhase phase, T listener)
      Adds a listener in the given phase. For events whose loader hook fires each EventPhase.Band at its own place, EventPhase.FIRST also runs before other mods' own listeners of the game event and EventPhase.LAST after them.
    • unregister

      boolean unregister(T listener)
      Removes a listener, from whichever phase it was added to.
      Returns:
      whether it was registered
    • hasListeners

      boolean hasListeners()
      Returns:
      whether the event has any listeners, so a loader can skip building its arguments
    • hasListeners

      boolean hasListeners(EventPhase.Band band)
      Returns:
      whether the event has listeners in phases of this band
    • invoker

      T invoker()
      Fires the event: calls every listener as the event combines them.
    • invoker

      T invoker(EventPhase.Band band)
      Fires the event for the listeners of one band only, in phase order: what a loader hook calls from each of its places (before, among and after other mods' listeners). The three bands together are invoker().