Restructure region calculations to occur in the RegionManager, and send out new PlayerRegionChangeEvents
This commit is contained in:
parent
d58663b8fa
commit
b2dbf53fd1
@ -24,30 +24,46 @@ import org.bukkit.event.player.PlayerTeleportEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerRespawnEvent;
|
||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class BukkitEventHandler implements Listener {
|
||||
Plugin m_plugin;
|
||||
public BukkitEventHandler(Plugin p) {
|
||||
m_plugin = p;
|
||||
RegionManager m_manager;
|
||||
public BukkitEventHandler(RegionManager manager) {
|
||||
m_manager = manager;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onTeleport(PlayerTeleportEvent event) {
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
m_manager.recalculatePlayerRegions();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onJoin(PlayerJoinEvent event) {
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
m_manager.recalculatePlayerRegions();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onRespawn(PlayerRespawnEvent event) {
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
m_manager.recalculatePlayerRegions();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onWorldChange(PlayerChangedWorldEvent event) {
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
m_manager.recalculatePlayerRegions();
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerRegionChanged(PlayerRegionChangeEvent event) {
|
||||
if (event.oldRegion != null) {
|
||||
for (Player p : m_manager.playersInRegion(event.oldRegion)) {
|
||||
p.sendMessage(event.player.getName()+" has left the region.");
|
||||
}
|
||||
}
|
||||
for (Player p : m_manager.playersInRegion(event.newRegion)) {
|
||||
if (p != event.player) {
|
||||
p.sendMessage(event.player.getName()+" has entered the region.");
|
||||
}
|
||||
}
|
||||
event.player.sendMessage("Now entering region: "+event.newRegion.name());
|
||||
}
|
||||
}
|
||||
|
@ -43,7 +43,6 @@ public class CityRegionCommand implements CommandExecutor {
|
||||
if (city != null) {
|
||||
if (p.getLocation().distance(nearest.teleportLocation()) <= 5) {
|
||||
p.teleport(city.teleportLocation(), TeleportCause.COMMAND);
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
} else {
|
||||
sender.sendMessage("You must be within 5 blocks of a region center.");
|
||||
}
|
||||
|
@ -44,7 +44,6 @@ public class HomeRegionCommand implements CommandExecutor {
|
||||
if (home != null) {
|
||||
if (p.getLocation().distance(nearest.teleportLocation()) <= 5) {
|
||||
p.teleport(home.teleportLocation(), TeleportCause.COMMAND);
|
||||
m_plugin.recalculatePlayerRegions();
|
||||
} else {
|
||||
sender.sendMessage("You must be within 5 blocks of a region center.");
|
||||
}
|
||||
|
45
src/main/java/us/camin/regions/PlayerRegionChangeEvent.java
Normal file
45
src/main/java/us/camin/regions/PlayerRegionChangeEvent.java
Normal file
@ -0,0 +1,45 @@
|
||||
package us.camin.regions;
|
||||
|
||||
/**
|
||||
* This file is part of Regions
|
||||
*
|
||||
* Regions is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Regions is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Regions. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class PlayerRegionChangeEvent extends Event {
|
||||
private static final HandlerList s_handlers = new HandlerList();
|
||||
public final Region oldRegion;
|
||||
public final Region newRegion;
|
||||
public final Player player;
|
||||
|
||||
public PlayerRegionChangeEvent(Player p, Region oldRegion, Region newRegion) {
|
||||
this.oldRegion = oldRegion;
|
||||
this.newRegion = newRegion;
|
||||
this.player = p;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return s_handlers;
|
||||
}
|
||||
|
||||
public static HandlerList getHandlerList() {
|
||||
return s_handlers;
|
||||
}
|
||||
}
|
@ -19,51 +19,15 @@ package us.camin.regions;
|
||||
*/
|
||||
|
||||
import java.lang.Runnable;
|
||||
import java.util.logging.Logger;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.Location;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class PlayerWatcher implements Runnable {
|
||||
Logger log = Logger.getLogger("Regions.PlayerWatcher");
|
||||
private Plugin m_plugin;
|
||||
private Map<Player, Region> m_lastKnownRegions;
|
||||
private RegionManager m_manager;
|
||||
|
||||
public PlayerWatcher(Plugin p) {
|
||||
m_plugin = p;
|
||||
m_lastKnownRegions = new HashMap<Player, Region>();
|
||||
public PlayerWatcher(RegionManager manager) {
|
||||
m_manager = manager;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
Player[] allPlayers = m_plugin.getServer().getOnlinePlayers();
|
||||
RegionManager manager = m_plugin.regionManager();
|
||||
Map<Region, List<Player>> newPlayers = new HashMap<Region, List<Player>>();
|
||||
for (Player p : allPlayers) {
|
||||
Location loc = p.getLocation();
|
||||
Region nearest = manager.nearestRegion(loc);
|
||||
if (nearest != null) {
|
||||
log.finest("Current region for "+p.getName()+": "+nearest.name());
|
||||
if (nearest != m_lastKnownRegions.get(p)) {
|
||||
p.sendMessage("Now entering region: "+nearest.name());
|
||||
log.fine("Player "+p.getName()+" entered region "+nearest.name());
|
||||
m_lastKnownRegions.put(p, nearest);
|
||||
if (!newPlayers.containsKey(nearest))
|
||||
newPlayers.put(nearest, new ArrayList<Player>());
|
||||
newPlayers.get(nearest).add(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Region r : newPlayers.keySet()) {
|
||||
for (Player newPlayer : newPlayers.get(r)) {
|
||||
for (Player oldPlayer : manager.filterPlayersInRegion(r, allPlayers)) {
|
||||
if (oldPlayer != newPlayer) {
|
||||
oldPlayer.sendMessage(newPlayer.getName()+" has entered the region");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
m_manager.recalculatePlayerRegions();
|
||||
}
|
||||
}
|
||||
|
@ -26,6 +26,8 @@ import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.plugin.ServicesManager;
|
||||
import org.dynmap.markers.MarkerAPI;
|
||||
import org.dynmap.DynmapCommonAPI;
|
||||
|
||||
@ -42,15 +44,11 @@ public class Plugin extends JavaPlugin implements RegionAPI {
|
||||
return m_regions;
|
||||
}
|
||||
|
||||
public void recalculatePlayerRegions() {
|
||||
m_playerWatcher.run();
|
||||
}
|
||||
|
||||
public void onEnable() {
|
||||
log.info("[Regions] Enabling Regions");
|
||||
m_regions = new RegionManager(getServer().getPluginManager());
|
||||
m_regions = new RegionManager(getServer());
|
||||
|
||||
m_playerWatcher = new PlayerWatcher(this);
|
||||
m_playerWatcher = new PlayerWatcher(m_regions);
|
||||
|
||||
getServer().getScheduler().scheduleAsyncRepeatingTask(this, m_playerWatcher, 0, 5*20);
|
||||
|
||||
@ -60,7 +58,7 @@ public class Plugin extends JavaPlugin implements RegionAPI {
|
||||
getCommand("homeregion").setExecutor(new HomeRegionCommand(this));
|
||||
getCommand("movein").setExecutor(new MoveinCommand(this));
|
||||
|
||||
getServer().getPluginManager().registerEvents(new BukkitEventHandler(this), this);
|
||||
getServer().getPluginManager().registerEvents(new BukkitEventHandler(m_regions), this);
|
||||
|
||||
org.bukkit.plugin.Plugin mapPlugin = getServer().getPluginManager().getPlugin("dynmap");
|
||||
if (mapPlugin instanceof DynmapCommonAPI) {
|
||||
@ -76,6 +74,9 @@ public class Plugin extends JavaPlugin implements RegionAPI {
|
||||
log.info("[Regions] Dynmap not found. Disabling map support.");
|
||||
}
|
||||
|
||||
ServicesManager sm = getServer().getServicesManager();
|
||||
sm.register(RegionAPI.class, this, this, ServicePriority.Normal);
|
||||
|
||||
loadRegions();
|
||||
}
|
||||
|
||||
@ -98,7 +99,7 @@ public class Plugin extends JavaPlugin implements RegionAPI {
|
||||
m_regions.clear();
|
||||
if (section != null)
|
||||
m_regions.loadRegions(section, getServer());
|
||||
recalculatePlayerRegions();
|
||||
m_regions.recalculatePlayerRegions();
|
||||
}
|
||||
|
||||
public void saveRegions() {
|
||||
|
@ -19,6 +19,5 @@ package us.camin.regions;
|
||||
*/
|
||||
|
||||
public interface RegionAPI {
|
||||
public void recalculatePlayerRegions();
|
||||
public RegionManager regionManager();
|
||||
}
|
||||
|
@ -24,6 +24,7 @@ import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
|
||||
import java.util.logging.Logger;
|
||||
@ -40,50 +41,88 @@ public class RegionManager {
|
||||
private Map<String, Collection<Region>> m_regions;
|
||||
private Map<String, Region> m_cityRegions;
|
||||
private Map<String, Region> m_homeRegions;
|
||||
private PluginManager m_pm;
|
||||
private Server m_server;
|
||||
private Map<Player, Region> m_lastKnownRegions;
|
||||
private Map<Region, Collection<Player>> m_regionPlayerLists;
|
||||
|
||||
public RegionManager(PluginManager pm) {
|
||||
m_pm = pm;
|
||||
public RegionManager(Server server) {
|
||||
m_server = server;
|
||||
clear();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
public synchronized Collection<Player> playersInRegion(Region r) {
|
||||
if (m_regionPlayerLists.get(r) == null)
|
||||
return Collections.unmodifiableCollection(new ArrayList<Player>());
|
||||
return Collections.unmodifiableCollection(m_regionPlayerLists.get(r));
|
||||
}
|
||||
|
||||
public synchronized void recalculatePlayerRegions() {
|
||||
ArrayList<Event> updateEvents = new ArrayList<Event>();
|
||||
Player[] allPlayers = m_server.getOnlinePlayers();
|
||||
for (Player p : allPlayers) {
|
||||
Location loc = p.getLocation();
|
||||
Region nearest = nearestRegion(loc);
|
||||
if (nearest != null) {
|
||||
log.finest("Current region for "+p.getName()+": "+nearest.name());
|
||||
Region last = m_lastKnownRegions.get(p);
|
||||
if (nearest != last) {
|
||||
updateEvents.add(new PlayerRegionChangeEvent(p, last, nearest));
|
||||
log.fine("Player "+p.getName()+" entered region "+nearest.name());
|
||||
m_regionPlayerLists.get(nearest).add(p);
|
||||
if (m_regionPlayerLists.get(last) != null)
|
||||
m_regionPlayerLists.get(last).remove(p);
|
||||
m_lastKnownRegions.put(p, nearest);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (Event e : updateEvents) {
|
||||
m_server.getPluginManager().callEvent(e);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void clear() {
|
||||
m_regions = new HashMap<String, Collection<Region>>();
|
||||
m_cityRegions = new HashMap<String, Region>();
|
||||
m_homeRegions = new HashMap<String, Region>();
|
||||
m_lastKnownRegions = new HashMap<Player, Region>();
|
||||
m_regionPlayerLists = new HashMap<Region, Collection<Player>>();
|
||||
}
|
||||
|
||||
public void renameWorld(String oldName, String newName) {
|
||||
public synchronized void renameWorld(String oldName, String newName) {
|
||||
log.fine("Renaming "+oldName+" to "+newName);
|
||||
m_regions.put(newName, m_regions.remove(oldName));
|
||||
m_cityRegions.put(newName, m_cityRegions.remove(oldName));
|
||||
}
|
||||
|
||||
public Region cityRegion(String worldName) {
|
||||
public synchronized Region cityRegion(String worldName) {
|
||||
return m_cityRegions.get(worldName);
|
||||
}
|
||||
|
||||
public void setCityRegion(String worldName, Region region) {
|
||||
public synchronized void setCityRegion(String worldName, Region region) {
|
||||
m_cityRegions.put(worldName, region);
|
||||
}
|
||||
|
||||
public boolean addRegion(Region r) {
|
||||
public synchronized boolean addRegion(Region r) {
|
||||
String worldName = r.location().getWorld().getName();
|
||||
log.fine("Adding new region "+r.name()+" at "+r.location());
|
||||
if (!m_regions.containsKey(worldName))
|
||||
m_regions.put(worldName, new ArrayList<Region>());
|
||||
if (m_regions.get(worldName).add(r)) {
|
||||
m_pm.callEvent(new RegionCreateEvent(r));
|
||||
m_regionPlayerLists.put(r, new ArrayList<Player>());
|
||||
m_server.getPluginManager().callEvent(new RegionCreateEvent(r));
|
||||
recalculatePlayerRegions();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean removeRegion(Region r) {
|
||||
public synchronized boolean removeRegion(Region r) {
|
||||
String worldName = r.location().getWorld().getName();
|
||||
log.fine("Removing region "+r.name()+" from "+r.location());
|
||||
if (m_regions.containsKey(worldName)) {
|
||||
if (m_regions.get(worldName).remove(r)) {
|
||||
m_pm.callEvent(new RegionRemoveEvent(r));
|
||||
m_regionPlayerLists.remove(r);
|
||||
m_server.getPluginManager().callEvent(new RegionRemoveEvent(r));
|
||||
recalculatePlayerRegions();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -94,23 +133,13 @@ public class RegionManager {
|
||||
return regionsForWorld(world.getName());
|
||||
}
|
||||
|
||||
public Collection<Region> regionsForWorld(String worldName) {
|
||||
public synchronized Collection<Region> regionsForWorld(String worldName) {
|
||||
if (m_regions.containsKey(worldName))
|
||||
return Collections.unmodifiableCollection(m_regions.get(worldName));
|
||||
else
|
||||
return Collections.unmodifiableCollection(new ArrayList<Region>());
|
||||
}
|
||||
|
||||
public List<Player> filterPlayersInRegion(Region r, Player[] players) {
|
||||
ArrayList<Player> ret = new ArrayList<Player>();
|
||||
for (Player p : players) {
|
||||
Region nearest = nearestRegion(p.getLocation());
|
||||
if (nearest == r)
|
||||
ret.add(p);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
public Region nearestRegion(Location loc) {
|
||||
Collection<Region> regions = regionsForWorld(loc.getWorld());
|
||||
Region nearest = null;
|
||||
@ -125,7 +154,7 @@ public class RegionManager {
|
||||
return nearest;
|
||||
}
|
||||
|
||||
public void saveRegions(ConfigurationSection section) {
|
||||
public synchronized void saveRegions(ConfigurationSection section) {
|
||||
for(String worldName : m_regions.keySet()) {
|
||||
ConfigurationSection worldSection = section.createSection(worldName);
|
||||
Region cityRegion = cityRegion(worldName);
|
||||
@ -147,15 +176,15 @@ public class RegionManager {
|
||||
}
|
||||
}
|
||||
|
||||
public Region homeRegion(String playerName) {
|
||||
public synchronized Region homeRegion(String playerName) {
|
||||
return m_homeRegions.get(playerName);
|
||||
}
|
||||
|
||||
public void setHomeRegion(String player, Region r) {
|
||||
public synchronized void setHomeRegion(String player, Region r) {
|
||||
m_homeRegions.put(player, r);
|
||||
}
|
||||
|
||||
public void loadRegions(ConfigurationSection section, Server server) {
|
||||
public synchronized void loadRegions(ConfigurationSection section, Server server) {
|
||||
Set<String> worldNames = section.getKeys(false);
|
||||
for(String worldName : worldNames) {
|
||||
ConfigurationSection worldSection = section.getConfigurationSection(worldName);
|
||||
|
Loading…
Reference in New Issue
Block a user