diff --git a/src/org/apollo/game/model/entity/event/Event.java b/src/org/apollo/game/model/entity/event/Event.java new file mode 100644 index 00000000..cf349844 --- /dev/null +++ b/src/org/apollo/game/model/entity/event/Event.java @@ -0,0 +1,10 @@ +package org.apollo.game.model.entity.event; + +/** + * A type of event that may occur in the game world. + * + * @author Major + */ +public abstract class Event { + +} \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/event/EventContext.java b/src/org/apollo/game/model/entity/event/EventContext.java new file mode 100644 index 00000000..f9568779 --- /dev/null +++ b/src/org/apollo/game/model/entity/event/EventContext.java @@ -0,0 +1,31 @@ +package org.apollo.game.model.entity.event; + +/** + * The context of an {@link Event}. + * + * @author Major + */ +final class EventContext { + + /** + * Indicates whether or not the Event chain has been terminated. + */ + private boolean terminated; + + /** + * Terminates the Event chain. + */ + public void terminate() { + terminated = true; + } + + /** + * Returns whether or not the Event chain has been terminated. + * + * @return {@code true} if the Event chain has been terminated, otherwise {@code false}. + */ + public boolean terminated() { + return terminated; + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/event/EventListener.java b/src/org/apollo/game/model/entity/event/EventListener.java new file mode 100644 index 00000000..46ad7a1c --- /dev/null +++ b/src/org/apollo/game/model/entity/event/EventListener.java @@ -0,0 +1,21 @@ +package org.apollo.game.model.entity.event; + +/** + * A listener for an {@link Event} that may occur in the game world. + * + * @author Major + * + * @param The type of Event. + */ +@FunctionalInterface +public interface EventListener { + + /** + * Handles the {@link Event} that occurred. + * + * @param event The Event. + * @param context The {@link EventContext}. + */ + public void handle(E event, EventContext context); + +} \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/event/EventListenerChain.java b/src/org/apollo/game/model/entity/event/EventListenerChain.java new file mode 100644 index 00000000..c72a09d8 --- /dev/null +++ b/src/org/apollo/game/model/entity/event/EventListenerChain.java @@ -0,0 +1,47 @@ +package org.apollo.game.model.entity.event; + +import java.util.ArrayList; +import java.util.List; + +/** + * A chain of {@link EventListener}s. + * + * @author Major + * @param The type of {@link Event} the listeners in this chain listen for. + */ +final class EventListenerChain { + + /** + * The List of EventListeners. + */ + private final List> listeners = new ArrayList<>(); + + /** + * Adds an {@link EventListener} to this chain. + * + * @param listener The EventListener to add. + */ + public void addListener(EventListener listener) { + listeners.add(listener); + } + + /** + * Notifies each {@link EventListener} in this chain that an {@link Event} has occurred. + * + * @param event The event. + * @return {@code true} if the Event should continue on with its outcome, {@code false} if not. + */ + public boolean notify(E event) { + EventContext context = new EventContext(); + for (EventListener listener : listeners) { + listener.handle(event, context); + + if (context.terminated()) { + return false; + } + } + + return false; + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/event/EventListenerChainSet.java b/src/org/apollo/game/model/entity/event/EventListenerChainSet.java new file mode 100644 index 00000000..b8d327a4 --- /dev/null +++ b/src/org/apollo/game/model/entity/event/EventListenerChainSet.java @@ -0,0 +1,40 @@ +package org.apollo.game.model.entity.event; + +import java.util.HashMap; +import java.util.Map; + +/** + * A set of {@link EventListenerChain}s. + * + * @author Major + */ +public final class EventListenerChainSet { + + /** + * The Map of Event Classes to EventListenerChains. + */ + private final Map, EventListenerChain> chains = new HashMap<>(); + + /** + * Notifies the appropriate {@link EventListenerChain} that an {@link Event} has occurred. + * + * @param event The Event. + * @return {@code true} if the Event should continue on with its outcome, {@code false} if not. + */ + public boolean notify(E event) { + @SuppressWarnings("unchecked") + EventListenerChain chain = (EventListenerChain) chains.get(event); + return chain.notify(event); + } + + /** + * Places the {@link EventListenerChain} into this set. + * + * @param clazz The {@link Class} to associate the EventListenerChain with. + * @param chain The EventListenerChain. + */ + public void putListenerChain(Class clazz, EventListenerChain chain) { + chains.put(clazz, chain); + } + +} \ No newline at end of file diff --git a/src/org/apollo/game/model/entity/event/package-info.java b/src/org/apollo/game/model/entity/event/package-info.java new file mode 100644 index 00000000..6b10ac54 --- /dev/null +++ b/src/org/apollo/game/model/entity/event/package-info.java @@ -0,0 +1,4 @@ +/** + * Contains event-related classes. + */ +package org.apollo.game.model.entity.event; \ No newline at end of file