From 3e54d79dd3181294489beac8a14d14f03c7fe110 Mon Sep 17 00:00:00 2001 From: graywolf336 Date: Thu, 8 May 2014 19:30:20 -0500 Subject: [PATCH] Try to lower the amount of time spent on each move event as mentioned in issue #26 --- .../java/com/graywolf336/jail/JailIO.java | 4 +- .../java/com/graywolf336/jail/JailMain.java | 2 +- .../com/graywolf336/jail/JailManager.java | 14 +++---- .../java/com/graywolf336/jail/JailTimer.java | 2 +- .../graywolf336/jail/ScoreBoardManager.java | 2 +- .../java/com/graywolf336/jail/beans/Jail.java | 39 +++++++++---------- .../command/subcommands/JailListCommand.java | 6 +-- .../subcommands/JailTransferAllCommand.java | 2 +- .../listeners/MoveProtectionListener.java | 5 ++- .../jail/listeners/PlayerListener.java | 2 +- 10 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/main/java/com/graywolf336/jail/JailIO.java b/src/main/java/com/graywolf336/jail/JailIO.java index cf2d24b..b82de83 100644 --- a/src/main/java/com/graywolf336/jail/JailIO.java +++ b/src/main/java/com/graywolf336/jail/JailIO.java @@ -742,7 +742,7 @@ public class JailIO { try { if(con == null) this.prepareStorage(false); - for(Prisoner p : j.getPrisonersNotInCells()) { + for(Prisoner p : j.getPrisonersNotInCells().values()) { PreparedStatement pPS = con.prepareStatement("REPLACE INTO `" + prefix + "prisoners` (`uuid`, `name`, `jail`, `cell`, `muted`, `time`," + "`offlinePending`, `toBeTransferred`, `jailer`, `reason`, `inventory`, `armor`, `previousLocation`, `previousGameMode`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); pPS.setString(1, p.getUUID().toString()); @@ -848,7 +848,7 @@ public class JailIO { //Null all the prisoners out before we save them again, this way no prisoners are left behind flat.set(node + "prisoners", null); - for(Prisoner p : j.getPrisonersNotInCells()) { + for(Prisoner p : j.getPrisonersNotInCells().values()) { String pNode = node + "prisoners." + p.getUUID().toString() + "."; flat.set(pNode + "name", p.getLastKnownName()); flat.set(pNode + "muted", p.isMuted()); diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 8834a39..5559e18 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -171,7 +171,7 @@ public class JailMain extends JavaPlugin { if(getConfig().getBoolean(Settings.SCOREBOARDENABLED.getPath())) { for(Jail j : jm.getJails()) { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : j.getAllPrisoners().values()) { if(getServer().getPlayer(p.getUUID()) != null) { this.sbm.addScoreBoard(getServer().getPlayer(p.getUUID()), p); } diff --git a/src/main/java/com/graywolf336/jail/JailManager.java b/src/main/java/com/graywolf336/jail/JailManager.java index 005278d..27dfb59 100644 --- a/src/main/java/com/graywolf336/jail/JailManager.java +++ b/src/main/java/com/graywolf336/jail/JailManager.java @@ -174,11 +174,11 @@ public class JailManager { * * @return HashSet of Prisoners. */ - public HashSet getAllPrisoners() { - HashSet prisoners = new HashSet(); + public HashMap getAllPrisoners() { + HashMap prisoners = new HashMap(); for(Jail j : jails.values()) - prisoners.addAll(j.getAllPrisoners()); + prisoners.putAll(j.getAllPrisoners()); return prisoners; } @@ -243,7 +243,7 @@ public class JailManager { */ public Jail getJailPlayerIsInByLastKnownName(String username) { for(Jail j : jails.values()) - for(Prisoner p : j.getAllPrisoners()) + for(Prisoner p : j.getAllPrisoners().values()) if(p.getLastKnownName().equalsIgnoreCase(username)) return j; @@ -257,7 +257,7 @@ public class JailManager { * @return {@link Prisoner prisoner} data */ public Prisoner getPrisonerByLastKnownName(String username) { - for(Prisoner p : this.getAllPrisoners()) + for(Prisoner p : this.getAllPrisoners().values()) if(p.getLastKnownName().equalsIgnoreCase(username)) return p; @@ -286,7 +286,7 @@ public class JailManager { Jail j = getJail(jail); if(j != null) { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : j.getAllPrisoners().values()) { getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayer(p.getUUID()), p); } @@ -310,7 +310,7 @@ public class JailManager { return getPlugin().getJailIO().getLanguageString(LangString.NOJAILS); }else { for(Jail j : getJails()) { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : j.getAllPrisoners().values()) { getPlugin().getPrisonerManager().releasePrisoner(getPlugin().getServer().getPlayer(p.getUUID()), p); } } diff --git a/src/main/java/com/graywolf336/jail/JailTimer.java b/src/main/java/com/graywolf336/jail/JailTimer.java index 9ae4503..a226f96 100644 --- a/src/main/java/com/graywolf336/jail/JailTimer.java +++ b/src/main/java/com/graywolf336/jail/JailTimer.java @@ -66,7 +66,7 @@ public class JailTimer { } for(Jail j : pl.getJailManager().getJails()) { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : j.getAllPrisoners().values()) { //only execute this code if the prisoner's time is more than 0 milliseconds if(p.getRemainingTime() > 0) { Player player = pl.getServer().getPlayer(p.getUUID()); diff --git a/src/main/java/com/graywolf336/jail/ScoreBoardManager.java b/src/main/java/com/graywolf336/jail/ScoreBoardManager.java index 1ae541e..2559963 100644 --- a/src/main/java/com/graywolf336/jail/ScoreBoardManager.java +++ b/src/main/java/com/graywolf336/jail/ScoreBoardManager.java @@ -84,7 +84,7 @@ public class ScoreBoardManager { /** Updates the prisoners time on their scoreboard. */ private void updatePrisonersTime() { for(Jail j : pl.getJailManager().getJails()) { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : j.getAllPrisoners().values()) { if(pl.getServer().getPlayer(p.getUUID()) != null) { addScoreBoard(pl.getServer().getPlayer(p.getUUID()), p); } diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index ae2a749..487071f 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -23,7 +23,7 @@ import com.graywolf336.jail.Util; public class Jail { private JailMain plugin; private HashMap cells; - private HashSet nocellPrisoners;//prisoners who aren't in a cell + private HashMap nocellPrisoners;//prisoners who aren't in a cell private String name = "", world = ""; private int minX, minY, minZ, maxX, maxY, maxZ; private SimpleLocation in, free; @@ -32,7 +32,7 @@ public class Jail { this.plugin = plugin; this.name = name; cells = new HashMap(); - nocellPrisoners = new HashSet(); + nocellPrisoners = new HashMap(); } /** Gets the instance of the plugin's main class. */ @@ -135,7 +135,7 @@ public class Jail { /** Add a prisoner to this jail. */ public void addPrisoner(Prisoner p) { - this.nocellPrisoners.add(p); + this.nocellPrisoners.put(p.getUUID(), p); } /** Removes a prisoner from this jail, doesn't remove it from the cell. */ @@ -246,16 +246,16 @@ public class Jail { //Replace all the current no cell prisoners with //a new hashset of prisoners. - this.nocellPrisoners = new HashSet(); + this.nocellPrisoners = new HashMap(); } - /** Gets a HashSet of all the prisoners, the ones in cells and ones who aren't. */ - public HashSet getAllPrisoners() { - HashSet all = new HashSet(nocellPrisoners); //initalize the temp one to return with the prisoners not in any cells + /** Gets a HashMap of all the prisoners, the ones in cells and ones who aren't. */ + public HashMap getAllPrisoners() { + HashMap all = new HashMap(nocellPrisoners); //initalize the temp one to return with the prisoners not in any cells for(Cell c : cells.values()) if(c.hasPrisoner()) - all.add(c.getPrisoner()); + all.put(c.getPrisoner().getUUID(), c.getPrisoner()); return all; } @@ -272,7 +272,7 @@ public class Jail { } /** Gets a HashSet of the prisoners not in cells.*/ - public HashSet getPrisonersNotInCells() { + public HashMap getPrisonersNotInCells() { return this.nocellPrisoners; } @@ -303,11 +303,7 @@ public class Jail { * @return true if is a prisoner, false if not. */ private boolean isPlayerAPrisoner(UUID uuid) { - for(Prisoner p : this.getAllPrisoners()) - if(p.getUUID().equals(uuid)) - return true; - - return false; + return this.getAllPrisoners().containsKey(uuid); } /** @@ -317,9 +313,7 @@ public class Jail { * @return true if is jailed in a cell, false if not. */ public boolean isJailedInACell(UUID uuid) { - for(Prisoner p : nocellPrisoners) - if(p.getUUID().equals(uuid)) - return false; + if(this.nocellPrisoners.containsKey(uuid)) return false; for(Cell c : cells.values()) if(c.getPrisoner() != null) @@ -336,7 +330,7 @@ public class Jail { * @return the prisoner instance, can be null */ public Prisoner getPrisonerByLastKnownName(String name) { - for(Prisoner p : this.getAllPrisoners()) + for(Prisoner p : this.getAllPrisoners().values()) if(p.getLastKnownName().equalsIgnoreCase(name)) return p; @@ -350,9 +344,12 @@ public class Jail { * @return the prisoner instance, can be null */ public Prisoner getPrisoner(UUID uuid) { - for(Prisoner p : this.getAllPrisoners()) - if(p.getUUID().equals(uuid)) - return p; + if(this.nocellPrisoners.containsKey(uuid)) return this.nocellPrisoners.get(uuid); + + for(Cell c : cells.values()) + if(c.hasPrisoner()) + if(c.getPrisoner().getUUID().equals(uuid)) + return c.getPrisoner(); return null; } diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailListCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailListCommand.java index 04e14e0..b1d33e8 100644 --- a/src/main/java/com/graywolf336/jail/command/subcommands/JailListCommand.java +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailListCommand.java @@ -1,6 +1,6 @@ package com.graywolf336.jail.command.subcommands; -import java.util.HashSet; +import java.util.Collection; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; @@ -41,13 +41,13 @@ public class JailListCommand implements Command { //No jail was found sender.sendMessage(" " + jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, args[1])); }else { - HashSet pris = j.getAllPrisoners(); + Collection pris = j.getAllPrisoners().values(); if(pris.isEmpty()) { //If there are no prisoners, then send that message sender.sendMessage(" " + jm.getPlugin().getJailIO().getLanguageString(LangString.NOPRISONERS, j.getName())); }else { - for(Prisoner p : j.getAllPrisoners()) { + for(Prisoner p : pris) { //graywolf663: Being gray's evil twin; CONSOLE (10) //prisoner: reason; jailer (time in minutes) sender.sendMessage(ChatColor.BLUE + " " + p.getLastKnownName() + ": " + p.getReason() + "; " + p.getJailer() + " (" + p.getRemainingTimeInMinutes() + " mins)"); diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferAllCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferAllCommand.java index e34f43f..81fddca 100644 --- a/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferAllCommand.java +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailTransferAllCommand.java @@ -41,7 +41,7 @@ public class JailTransferAllCommand implements Command { jm.getPlugin().debug("Sending the transferring off, jail checks all came clean."); Jail old = jm.getJail(args[1]); - HashSet oldPrisoners = new HashSet(old.getAllPrisoners()); + HashSet oldPrisoners = new HashSet(old.getAllPrisoners().values()); //Transfer all the prisoners for(Prisoner p : oldPrisoners) { diff --git a/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java index 5c8f76a..2f3fd35 100644 --- a/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java @@ -27,8 +27,9 @@ public class MoveProtectionListener implements Listener { //Other wise we don't need to deal with it. if(pl.getConfig().getBoolean(Settings.MOVEPROTECTION.getPath())) { //Let's be sure the player we're dealing with is in jail - if(pl.getJailManager().isPlayerJailed(event.getPlayer().getUniqueId())) { - Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getUniqueId()); + + Jail j = pl.getJailManager().getJailPlayerIsIn(event.getPlayer().getUniqueId()); + if(j != null) { Prisoner p = j.getPrisoner(event.getPlayer().getUniqueId()); //If the player is being teleported, let's ignore it diff --git a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java index 7c33f8f..9aa9a97 100644 --- a/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/PlayerListener.java @@ -76,7 +76,7 @@ public class PlayerListener implements Listener { Set rec = new HashSet(event.getRecipients()); for(Jail j : pl.getJailManager().getJails()) - for(Prisoner p : j.getAllPrisoners()) + for(Prisoner p : j.getAllPrisoners().values()) rec.remove(pl.getServer().getPlayer(p.getUUID())); event.getRecipients().clear();