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.
    • 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.