Finish custom Events.

This commit is contained in:
Major-
2015-02-27 03:35:26 +00:00
parent d60075dcf6
commit 3d2484c42d
3 changed files with 56 additions and 7 deletions
+31 -4
View File
@@ -27,6 +27,9 @@ import org.apollo.game.model.entity.Entity;
import org.apollo.game.model.entity.GameObject;
import org.apollo.game.model.entity.Npc;
import org.apollo.game.model.entity.Player;
import org.apollo.game.model.entity.event.Event;
import org.apollo.game.model.entity.event.EventListener;
import org.apollo.game.model.entity.event.EventListenerChainSet;
import org.apollo.game.scheduling.ScheduledTask;
import org.apollo.game.scheduling.Scheduler;
import org.apollo.io.EquipmentDefinitionParser;
@@ -91,6 +94,11 @@ public final class World {
*/
private final CommandDispatcher commandDispatcher = new CommandDispatcher();
/**
* The EventListenerChainSet for this World.
*/
private final EventListenerChainSet events = new EventListenerChainSet();
/**
* The login dispatcher.
*/
@@ -279,12 +287,13 @@ public final class World {
}
/**
* Adds entities to sectors in the {@link SectorRepository}.
* Adds an {@link EventListener}, listening for an {@link Event} of the specified type.
*
* @param entities The entities.
* @param type The type of the Event.
* @param listener The EventListener.
*/
private void placeEntities(Entity... entities) {
Arrays.stream(entities).forEach(entity -> sectors.fromPosition(entity.getPosition()).addEntity(entity));
public <E extends Event> void listenFor(Class<E> type, EventListener<E> listener) {
events.putListener(type, listener);
}
/**
@@ -348,6 +357,15 @@ public final class World {
return scheduler.schedule(task);
}
/**
* Submits the specified {@link Event}, passing it to the listeners..
*
* @param event The Event.
*/
public void submit(Event event) {
events.notify(event);
}
/**
* Unregisters the specified {@link Npc}.
*
@@ -382,4 +400,13 @@ public final class World {
}
}
/**
* Adds entities to sectors in the {@link SectorRepository}.
*
* @param entities The entities.
*/
private void placeEntities(Entity... entities) {
Arrays.stream(entities).forEach(entity -> sectors.fromPosition(entity.getPosition()).addEntity(entity));
}
}
@@ -3,6 +3,8 @@ package org.apollo.game.model.entity.event;
import java.util.ArrayList;
import java.util.List;
import com.google.common.base.MoreObjects;
/**
* A chain of {@link EventListener}s.
*
@@ -16,6 +18,20 @@ final class EventListenerChain<E extends Event> {
*/
private final List<EventListener<E>> listeners = new ArrayList<>();
/**
* The Class type of this chain.
*/
private final Class<E> type;
/**
* Creates the EventListenerChain.
*
* @param type The {@link Class} type of this chain.
*/
public EventListenerChain(Class<E> type) {
this.type = type;
}
/**
* Adds an {@link EventListener} to this chain.
*
@@ -44,4 +60,8 @@ final class EventListenerChain<E extends Event> {
return false;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("type", type).add("listeners", listeners).toString();
}
}
@@ -31,10 +31,12 @@ public final class EventListenerChainSet {
* Places the {@link EventListenerChain} into this set.
*
* @param clazz The {@link Class} to associate the EventListenerChain with.
* @param chain The EventListenerChain.
* @param listener The EventListenerChain.
*/
public <E extends Event> void putListenerChain(Class<E> clazz, EventListenerChain<E> chain) {
chains.put(clazz, chain);
public <E extends Event> void putListener(Class<E> clazz, EventListener<E> listener) {
@SuppressWarnings("unchecked")
EventListenerChain<E> chain = (EventListenerChain<E>) chains.computeIfAbsent(clazz, EventListenerChain::new);
chain.addListener(listener);
}
}