Allow registering region listeners globally

Adds the ability to register region listeners with the
RegionRepository, which will register the listener with any existing
regions and cache it for any newly created regions later.
This commit is contained in:
Gary Tierney
2016-12-30 23:47:03 +00:00
parent 6a4625cb32
commit 376d36871a
2 changed files with 25 additions and 0 deletions
@@ -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.
*
@@ -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<RegionCoordinates, Region> regions = new HashMap<>();
/**
* A list of default {@link RegionListener}s which will be added to {@link Region}s upon creation.
*/
private final List<RegionListener> 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.
*