diff --git a/src/main/java/com/graywolf336/jail/JailManager.java b/src/main/java/com/graywolf336/jail/JailManager.java index b6bc633..f9923c9 100644 --- a/src/main/java/com/graywolf336/jail/JailManager.java +++ b/src/main/java/com/graywolf336/jail/JailManager.java @@ -1,657 +1,667 @@ -package com.graywolf336.jail; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.UUID; - -import org.bukkit.Location; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.graywolf336.jail.beans.CachePrisoner; -import com.graywolf336.jail.beans.Cell; -import com.graywolf336.jail.beans.ConfirmPlayer; -import com.graywolf336.jail.beans.CreationPlayer; -import com.graywolf336.jail.beans.Jail; -import com.graywolf336.jail.beans.Prisoner; -import com.graywolf336.jail.enums.Confirmation; -import com.graywolf336.jail.enums.LangString; -import com.graywolf336.jail.steps.CellCreationSteps; -import com.graywolf336.jail.steps.JailCreationSteps; - -/** - * Handles all things related to jails. - * - *
- * - * Stores the following: - *
- * - * If you want to check to see if they're just creating a jail then use {@link #isCreatingAJail(String) isCreatingAJail} or if you want to see if they're creating a cell then use {@link #isCreatingACell(String) isCreatingACell}. - * - * @param name The name of the player, in any case as we convert it to lowercase. - * @return True if the player is creating a jail or cell, false if they're not creating anything. - */ - public boolean isCreatingSomething(String name) { - return this.jailCreators.containsKey(name.toLowerCase()) || this.cellCreators.containsKey(name.toLowerCase()); - } - - /** Returns a message used for telling them what they're creating and what step they're on. */ - public String getStepMessage(String player) { - String message = ""; - - if(isCreatingACell(player)) {//Check whether it is a jail cell - CreationPlayer cp = this.getCellCreationPlayer(player); - message = "You're already creating a Cell with the name '" + cp.getCellName() + "' and you still need to "; - - switch(cp.getStep()) { - case 1: - message += "set the teleport in location."; - break; - case 2: - message += "select all the signs."; - break; - case 3: - message += "set the double chest location."; - break; - } - - }else if(isCreatingAJail(player)) {//If not a cell, then check if a jail. - CreationPlayer cp = this.getJailCreationPlayer(player); - message = "You're already creating a Jail with the name '" + cp.getJailName() + "' and you still need to "; - - switch(cp.getStep()) { - case 1: - message += "select the first point."; - break; - case 2: - message += "select the second point."; - break; - case 3: - message += "set the teleport in location."; - break; - case 4: - message += "set the release location."; - break; - } - } - - return message; - } - - /** Returns whether or not someone is creating a Jail. */ - public boolean isCreatingAJail(String name) { - return this.jailCreators.containsKey(name.toLowerCase()); - } - - /** - * Method for setting a player to be creating a Jail, returns whether or not they were added successfully. - * - * @param player The player who is creating a jail. - * @param jailName The name of the jail we are creating. - * @return True if they were added successfully, false if they are already creating a Jail. - */ - public boolean addCreatingJail(String player, String jailName) { - if(isCreatingAJail(player)) { - return false; - }else { - this.jailCreators.put(player.toLowerCase(), new CreationPlayer(jailName)); - return true; - } - } - - /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ - public CreationPlayer getJailCreationPlayer(String name) { - return this.jailCreators.get(name.toLowerCase()); - } - - /** Removes a CreationPlayer with the given name from the jail creators. */ - public void removeJailCreationPlayer(String name) { - this.jailCreators.remove(name.toLowerCase()); - } - - /** Returns whether or not someone is creating a Cell. */ - public boolean isCreatingACell(String name) { - return this.cellCreators.containsKey(name.toLowerCase()); - } - - /** - * Method for setting a player to be creating a Cell, returns whether or not they were added successfully. - * - * @param player The player who is creating a jail. - * @param jailName The name of the jail this cell is going. - * @param cellName The name of the cell we are creating. - * @return True if they were added successfully, false if they are already creating a Jail. - */ - public boolean addCreatingCell(String player, String jailName, String cellName) { - if(isCreatingACell(player)) { - return false; - }else { - this.cellCreators.put(player.toLowerCase(), new CreationPlayer(jailName, cellName)); - return true; - } - } - - /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ - public CreationPlayer getCellCreationPlayer(String name) { - return this.cellCreators.get(name.toLowerCase()); - } - - /** Removes a CreationPlayer with the given name from the cell creators. */ - public void removeCellCreationPlayer(String name) { - this.cellCreators.remove(name.toLowerCase()); - } - - /** Gets the instance of the {@link JailCreationSteps}. */ - public JailCreationSteps getJailCreationSteps() { - return this.jcs; - } - - /** Gets the instance of the {@link CellCreationSteps}. */ - public CellCreationSteps getCellCreationSteps() { - return this.ccs; - } - - /** Adds something to the confirming list. */ - public void addConfirming(String name, ConfirmPlayer confirmer) { - getPlugin().debug("Adding a confirming for " + name + " to confirm " + confirmer.getConfirming().toString().toLowerCase()); - this.confirms.put(name, confirmer); - } - - /** Removes a name from the confirming list. */ - public void removeConfirming(String name) { - this.confirms.remove(name); - } - - /** Checks if the given name is confirming something. */ - public boolean isConfirming(String name) { - if(this.confirms.containsKey(name)) { - if(this.confirmingHasExpired(name)) { - getPlugin().debug("Removing " + name + "'s confirmation as it expired."); - this.removeConfirming(name); - } - - return this.confirms.containsKey(name); - }else { - return false; - } - } - - /** Returns true if the confirmation has expired, false if it is still valid. */ - public boolean confirmingHasExpired(String name) { - return this.confirms.get(name).getExpiryTime() >= (System.currentTimeMillis() + 5000L); - } - - /** Returns the original arguments for what we are confirming. */ - public String[] getOriginalArgs(String name) { - return this.confirms.get(name).getArguments(); - } - - /** Returns what the given name is confirming. */ - public Confirmation getWhatIsConfirming(String name) { - return this.confirms.get(name).getConfirming(); - } -} +package com.graywolf336.jail; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.UUID; + +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.graywolf336.jail.beans.CachePrisoner; +import com.graywolf336.jail.beans.Cell; +import com.graywolf336.jail.beans.ConfirmPlayer; +import com.graywolf336.jail.beans.CreationPlayer; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.enums.Confirmation; +import com.graywolf336.jail.enums.LangString; +import com.graywolf336.jail.steps.CellCreationSteps; +import com.graywolf336.jail.steps.JailCreationSteps; + +/** + * Handles all things related to jails. + * + *
+ * + * Stores the following: + *
+ * + * If you want to check to see if they're just creating a jail then use {@link #isCreatingAJail(String) isCreatingAJail} or if you want to see if they're creating a cell then use {@link #isCreatingACell(String) isCreatingACell}. + * + * @param name The name of the player, in any case as we convert it to lowercase. + * @return True if the player is creating a jail or cell, false if they're not creating anything. + */ + public boolean isCreatingSomething(String name) { + return this.jailCreators.containsKey(name.toLowerCase()) || this.cellCreators.containsKey(name.toLowerCase()); + } + + /** Returns a message used for telling them what they're creating and what step they're on. */ + public String getStepMessage(String player) { + String message = ""; + + if(isCreatingACell(player)) {//Check whether it is a jail cell + CreationPlayer cp = this.getCellCreationPlayer(player); + message = "You're already creating a Cell with the name '" + cp.getCellName() + "' and you still need to "; + + switch(cp.getStep()) { + case 1: + message += "set the teleport in location."; + break; + case 2: + message += "select all the signs."; + break; + case 3: + message += "set the double chest location."; + break; + } + + }else if(isCreatingAJail(player)) {//If not a cell, then check if a jail. + CreationPlayer cp = this.getJailCreationPlayer(player); + message = "You're already creating a Jail with the name '" + cp.getJailName() + "' and you still need to "; + + switch(cp.getStep()) { + case 1: + message += "select the first point."; + break; + case 2: + message += "select the second point."; + break; + case 3: + message += "set the teleport in location."; + break; + case 4: + message += "set the release location."; + break; + } + } + + return message; + } + + /** Returns whether or not someone is creating a Jail. */ + public boolean isCreatingAJail(String name) { + return this.jailCreators.containsKey(name.toLowerCase()); + } + + /** + * Method for setting a player to be creating a Jail, returns whether or not they were added successfully. + * + * @param player The player who is creating a jail. + * @param jailName The name of the jail we are creating. + * @return True if they were added successfully, false if they are already creating a Jail. + */ + public boolean addCreatingJail(String player, String jailName) { + if(isCreatingAJail(player)) { + return false; + }else { + this.jailCreators.put(player.toLowerCase(), new CreationPlayer(jailName)); + return true; + } + } + + /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ + public CreationPlayer getJailCreationPlayer(String name) { + return this.jailCreators.get(name.toLowerCase()); + } + + /** Removes a CreationPlayer with the given name from the jail creators. */ + public void removeJailCreationPlayer(String name) { + this.jailCreators.remove(name.toLowerCase()); + } + + /** Returns whether or not someone is creating a Cell. */ + public boolean isCreatingACell(String name) { + return this.cellCreators.containsKey(name.toLowerCase()); + } + + /** + * Method for setting a player to be creating a Cell, returns whether or not they were added successfully. + * + * @param player The player who is creating a jail. + * @param jailName The name of the jail this cell is going. + * @param cellName The name of the cell we are creating. + * @return True if they were added successfully, false if they are already creating a Jail. + */ + public boolean addCreatingCell(String player, String jailName, String cellName) { + if(isCreatingACell(player)) { + return false; + }else { + this.cellCreators.put(player.toLowerCase(), new CreationPlayer(jailName, cellName)); + return true; + } + } + + /** Returns the instance of the CreationPlayer for this player, null if there was none found. */ + public CreationPlayer getCellCreationPlayer(String name) { + return this.cellCreators.get(name.toLowerCase()); + } + + /** Removes a CreationPlayer with the given name from the cell creators. */ + public void removeCellCreationPlayer(String name) { + this.cellCreators.remove(name.toLowerCase()); + } + + /** Gets the instance of the {@link JailCreationSteps}. */ + public JailCreationSteps getJailCreationSteps() { + return this.jcs; + } + + /** Gets the instance of the {@link CellCreationSteps}. */ + public CellCreationSteps getCellCreationSteps() { + return this.ccs; + } + + /** Adds something to the confirming list. */ + public void addConfirming(String name, ConfirmPlayer confirmer) { + getPlugin().debug("Adding a confirming for " + name + " to confirm " + confirmer.getConfirming().toString().toLowerCase()); + this.confirms.put(name, confirmer); + } + + /** Removes a name from the confirming list. */ + public void removeConfirming(String name) { + this.confirms.remove(name); + } + + /** Checks if the given name is confirming something. */ + public boolean isConfirming(String name) { + if(this.confirms.containsKey(name)) { + if(this.confirmingHasExpired(name)) { + getPlugin().debug("Removing " + name + "'s confirmation as it expired."); + this.removeConfirming(name); + } + + return this.confirms.containsKey(name); + }else { + return false; + } + } + + /** Returns true if the confirmation has expired, false if it is still valid. */ + public boolean confirmingHasExpired(String name) { + return this.confirms.get(name).getExpiryTime() >= (System.currentTimeMillis() + 5000L); + } + + /** Returns the original arguments for what we are confirming. */ + public String[] getOriginalArgs(String name) { + return this.confirms.get(name).getArguments(); + } + + /** Returns what the given name is confirming. */ + public Confirmation getWhatIsConfirming(String name) { + return this.confirms.get(name).getConfirming(); + } +} diff --git a/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java index c14103c..2c68108 100644 --- a/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/MoveProtectionListener.java @@ -1,87 +1,89 @@ -package com.graywolf336.jail.listeners; - -import java.util.concurrent.TimeUnit; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerMoveEvent; -import org.bukkit.event.player.PlayerTeleportEvent; - -import com.graywolf336.jail.JailMain; -import com.graywolf336.jail.Util; -import com.graywolf336.jail.beans.Jail; -import com.graywolf336.jail.beans.Prisoner; -import com.graywolf336.jail.enums.LangString; -import com.graywolf336.jail.enums.Settings; - -public class MoveProtectionListener implements Listener { - private JailMain pl; - - public MoveProtectionListener(JailMain plugin) { - this.pl = plugin; - } - - @EventHandler(ignoreCancelled=true) - public void moveProtection(PlayerMoveEvent event) { - //If we have the move protection enabled, then let's do it. - //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 - - 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 - if(p.isTeleporting()) { - return; - } - - //They moved, so they're no longer afk - p.setAFKTime(0L); - - //If the event's to location is NOT inside the jail, then let's do some action. - //For right now, we're only going to apply the time. Later we're going to do - //the guards, but first get a beta version out. - if (!j.isInside(event.getTo())) { - try { - long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath())); - p.addTime(add); - - String msg = ""; - if(add == 0L) { - //Generate the protection message, provide the method with one argument - //which is the thing we are protecting against - msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING)); - }else { - //Generate the protection message, provide the method with two arguments - //First is the time in minutes and second is the thing we are protecting against - msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, - new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), - pl.getJailIO().getLanguageString(LangString.MOVING) }); - } - - //Send the message - event.getPlayer().sendMessage(msg); - }catch(Exception e) { - pl.getLogger().severe("Moving (escaping) outside a jail penalty time is in the wrong format, please fix."); - } - - //If the prisoner is in a cell, then let's teleport them to the cell's in location - if(j.isJailedInACell(event.getPlayer().getUniqueId())) { - event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getUniqueId()).getTeleport()); - }else { - //Otherwise let's teleport them to the in location of the jail - event.setTo(j.getTeleportIn()); - } - } - } - } - } - - @EventHandler(ignoreCancelled=true) - public void onPlayerTeleport(PlayerTeleportEvent event) { - PlayerMoveEvent move = new PlayerMoveEvent(event.getPlayer(), event.getFrom(), event.getTo()); - moveProtection(move); - } -} +package com.graywolf336.jail.listeners; + +import java.util.concurrent.TimeUnit; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerTeleportEvent; + +import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.Util; +import com.graywolf336.jail.beans.CachePrisoner; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.enums.LangString; +import com.graywolf336.jail.enums.Settings; + +public class MoveProtectionListener implements Listener { + private JailMain pl; + + public MoveProtectionListener(JailMain plugin) { + this.pl = plugin; + } + + @EventHandler(ignoreCancelled=true) + public void moveProtection(PlayerMoveEvent event) { + //If we have the move protection enabled, then let's do it. + //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 + + CachePrisoner cp = pl.getJailManager().getCacheObject(event.getPlayer().getUniqueId()); + if(cp != null) { + Jail j = cp.getJail(); + Prisoner p = cp.getPrisoner(); + + //If the player is being teleported, let's ignore it + if(p.isTeleporting()) { + return; + } + + //They moved, so they're no longer afk + p.setAFKTime(0L); + + //If the event's to location is NOT inside the jail, then let's do some action. + //For right now, we're only going to apply the time. Later we're going to do + //the guards, but first get a beta version out. + if (!j.isInside(event.getTo())) { + try { + long add = Util.getTime(pl.getConfig().getString(Settings.MOVEPENALTY.getPath())); + p.addTime(add); + + String msg = ""; + if(add == 0L) { + //Generate the protection message, provide the method with one argument + //which is the thing we are protecting against + msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGENOPENALTY, pl.getJailIO().getLanguageString(LangString.MOVING)); + }else { + //Generate the protection message, provide the method with two arguments + //First is the time in minutes and second is the thing we are protecting against + msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, + new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), + pl.getJailIO().getLanguageString(LangString.MOVING) }); + } + + //Send the message + event.getPlayer().sendMessage(msg); + }catch(Exception e) { + pl.getLogger().severe("Moving (escaping) outside a jail penalty time is in the wrong format, please fix."); + } + + //If the prisoner is in a cell, then let's teleport them to the cell's in location + if(j.isJailedInACell(event.getPlayer().getUniqueId())) { + event.setTo(j.getCellPrisonerIsIn(event.getPlayer().getUniqueId()).getTeleport()); + }else { + //Otherwise let's teleport them to the in location of the jail + event.setTo(j.getTeleportIn()); + } + } + } + } + } + + @EventHandler(ignoreCancelled=true) + public void onPlayerTeleport(PlayerTeleportEvent event) { + PlayerMoveEvent move = new PlayerMoveEvent(event.getPlayer(), event.getFrom(), event.getTo()); + moveProtection(move); + } +} diff --git a/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java b/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java index 5f2f7a2..f9756c1 100644 --- a/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java +++ b/src/test/java/test/java/com/graywolf336/jail/BenchmarkTest.java @@ -24,6 +24,7 @@ import org.powermock.modules.junit4.PowerMockRunner; import com.carrotsearch.junitbenchmarks.AbstractBenchmark; import com.carrotsearch.junitbenchmarks.BenchmarkOptions; import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.beans.CachePrisoner; import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.Prisoner; @@ -61,6 +62,7 @@ public class BenchmarkTest extends AbstractBenchmark { main.getPrisonerManager().prepareJail(main.getJailManager().getJail("testingJail"), null, null, new Prisoner(i == 555 ? use.toString() : UUID.randomUUID().toString(), "mockPlayer" + i, true, 100000L, "testJailer", "Test jailing " + i)); } + main.getJailManager().addCacheObject(new CachePrisoner(main.getJailManager().getJailPlayerIsIn(use), main.getJailManager().getPrisoner(use))); r = new Random(); }