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 TypeMethodDescriptionstatic <T> EventInvoker<T> 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 (elseEventResult.PASS);EventResultHolder: the same, with its value;boolean: listeners run until one returnsfalse. Usecreate(Class, Function)for anything else, or for a hot event that shouldn't go through reflection.static <T> EventInvoker<T> A new event whose invoker is built bycombinerfrom the current listeners, in phase order.booleaninvoker()Fires the event: calls every listener as the event combines them.static <T> EventInvoker<T> The event with this listener type.voidregister(EventPhase phase, T listener) Adds a listener in the given phase.default voidAdds a listener in theEventPhase.DEFAULTphase.type()The listener type identifying this event.booleanunregister(T listener) Removes a listener, from whichever phase it was added to.
-
Method Details
-
lookup
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
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 (elseEventResult.PASS);EventResultHolder: the same, with its value;boolean: listeners run until one returnsfalse.
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
A new event whose invoker is built bycombinerfrom 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
The listener type identifying this event. -
register
Adds a listener in theEventPhase.DEFAULTphase. -
register
Adds a listener in the given phase. -
unregister
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
-
invoker
T invoker()Fires the event: calls every listener as the event combines them.
-