Class EventListeners<T extends Event>

java.lang.Object
com.prineside.tdi2.events.EventListeners<T>
Type Parameters:
T - type of listener class

public final class EventListeners<T extends Event> extends Object
Contains a list of listeners for a specific event type. Thread safe. Listeners may have up to 8 different (binary) flags set to control their behavior. Flags are being set as a bit mask which takes 1 byte: - 0b1 / 1 / 1 << 1 / FLAG_AUTO_PRIORITY - set by the EventListeners automatically, ignored if set manually. Marks listeners to decide their auto-priority - 0b10 / 2 / 1 << 2 / FLAG_AFFECTS_STATE - can be used to denote listener as state-affecting, normally used only in a game session. By default, all listeners are not considered to be state-affecting thus will not be serialized (will throw exception if you try), will not affect the order and priorities of the state-affecting listeners and will not be compared during sync checks. A simple rule: if your listener affects game's state (for example, gives score to the player = state-affecting, forces some graphics / ui to update = not-state-affecting), then use this flag on it - 0b100 / 4 / 1 << 3 / FLAG_PERSISTENT - listeners marked with this flag will survive EventDispatcher.reset(false). Note: Engine.reset() triggers EventDispatcher.reset(false) - Other flags / bytes (0b11111000) may depend on custom implementation of EventDispatcher. By default, you are free to use them as you wish with the default implementation of EventDispatcher
  • Field Details

  • Constructor Details

    • EventListeners

      public EventListeners(Class<?> eventClass)
  • Method Details

    • getEntriesBackingArray

      public EventListeners.Entry<T>[] getEntriesBackingArray()
      Iterate from 0 to size(), may contain nulls for (int i = 0; i < listeners.size(); i++) { EventListeners.Entry<?> entry = listeners.getEntriesBackingArray()[i]; if (entry == null) continue; ... }
    • trigger

      public void trigger(T event)
    • getEventClass

      public Class<?> getEventClass()
      Returns:
      event type class passed to listeners
    • contains

      public boolean contains(Listener<T> listener)
    • getStateHash

      public int getStateHash()
    • getStateAffectingEntriesCount

      public int getStateAffectingEntriesCount()
    • getNonStateAffectingEntriesCount

      public int getNonStateAffectingEntriesCount()
    • getLoops

      public int getLoops()
    • add

      public EventListeners.Entry<T> add(Listener<T> listener)
      Add listener without flags with auto priority Note: listener will only get FLAG_AUTO_PRIORITY (not state-affecting, not persistent etc)
    • addWithPriority

      public EventListeners.Entry<T> addWithPriority(Listener<T> listener, int priority)
      Add listener without flags with manual priority Note: flags will be 0 (not state-affecting, not persistent etc)
    • addStateAffectingWithPriority

      public EventListeners.Entry<T> addStateAffectingWithPriority(Listener<T> listener, int priority)
      Add listener with STATE_AFFECTING flag with manual priority
    • addWithFlags

      public EventListeners.Entry<T> addWithFlags(Listener<T> listener, int flags)
      Add listener and set its priority automatically (will be lower than priority of any of the existing listeners in stateful / not stateful groups) Throws exception if listener is already added. Checks for AFFECTS_STATE flag.
      Parameters:
      flags - will be set on the listener with FLAG_AUTO_PRIORITY set to true. Its AFFECTS_STATE bit will be checked to decide its priority among the listeners of the same state-affection Note: resulting flags will be exactly the same as in @param flags but with FLAG_AUTO_PRIORITY set to true
    • addStateAffecting

      public EventListeners.Entry<T> addStateAffecting(Listener<T> listener)
      Auto-priority
    • add

      public EventListeners.Entry<T> add(Listener<T> listener, int flags, int priority)
      Throws exception if listener does not affect game state but has been added during iteration.
      Returns:
      entry in the group
    • remove

      public boolean remove(Listener<T> listener)
      Remove (unsubscribe) the listener. If event is being triggered, the listener will be removed at the end of the loop. Note: method references / lambdas are dynamic in Java, do not pass method references as listeners if you want to remove them later (or store them in some field first) as it is impossible to find a subscribed listener by method reference (new object is being created each time)
      Returns:
      true if listener has been removed, false if it was not scheduled and was not in the list of listeners to be added
    • getEventsTriggered

      public int getEventsTriggered()
      Returns:
      number of begun events (begin() calls)
    • getEventsStopped

      public int getEventsStopped()
    • size

      public int size()
    • clear

      public void clear()
    • describe

      public StringBuilder describe()