Interface Events


@NonExtendable public interface Events
Registration of event listeners. Listeners of one event run in registration order. A listener that throws is logged with the mod's id; one that throws repeatedly is removed.
  • Method Details

    • onMain

      <E extends Event> Subscription onMain(Class<E> type, Consumer<? super E> listener)
      listener runs on the main thread while the event is posted. It may touch game objects and must return quickly: it runs inside the tick.
    • onModThread

      <E extends Event> Subscription onModThread(Class<E> type, Consumer<? super E> listener)
      listener runs later on the mod's own thread. It must not touch game objects; main-thread work goes through main().
      Throws:
      IllegalArgumentException - when type is a MainThreadEvent
    • register

      Subscription register(Object listener)
      Registers the methods of listener annotated with OnMain, OnModThread, or OnClientCommand, as onMain(Class, Consumer) and onModThread(Class, Consumer) do, and schedules those annotated with Every or EveryNTicks. ZomboidForge registers the mod's entry point this way after its constructor and before Mod.onLoad(); other objects opt in through this method.

      Each annotated method:

      • returns void. A listener has one parameter, a class that implements Event and is neither abstract nor an interface, because listeners are matched by the event's exact class; a scheduled method has none;
      • carries one of the annotations; OnModThread not for a MainThreadEvent, OnClientCommand only for a ClientCommandEvent;
      • may be static, private, and declared in a superclass of listener's class. An instance method that a subclass overrides listens only when the override carries the annotation itself.
      Superclass methods are registered before subclass methods, and the methods of one class sorted by name, then by parameter type.
      Returns:
      one subscription for every listener and scheduled method of listener; cancelling it allows registering listener again
      Throws:
      IllegalArgumentException - listing every method that breaks a rule, when a class the methods name cannot be loaded, or when listener is registered already