diff --git a/game/src/main/org/apollo/game/model/area/Region.java b/game/src/main/org/apollo/game/model/area/Region.java index 1e66b211..9b2f4a8c 100644 --- a/game/src/main/org/apollo/game/model/area/Region.java +++ b/game/src/main/org/apollo/game/model/area/Region.java @@ -163,6 +163,10 @@ public final class Region { addEntity(entity, true); } + public void addListener(RegionListener listener) { + listeners.add(listener); + } + /** * Checks if this Region contains the specified Entity. * diff --git a/game/src/main/org/apollo/game/model/area/RegionRepository.java b/game/src/main/org/apollo/game/model/area/RegionRepository.java index 693ce8cd..902ffdbf 100644 --- a/game/src/main/org/apollo/game/model/area/RegionRepository.java +++ b/game/src/main/org/apollo/game/model/area/RegionRepository.java @@ -1,5 +1,6 @@ package org.apollo.game.model.area; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -48,6 +49,11 @@ public final class RegionRepository { */ private final Map regions = new HashMap<>(); + /** + * A list of default {@link RegionListener}s which will be added to {@link Region}s upon creation. + */ + private final List defaultRegionListeners = new ArrayList<>(); + /** * Creates a new RegionRepository. * @@ -71,9 +77,24 @@ public final class RegionRepository { throw new UnsupportedOperationException("Cannot add a Region with the same coordinates as an existing Region."); } + defaultRegionListeners.forEach(region::addListener); regions.put(region.getCoordinates(), region); } + /** + * Adds a {@link RegionListener} to be registered as a default listener with all newly created {@link Region}s and + * associated with any existing instances. + * + * @param listener The listener to add. + */ + public void addRegionListener(RegionListener listener) { + for (Region region : regions.values()) { + region.addListener(listener); + } + + defaultRegionListeners.add(listener); + } + /** * Indicates whether the supplied value (i.e. the {@link Region}) has a mapping. *