diff --git a/src/org/apollo/game/model/World.java b/src/org/apollo/game/model/World.java index 990a8a40..3374eb10 100644 --- a/src/org/apollo/game/model/World.java +++ b/src/org/apollo/game/model/World.java @@ -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 void listenFor(Class type, EventListener 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)); + } + } \ 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 index c72a09d8..b46f8dc9 100644 --- a/src/org/apollo/game/model/entity/event/EventListenerChain.java +++ b/src/org/apollo/game/model/entity/event/EventListenerChain.java @@ -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 { */ private final List> listeners = new ArrayList<>(); + /** + * The Class type of this chain. + */ + private final Class type; + + /** + * Creates the EventListenerChain. + * + * @param type The {@link Class} type of this chain. + */ + public EventListenerChain(Class type) { + this.type = type; + } + /** * Adds an {@link EventListener} to this chain. * @@ -44,4 +60,8 @@ final class EventListenerChain { return false; } + @Override + public String toString() { + return MoreObjects.toStringHelper(this).add("type", type).add("listeners", listeners).toString(); + } } \ 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 index b8d327a4..4df75722 100644 --- a/src/org/apollo/game/model/entity/event/EventListenerChainSet.java +++ b/src/org/apollo/game/model/entity/event/EventListenerChainSet.java @@ -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 void putListenerChain(Class clazz, EventListenerChain chain) { - chains.put(clazz, chain); + public void putListener(Class clazz, EventListener listener) { + @SuppressWarnings("unchecked") + EventListenerChain chain = (EventListenerChain) chains.computeIfAbsent(clazz, EventListenerChain::new); + chain.addListener(listener); } } \ No newline at end of file