From 978a203aa984b02cc6833780ed2d814a16cc2d3d Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Wed, 12 Oct 2011 13:18:00 +0800 Subject: [PATCH] Fix off-server-thread calls to get player list --- src/main/java/org/dynmap/DynmapPlugin.java | 10 +++++++++ src/main/java/org/dynmap/PlayerList.java | 26 +++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/dynmap/DynmapPlugin.java b/src/main/java/org/dynmap/DynmapPlugin.java index 04b3b8a4..b53b4725 100644 --- a/src/main/java/org/dynmap/DynmapPlugin.java +++ b/src/main/java/org/dynmap/DynmapPlugin.java @@ -268,6 +268,16 @@ public class DynmapPlugin extends JavaPlugin { playerList = new PlayerList(getServer(), getFile("hiddenplayers.txt"), configuration); playerList.load(); + PlayerListener pl = new PlayerListener() { + public void onPlayerJoin(PlayerJoinEvent evt) { + playerList.updateOnlinePlayers(null); + } + public void onPlayerQuit(PlayerQuitEvent evt) { + playerList.updateOnlinePlayers(evt.getPlayer()); + } + }; + registerEvent(Type.PLAYER_JOIN, pl); + registerEvent(Type.PLAYER_QUIT, pl); mapManager = new MapManager(this, configuration); mapManager.startRendering(); diff --git a/src/main/java/org/dynmap/PlayerList.java b/src/main/java/org/dynmap/PlayerList.java index 49444d86..52c303b9 100644 --- a/src/main/java/org/dynmap/PlayerList.java +++ b/src/main/java/org/dynmap/PlayerList.java @@ -19,11 +19,13 @@ public class PlayerList { private HashSet hiddenPlayerNames = new HashSet(); private File hiddenPlayersFile; private ConfigurationNode configuration; + private Player[] online; public PlayerList(Server server, File hiddenPlayersFile, ConfigurationNode configuration) { this.server = server; this.hiddenPlayersFile = hiddenPlayersFile; this.configuration = configuration; + updateOnlinePlayers(null); } public void save() { @@ -75,10 +77,11 @@ public class PlayerList { // TODO: Clean this up... one day public Player[] getVisiblePlayers(String worldName) { ArrayList visiblePlayers = new ArrayList(); - Player[] onlinePlayers = server.getOnlinePlayers(); + Player[] onlinePlayers = online; /* Use copied list - we don't call from server thread */ boolean useWhitelist = configuration.getBoolean("display-whitelist", false); for (int i = 0; i < onlinePlayers.length; i++) { Player p = onlinePlayers[i]; + if(p == null) continue; if (p.getWorld().getName().equals(worldName) && !(useWhitelist ^ hiddenPlayerNames.contains(p.getName().toLowerCase()))) { visiblePlayers.add(p); } @@ -90,10 +93,11 @@ public class PlayerList { public Player[] getVisiblePlayers() { ArrayList visiblePlayers = new ArrayList(); - Player[] onlinePlayers = server.getOnlinePlayers(); + Player[] onlinePlayers = online; /* Use copied list - we don't call from server thread */ boolean useWhitelist = configuration.getBoolean("display-whitelist", false); for (int i = 0; i < onlinePlayers.length; i++) { Player p = onlinePlayers[i]; + if(p == null) continue; if (!(useWhitelist ^ hiddenPlayerNames.contains(p.getName().toLowerCase()))) { visiblePlayers.add(p); } @@ -105,10 +109,11 @@ public class PlayerList { public Set getHiddenPlayers() { HashSet hidden = new HashSet(); - Player[] onlinePlayers = server.getOnlinePlayers(); + Player[] onlinePlayers = online; /* Use copied list - we don't call from server thread */ boolean useWhitelist = configuration.getBoolean("display-whitelist", false); for (int i = 0; i < onlinePlayers.length; i++) { Player p = onlinePlayers[i]; + if(p == null) continue; if (useWhitelist ^ hiddenPlayerNames.contains(p.getName().toLowerCase())) { hidden.add(p); } @@ -120,4 +125,19 @@ public class PlayerList { boolean useWhitelist = configuration.getBoolean("display-whitelist", false); return (!(useWhitelist ^ hiddenPlayerNames.contains(p.getName().toLowerCase()))); } + + /** + * Call this from server thread to update player list safely + */ + void updateOnlinePlayers(Player skipone) { + Player[] players = server.getOnlinePlayers(); + Player[] pl = new Player[players.length]; + System.arraycopy(players, 0, pl, 0, pl.length); + if(skipone != null) { + for(int i = 0; i < pl.length; i++) + if(pl[i] == skipone) + pl[i] = null; + } + online = pl; + } }