Class EventFactory

java.lang.Object
net.fabricmc.fabric.api.event.EventFactory

public final class EventFactory extends Object
Helper for creating Event classes.
  • Method Details

    • isProfilingEnabled

      public static boolean isProfilingEnabled()
      Returns:
      True if events are supposed to be profiled.
    • invalidate

      public static void invalidate()
      Invalidate and re-create all existing "invoker" instances across events created by this EventFactory. Use this if, for instance, the profilingEnabled field changes.
    • createArrayBacked

      public static <T> Event<T> createArrayBacked(Class<? super T> type, Function<T[],T> invokerFactory)
      Create an "array-backed" Event instance.

      If your factory simply delegates to the listeners without adding custom behavior, consider using the other overload if performance of this event is critical.

      Type Parameters:
      T - The listener type.
      Parameters:
      type - The listener class type.
      invokerFactory - The invoker factory, combining multiple listeners into one instance.
      Returns:
      The Event instance.
    • createArrayBacked

      public static <T> Event<T> createArrayBacked(Class<T> type, T emptyInvoker, Function<T[],T> invokerFactory)
      Create an "array-backed" Event instance with a custom empty invoker, for an event whose invokerFactory only delegates to the listeners.
      • If there is no listener, the custom empty invoker will be used.
      • If there is only one listener, that one will be used as the invoker and the factory will not be called.
      • Only when there are at least two listeners will the factory be used.

      Having a custom empty invoker (of type (...) -> {}) increases performance relative to iterating over an empty array; however, it only really matters if the event is executed thousands of times a second.

      Type Parameters:
      T - The listener type.
      Parameters:
      type - The listener class type.
      emptyInvoker - The custom empty invoker.
      invokerFactory - The invoker factory, combining multiple listeners into one instance.
      Returns:
      The Event instance.
    • createWithPhases

      public static <T> Event<T> createWithPhases(Class<? super T> type, Function<T[],T> invokerFactory, Identifier... defaultPhases)
      Create an array-backed event with a list of default phases that get invoked in order. Exposing the identifiers of the default phases as public static final constants is encouraged.

      An event phase is a named group of listeners, which may be ordered before or after other groups of listeners. This allows some listeners to take priority over other listeners. Adding separate events should be considered before making use of multiple event phases.

      Phases may be freely added to events created with any of the factory functions, however using this function is preferred for widely used event phases. If more phases are necessary, discussion with the author of the Event is encouraged.

      Refer to Event.addPhaseOrdering(net.minecraft.util.Identifier, net.minecraft.util.Identifier) for an explanation of event phases.

      Type Parameters:
      T - The listener type.
      Parameters:
      type - The listener class type.
      invokerFactory - The invoker factory, combining multiple listeners into one instance.
      defaultPhases - The default phases of this event, in the correct order. Must contain Event.DEFAULT_PHASE.
      Returns:
      The Event instance.
    • getHandlerName

      public static String getHandlerName(Object handler)
      Get the listener object name. This can be used in debugging/profiling scenarios.
      Parameters:
      handler - The listener object.
      Returns:
      The listener name.