From 0870a3abe25798c05d65d664f447a8f52b1d1224 Mon Sep 17 00:00:00 2001 From: Zrips Date: Sat, 14 Jan 2017 14:05:32 +0200 Subject: [PATCH] Optimize for explorer loading --- com/gamingmesh/jobs/Jobs.java | 2 +- com/gamingmesh/jobs/PlayerManager.java | 21 +- com/gamingmesh/jobs/config/.gitignore | 1 + .../jobs/config/ExploreManager.java | 197 ++++++++++-------- .../jobs/container/ExploreRegion.java | 51 ++--- com/gamingmesh/jobs/dao/JobsDAO.java | 10 +- 6 files changed, 156 insertions(+), 126 deletions(-) diff --git a/com/gamingmesh/jobs/Jobs.java b/com/gamingmesh/jobs/Jobs.java index d381a9d5..ab6d68db 100644 --- a/com/gamingmesh/jobs/Jobs.java +++ b/com/gamingmesh/jobs/Jobs.java @@ -737,7 +737,7 @@ public class Jobs extends JavaPlugin { // all loaded properly. dao.loadBlockProtection(); - dao.loadExplore(); + getExplore().load(); String message = ChatColor.translateAlternateColorCodes('&', "&e[Jobs] Plugin has been enabled succesfully."); ConsoleCommandSender console = Bukkit.getServer().getConsoleSender(); diff --git a/com/gamingmesh/jobs/PlayerManager.java b/com/gamingmesh/jobs/PlayerManager.java index caebfdb8..c03e9894 100644 --- a/com/gamingmesh/jobs/PlayerManager.java +++ b/com/gamingmesh/jobs/PlayerManager.java @@ -97,18 +97,26 @@ public class PlayerManager { public void addPlayerToCache(JobsPlayer jPlayer) { if (jPlayer.getUserName() != null) - this.playersCache.put(jPlayer.getUserName(), jPlayer); + this.playersCache.put(jPlayer.getUserName().toLowerCase(), jPlayer); if (jPlayer.getPlayerUUID() != null) this.playersUUIDCache.put(jPlayer.getPlayerUUID(), jPlayer); } public void addPlayer(JobsPlayer jPlayer) { if (jPlayer.getUserName() != null) - this.players.put(jPlayer.getUserName(), jPlayer); + this.players.put(jPlayer.getUserName().toLowerCase(), jPlayer); if (jPlayer.getPlayerUUID() != null) this.playersUUID.put(jPlayer.getPlayerUUID(), jPlayer); } + public JobsPlayer removePlayer(Player player) { + if (player == null) + return null; + this.players.remove(player.getName().toLowerCase()); + JobsPlayer jPlayer = this.playersUUID.remove(player.getUniqueId()); + return jPlayer; + } + public ConcurrentHashMap getPlayersCache() { return this.playersUUIDCache; } @@ -148,14 +156,13 @@ public class PlayerManager { * @param playername */ public void playerJoin(Player player) { - JobsPlayer jPlayer = this.playersCache.get(player.getName().toLowerCase()); + JobsPlayer jPlayer = this.playersUUIDCache.get(player.getUniqueId()); if (jPlayer == null || Jobs.getGCManager().MultiServerCompatability()) { jPlayer = Jobs.getJobsDAO().loadFromDao(player); jPlayer.loadLogFromDao(); - this.playersCache.put(player.getName().toLowerCase(), jPlayer); } - this.players.put(player.getName().toLowerCase(), jPlayer); + this.addPlayer(jPlayer); jPlayer.setPlayer(player); AutoJoinJobs(player); jPlayer.onConnect(); @@ -169,12 +176,12 @@ public class PlayerManager { * @param playername */ public void playerQuit(Player player) { - JobsPlayer jPlayer = this.players.remove(player.getName().toLowerCase()); + JobsPlayer jPlayer = this.removePlayer(player); if (jPlayer == null) jPlayer = Jobs.getPlayerManager().getJobsPlayer(player); if (jPlayer == null) return; - playersCache.put(player.getName().toLowerCase(), jPlayer); + addPlayerToCache(jPlayer); if (Jobs.getGCManager().saveOnDisconnect()) { jPlayer.save(); jPlayer.onDisconnect(); diff --git a/com/gamingmesh/jobs/config/.gitignore b/com/gamingmesh/jobs/config/.gitignore index c3e2623e..fee6760e 100644 --- a/com/gamingmesh/jobs/config/.gitignore +++ b/com/gamingmesh/jobs/config/.gitignore @@ -31,3 +31,4 @@ /BossBarManager.class /BlockProtectionManager.class /ScheduleManager$3.class +/ExploreManager$1.class diff --git a/com/gamingmesh/jobs/config/ExploreManager.java b/com/gamingmesh/jobs/config/ExploreManager.java index ab5fb46d..7980c9bd 100644 --- a/com/gamingmesh/jobs/config/ExploreManager.java +++ b/com/gamingmesh/jobs/config/ExploreManager.java @@ -1,89 +1,108 @@ -package com.gamingmesh.jobs.config; - -import java.util.HashMap; - -import org.bukkit.Chunk; -import org.bukkit.entity.Player; - -import com.gamingmesh.jobs.Jobs; -import com.gamingmesh.jobs.container.ExploreChunk; -import com.gamingmesh.jobs.container.ExploreRegion; -import com.gamingmesh.jobs.container.ExploreRespond; - -public class ExploreManager { - - private HashMap worlds = new HashMap(); - private boolean exploreEnabled = false; - private int playerAmount = 1; - - public ExploreManager() { - } - - public int getPlayerAmount() { - return this.playerAmount; - } - - public void setPlayerAmount(int amount) { - if (this.playerAmount < amount) - this.playerAmount = amount; - } - - public boolean isExploreEnabled() { - return this.exploreEnabled; - } - - public void setExploreEnabled() { - if (!exploreEnabled) { - this.exploreEnabled = true; - Jobs.getJobsDAO().loadExplore(); - } - } - - public HashMap getWorlds() { - return worlds; - } - - public ExploreRespond ChunkRespond(Player player, Chunk chunk, boolean isNew) { - return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), isNew); - } - - public ExploreRespond ChunkRespond(String player, String worldName, int x, int z, boolean isNew) { - - int ChunkX = x; - int ChunkZ = z; - - int RegionX = (int) Math.floor(ChunkX / 32D); - int RegionZ = (int) Math.floor(ChunkZ / 32D); - - if (!worlds.containsKey(worldName)) { - ExploreChunk eChunk = new ExploreChunk(player, ChunkX, ChunkZ); - if (!isNew) - eChunk.setOldChunk(); - ExploreRegion eRegion = new ExploreRegion(RegionX, RegionZ); - eRegion.addChunk(eChunk); - worlds.put(worldName, eRegion); - return new ExploreRespond(eChunk.getCount(), true); - } - ExploreRegion eRegion = worlds.get(worldName); - ExploreChunk eChunk = null; - for (ExploreChunk one : eRegion.getChunks()) { - if (one.getX() != ChunkX) - continue; - if (one.getZ() != ChunkZ) - continue; - eChunk = one; - if (!isNew) - eChunk.setOldChunk(); - break; - } - - if (eChunk == null) { - eChunk = new ExploreChunk(player, ChunkX, ChunkZ); - if (!isNew) - eChunk.setOldChunk(); - eRegion.addChunk(eChunk); - return new ExploreRespond(eChunk.getCount(), true); - } - return eChunk.addPlayer(player); - } -} +package com.gamingmesh.jobs.config; + +import java.util.HashMap; + +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Chunk; +import org.bukkit.entity.Player; + +import com.gamingmesh.jobs.Jobs; +import com.gamingmesh.jobs.container.ExploreChunk; +import com.gamingmesh.jobs.container.ExploreRegion; +import com.gamingmesh.jobs.container.ExploreRespond; + +public class ExploreManager { + + private HashMap worlds = new HashMap(); + private boolean exploreEnabled = false; + private int playerAmount = 1; + + public int getPlayerAmount() { + return this.playerAmount; + } + + public void setPlayerAmount(int amount) { + if (this.playerAmount < amount) + this.playerAmount = amount; + } + + public boolean isExploreEnabled() { + return this.exploreEnabled; + } + + public void setExploreEnabled() { + if (!exploreEnabled) { + this.exploreEnabled = true; + } + } + + public void load() { + if (!exploreEnabled) + return; + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loading explorer data"); + Jobs.getJobsDAO().loadExplore(); + Bukkit.getConsoleSender().sendMessage(ChatColor.YELLOW + "[Jobs] Loaded explorer data"); + } + + public HashMap getWorlds() { + return worlds; + } + + public ExploreRespond ChunkRespond(Player player, Chunk chunk, boolean isNew) { + return ChunkRespond(player.getName(), chunk.getWorld().getName(), chunk.getX(), chunk.getZ(), isNew); + } + + public ExploreRespond ChunkRespond(String player, String worldName, int x, int z, boolean isNew) { + + int ChunkX = x; + int ChunkZ = z; + + int RegionX = (int) Math.floor(ChunkX / 32D); + int RegionZ = (int) Math.floor(ChunkZ / 32D); + + if (!worlds.containsKey(worldName)) { + ExploreChunk eChunk = new ExploreChunk(player, ChunkX, ChunkZ); + if (!isNew) + eChunk.setOldChunk(); + ExploreRegion eRegion = new ExploreRegion(RegionX, RegionZ); + eRegion.addChunk(eChunk); + worlds.put(worldName, eRegion); + return new ExploreRespond(eChunk.getCount(), true); + } + ExploreRegion eRegion = worlds.get(worldName); + ExploreChunk eChunk = eRegion.getChunk(ChunkX + ":" + ChunkZ); + + if (eChunk == null) { + eChunk = new ExploreChunk(player, ChunkX, ChunkZ); + if (!isNew) + eChunk.setOldChunk(); + eRegion.addChunk(eChunk); + return new ExploreRespond(eChunk.getCount(), true); + } + eChunk.setOldChunk(); + return eChunk.addPlayer(player); + } + + public void addChunk(String player, String worldName, int x, int z) { + int ChunkX = x; + int ChunkZ = z; + int RegionX = (int) Math.floor(ChunkX / 32D); + int RegionZ = (int) Math.floor(ChunkZ / 32D); + if (!worlds.containsKey(worldName)) { + ExploreChunk eChunk = new ExploreChunk(player, ChunkX, ChunkZ); + eChunk.setOldChunk(); + ExploreRegion eRegion = new ExploreRegion(RegionX, RegionZ); + eRegion.addChunk(eChunk); + worlds.put(worldName, eRegion); + } + ExploreRegion eRegion = worlds.get(worldName); + ExploreChunk eChunk = eRegion.getChunk(ChunkX + ":" + ChunkZ); + if (eChunk == null) { + eChunk = new ExploreChunk(player, ChunkX, ChunkZ); + eChunk.setOldChunk(); + eRegion.addChunk(eChunk); + } else + eChunk.setOldChunk(); + } +} diff --git a/com/gamingmesh/jobs/container/ExploreRegion.java b/com/gamingmesh/jobs/container/ExploreRegion.java index dee31541..a653356f 100644 --- a/com/gamingmesh/jobs/container/ExploreRegion.java +++ b/com/gamingmesh/jobs/container/ExploreRegion.java @@ -1,24 +1,27 @@ -package com.gamingmesh.jobs.container; - -import java.util.ArrayList; -import java.util.List; - -public class ExploreRegion { - - int x; - int z; - List chunks = new ArrayList(); - - public ExploreRegion(int x, int z) { - this.x = x; - this.z = z; - } - - public void addChunk(ExploreChunk chunk) { - chunks.add(chunk); - } - - public List getChunks() { - return chunks; - } -} +package com.gamingmesh.jobs.container; + +import java.util.HashMap; + +public class ExploreRegion { + + int x; + int z; + HashMap chunks = new HashMap(); + + public ExploreRegion(int x, int z) { + this.x = x; + this.z = z; + } + + public void addChunk(ExploreChunk chunk) { + chunks.put(chunk.getX() + ":" + chunk.getZ(), chunk); + } + + public HashMap getChunks() { + return chunks; + } + + public ExploreChunk getChunk(String cord) { + return chunks.get(cord); + } +} diff --git a/com/gamingmesh/jobs/dao/JobsDAO.java b/com/gamingmesh/jobs/dao/JobsDAO.java index e74f38a1..7a60c14a 100644 --- a/com/gamingmesh/jobs/dao/JobsDAO.java +++ b/com/gamingmesh/jobs/dao/JobsDAO.java @@ -1310,13 +1310,13 @@ public abstract class JobsDAO { HashMap temp = new HashMap(Jobs.getExplore().getWorlds()); for (Entry worlds : temp.entrySet()) { - for (ExploreChunk oneChunk : worlds.getValue().getChunks()) { - if (!oneChunk.isNew()) + for (Entry oneChunk : worlds.getValue().getChunks().entrySet()) { + if (!oneChunk.getValue().isNew()) continue; - for (String oneuser : oneChunk.getPlayers()) { + for (String oneuser : oneChunk.getValue().getPlayers()) { prest2.setString(1, worlds.getKey()); - prest2.setInt(2, oneChunk.getX()); - prest2.setInt(3, oneChunk.getZ()); + prest2.setInt(2, oneChunk.getValue().getX()); + prest2.setInt(3, oneChunk.getValue().getZ()); prest2.setString(4, oneuser); prest2.addBatch(); i++;