diff --git a/src/main/java/com/graywolf336/jail/beans/Jail.java b/src/main/java/com/graywolf336/jail/beans/Jail.java index 551d0a4..9e86edc 100644 --- a/src/main/java/com/graywolf336/jail/beans/Jail.java +++ b/src/main/java/com/graywolf336/jail/beans/Jail.java @@ -6,6 +6,7 @@ import java.util.HashSet; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.util.Vector; @@ -152,8 +153,26 @@ public class Jail { return this.cells.get(name); } + /** Checks if the given name is a valid cell. */ + public boolean isValidCell(String name) { + return this.cells.get(name) != null; + } + /** Removes the cell from the jail. */ public void removeCell(String name) { + //Clear the chest and reset the sign + this.cells.get(name).getChest().getInventory().clear(); + + //For each sign, clear the lines on the sign + for(SimpleLocation s : this.cells.get(name).getSigns()) { + if(s.getLocation().getBlock().getState() instanceof Sign) { + Sign sign = (Sign) s.getLocation().getBlock(); + for(int i = 0; i < 4; i++) { + sign.setLine(i, ""); + } + } + } + this.cells.remove(name); } @@ -188,6 +207,29 @@ public class Jail { return new HashSet(this.cells.values()); } + /** Gets the closest cell to the provided location, via the teleport in location of the cells. */ + public Cell getNearestCell(Location loc) { + Cell cell = null; + double distance = -1; + + for(Cell c : getCells()) { + //Check if the worlds are the same, if not we can't calculate anything + if(c.getTeleport().getWorld().getName().equalsIgnoreCase(loc.getWorld().getName())) { + //They are in the same world + double dist = c.getTeleport().distance(loc); + if (dist < distance || distance < 0) { + cell = c; + distance = dist; + } + }else { + //If they aren't, return the first cell found. + return c; + } + } + + return cell; + } + /** Clears all the prisoners from this Jail. */ public void clearPrisoners() { //Remove the prisoners from all the cells diff --git a/src/main/java/com/graywolf336/jail/command/JailHandler.java b/src/main/java/com/graywolf336/jail/command/JailHandler.java index db5330f..1671c2a 100644 --- a/src/main/java/com/graywolf336/jail/command/JailHandler.java +++ b/src/main/java/com/graywolf336/jail/command/JailHandler.java @@ -16,6 +16,8 @@ import com.graywolf336.jail.command.subcommands.JailClearCommand; import com.graywolf336.jail.command.subcommands.JailClearForceCommand; import com.graywolf336.jail.command.subcommands.JailCommand; import com.graywolf336.jail.command.subcommands.JailCreateCommand; +import com.graywolf336.jail.command.subcommands.JailDeleteCellCommand; +import com.graywolf336.jail.command.subcommands.JailDeleteCellsCommand; import com.graywolf336.jail.command.subcommands.JailListCellsCommand; import com.graywolf336.jail.command.subcommands.JailListCommand; import com.graywolf336.jail.command.subcommands.JailMuteCommand; @@ -167,6 +169,8 @@ public class JailHandler { load(JailClearForceCommand.class); load(JailCommand.class); load(JailCreateCommand.class); + load(JailDeleteCellCommand.class); + load(JailDeleteCellsCommand.class); load(JailListCellsCommand.class); load(JailListCommand.class); load(JailMuteCommand.class); diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellCommand.java new file mode 100644 index 0000000..88250bb --- /dev/null +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellCommand.java @@ -0,0 +1,46 @@ +package com.graywolf336.jail.command.subcommands; + +import org.bukkit.command.CommandSender; + +import com.graywolf336.jail.JailManager; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.command.Command; +import com.graywolf336.jail.command.CommandInfo; +import com.graywolf336.jail.enums.LangString; + +@CommandInfo( + maxArgs = 2, + minimumArgs = 2, + needsPlayer = false, + pattern = "deletecell|dc", + permission = "jail.command.jaildeletecell", + usage = "/jail deletecell " + ) +public class JailDeleteCellCommand implements Command { + public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { + //Check if the jail name provided is a valid jail + if(jm.isValidJail(args[1])) { + Jail j = jm.getJail(args[1]); + + //check if the cell is a valid cell + if(j.isValidCell(args[2])) { + if(j.getCell(args[2]).hasPrisoner()) { + //The cell has a prisoner, so tell them to first transfer the prisoner + //or release the prisoner + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLREMOVALUNSUCCESSFUL, new String[] { args[2], args[1] })); + }else { + j.removeCell(args[2]); + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLREMOVED, new String[] { args[2], args[1] })); + } + }else { + //No cell found by the provided name in the stated jail + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELL, new String[] { args[2], args[1] })); + } + }else { + //No jail found by the provided name + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, args[1])); + } + + return true; + } +} diff --git a/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellsCommand.java b/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellsCommand.java new file mode 100644 index 0000000..ffbbcde --- /dev/null +++ b/src/main/java/com/graywolf336/jail/command/subcommands/JailDeleteCellsCommand.java @@ -0,0 +1,53 @@ +package com.graywolf336.jail.command.subcommands; + +import java.util.HashSet; + +import org.bukkit.command.CommandSender; + +import com.graywolf336.jail.JailManager; +import com.graywolf336.jail.beans.Cell; +import com.graywolf336.jail.beans.Jail; +import com.graywolf336.jail.command.Command; +import com.graywolf336.jail.command.CommandInfo; +import com.graywolf336.jail.enums.LangString; + +@CommandInfo( + maxArgs = 1, + minimumArgs = 1, + needsPlayer = false, + pattern = "deletecells|dcs", + permission = "jail.command.jaildeletecell", + usage = "/jail deletecells " + ) +public class JailDeleteCellsCommand implements Command { + public boolean execute(JailManager jm, CommandSender sender, String... args) throws Exception { + //Check if the jail name provided is a valid jail + if(jm.isValidJail(args[1])) { + Jail j = jm.getJail(args[1]); + + if(j.getCellCount() == 0) { + //There are no cells in this jail, thus we can't delete them. + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOCELLS, args[1])); + }else { + //Keep a local copy of the hashset so that we don't get any CMEs. + HashSet cells = new HashSet(j.getCells()); + + for(Cell c : cells) { + if(c.hasPrisoner()) { + //The cell has a prisoner, so tell them to first transfer the prisoner + //or release the prisoner + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLREMOVALUNSUCCESSFUL, new String[] { c.getName(), args[1] })); + }else { + j.removeCell(c.getName()); + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.CELLREMOVED, new String[] { args[2], args[1] })); + } + } + } + }else { + //No jail found by the provided name + sender.sendMessage(jm.getPlugin().getJailIO().getLanguageString(LangString.NOJAIL, args[1])); + } + + return true; + } +} diff --git a/src/main/java/com/graywolf336/jail/enums/LangString.java b/src/main/java/com/graywolf336/jail/enums/LangString.java index 49add94..0d75462 100644 --- a/src/main/java/com/graywolf336/jail/enums/LangString.java +++ b/src/main/java/com/graywolf336/jail/enums/LangString.java @@ -106,6 +106,8 @@ public enum LangString { /** Part message of any messages which require 'all the jails' or such. */ ALLJAILS ("general"), + /** The message sent to the sender whenever they try to remove a cell but was unsuccessful due to a prisoner. */ + CELLREMOVALUNSUCCESSFUL ("general"), /** The message sent whenever a cell is successfully removed. */ CELLREMOVED ("general"), /** The simple word jailing to be put in other parts. */ diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index 10fbb80..ce4fdf1 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -9,6 +9,7 @@ language: moving: 'trying to escape' general: alljails: 'all the jails' + cellremovalunsuccessful: '&cThe removal of cell %0% from jail %1% was unsuccessful because there is a prisoner in there still. Release or transfer before trying to remove the cell again.' cellremoved: '&9Cell %0% has been successfully removed from the jail %1%.' jailing: '&9jailing' jailremoved: '&9Jail %0% has been successfully removed.'