mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 00:38:21 +00:00
Add custom Event support.
This commit is contained in:
@@ -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 {
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 <E> The type of Event.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface EventListener<E> {
|
||||
|
||||
/**
|
||||
* Handles the {@link Event} that occurred.
|
||||
*
|
||||
* @param event The Event.
|
||||
* @param context The {@link EventContext}.
|
||||
*/
|
||||
public void handle(E event, EventContext context);
|
||||
|
||||
}
|
||||
@@ -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 <E> The type of {@link Event} the listeners in this chain listen for.
|
||||
*/
|
||||
final class EventListenerChain<E extends Event> {
|
||||
|
||||
/**
|
||||
* The List of EventListeners.
|
||||
*/
|
||||
private final List<EventListener<E>> listeners = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Adds an {@link EventListener} to this chain.
|
||||
*
|
||||
* @param listener The EventListener to add.
|
||||
*/
|
||||
public void addListener(EventListener<E> 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<E> listener : listeners) {
|
||||
listener.handle(event, context);
|
||||
|
||||
if (context.terminated()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<Class<? extends Event>, EventListenerChain<? extends Event>> 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 <E extends Event> boolean notify(E event) {
|
||||
@SuppressWarnings("unchecked")
|
||||
EventListenerChain<E> chain = (EventListenerChain<E>) 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 <E extends Event> void putListenerChain(Class<E> clazz, EventListenerChain<E> chain) {
|
||||
chains.put(clazz, chain);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Contains event-related classes.
|
||||
*/
|
||||
package org.apollo.game.model.entity.event;
|
||||
Reference in New Issue
Block a user