Class EventHandler

java.lang.Object
com.csse3200.game.events.EventHandler

public class EventHandler extends Object
Send and receive events between objects. EventHandler provides an implementation of the Observer pattern, also known as an event system or publish/subscribe. When an event is triggered with trigger(), all listeners are notified of the event.

Currently supports up to 3 arguments for an event. More can be added, but consider instead passing a class with required fields.

If you get a ClassCastException from an event, trigger is being called with different arguments than the listeners expect.

  • Constructor Details

    • EventHandler

      public EventHandler()
  • Method Details

    • addListener

      public void addListener(String eventName, EventListener0 listener)
      Add a listener to an event with zero arguments
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T> void addListener(String eventName, EventListener1<T> listener)
      Add a listener to an event with one argument
      Type Parameters:
      T - argument type
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T0, T1> void addListener(String eventName, EventListener2<T0,T1> listener)
      Add a listener to an event with two arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • addListener

      public <T0, T1, T2> void addListener(String eventName, EventListener3<T0,T1,T2> listener)
      Add a listener to an event with three arguments
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      T2 - Type of arg 2
      Parameters:
      eventName - name of the event
      listener - function to call when event fires
    • trigger

      public void trigger(String eventName)
      Trigger an event with no arguments
      Parameters:
      eventName - name of the event
    • trigger

      public <T> void trigger(String eventName, T arg0)
      Trigger an event with one argument
      Type Parameters:
      T - argument type
      Parameters:
      eventName - name of the event
      arg0 - arg to pass to event
    • trigger

      public <T0, T1> void trigger(String eventName, T0 arg0, T1 arg1)
      Trigger an event with one argument
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      Parameters:
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
    • trigger

      public <T0, T1, T2> void trigger(String eventName, T0 arg0, T1 arg1, T2 arg2)
      Trigger an event with one argument
      Type Parameters:
      T0 - Type of arg 0
      T1 - Type of arg 1
      T2 - Type of arg 2
      Parameters:
      eventName - name of the event
      arg0 - arg 0 to pass to event
      arg1 - arg 1 to pass to event
      arg2 - arg 2 to pass to event