diff --git a/src/main/java/com/graywolf336/jail/JailMain.java b/src/main/java/com/graywolf336/jail/JailMain.java index 1dc6a54..ecd33de 100644 --- a/src/main/java/com/graywolf336/jail/JailMain.java +++ b/src/main/java/com/graywolf336/jail/JailMain.java @@ -15,6 +15,7 @@ import com.graywolf336.jail.enums.Settings; import com.graywolf336.jail.legacy.LegacyManager; import com.graywolf336.jail.listeners.BlockListener; import com.graywolf336.jail.listeners.CacheListener; +import com.graywolf336.jail.listeners.CellSignListener; import com.graywolf336.jail.listeners.EntityListener; import com.graywolf336.jail.listeners.HandCuffListener; import com.graywolf336.jail.listeners.JailingListener; @@ -87,6 +88,7 @@ public class JailMain extends JavaPlugin { PluginManager plm = this.getServer().getPluginManager(); plm.registerEvents(new BlockListener(this), this); plm.registerEvents(new CacheListener(this), this); + plm.registerEvents(new CellSignListener(this), this); plm.registerEvents(new EntityListener(this), this); plm.registerEvents(new HandCuffListener(this), this); plm.registerEvents(new JailingListener(this), this); diff --git a/src/main/java/com/graywolf336/jail/Util.java b/src/main/java/com/graywolf336/jail/Util.java index c370aea..46028ad 100644 --- a/src/main/java/com/graywolf336/jail/Util.java +++ b/src/main/java/com/graywolf336/jail/Util.java @@ -23,6 +23,7 @@ import org.bukkit.util.io.BukkitObjectOutputStream; import org.yaml.snakeyaml.external.biz.base64Coder.Base64Coder; import com.graywolf336.jail.beans.Prisoner; +import com.graywolf336.jail.enums.Lang; /** * Provides a variety of methods, static, that are used throughout the plugin. @@ -95,6 +96,23 @@ public class Util { public static String getColorfulMessage(String message) { return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1"); } + + /** Returns a message with all the possible variables replaced. */ + public static String replaceAllVariables(Prisoner p, String msg) { + msg = msg.replaceAll("%player%", p.getLastKnownName()); + msg = msg.replaceAll("%uuid%", p.getUUID().toString()); + msg = msg.replaceAll("%reason%", p.getReason()); + msg = msg.replaceAll("%jailer", p.getJailer()); + msg = msg.replaceAll("%afktime%", TimeUnit.MILLISECONDS.toMinutes(p.getAFKTime()) + "m"); + + if(p.getRemainingTime() >= 0) { + msg = msg.replaceAll("%timeinminutes%", String.valueOf(p.getRemainingTimeInMinutes())); + }else { + msg = msg.replaceAll("%timeinminutes%", Lang.JAILEDFOREVERSIGN.get()); + } + + return getColorfulMessage(msg); + } /** Returns the wand used throughout the different creation steps. */ public static ItemStack getWand() { @@ -139,16 +157,16 @@ public class Util { if (match.matches()) { String units = match.group(2); if ("seconds".equals(units) || "second".equals(units) || "s".equals(units)) - t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.SECONDS); + t = TimeUnit.MILLISECONDS.toSeconds(Long.valueOf(match.group(1))); else if ("minutes".equals(units) || "minute".equals(units) || "mins".equals(units) || "min".equals(units) || "m".equals(units)) - t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.MINUTES); + t = TimeUnit.MILLISECONDS.toMinutes(Long.valueOf(match.group(1))); else if ("hours".equals(units) || "hour".equals(units) || "h".equals(units)) - t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.HOURS); + t = TimeUnit.MILLISECONDS.toHours(Long.valueOf(match.group(1))); else if ("days".equals(units) || "day".equals(units) || "d".equals(units)) - t = TimeUnit.MILLISECONDS.convert(Long.valueOf(match.group(1)), TimeUnit.DAYS); + t = TimeUnit.MILLISECONDS.toDays(Long.valueOf(match.group(1))); else { try { - t = TimeUnit.MILLISECONDS.convert(Long.parseLong(time), TimeUnit.MINUTES); + t = TimeUnit.MILLISECONDS.toMinutes(Long.parseLong(time)); }catch(NumberFormatException e) { throw new Exception("Invalid format."); } diff --git a/src/main/java/com/graywolf336/jail/beans/Cell.java b/src/main/java/com/graywolf336/jail/beans/Cell.java index 7b03cd8..a510512 100644 --- a/src/main/java/com/graywolf336/jail/beans/Cell.java +++ b/src/main/java/com/graywolf336/jail/beans/Cell.java @@ -72,6 +72,11 @@ public class Cell { public HashSet getSigns() { return this.signs; } + + /** Checks if there are any signs for this cell. */ + public boolean hasSigns() { + return !this.signs.isEmpty(); + } /** Returns the entire list of signs in a string. */ public String getSignString() { diff --git a/src/main/java/com/graywolf336/jail/beans/Prisoner.java b/src/main/java/com/graywolf336/jail/beans/Prisoner.java index 1454da8..ab24b4f 100644 --- a/src/main/java/com/graywolf336/jail/beans/Prisoner.java +++ b/src/main/java/com/graywolf336/jail/beans/Prisoner.java @@ -112,7 +112,7 @@ public class Prisoner { /** Gets the remaining time the prisoner has in minutes. */ public long getRemainingTimeInMinutes() { - return TimeUnit.MINUTES.convert(time, TimeUnit.MILLISECONDS); + return TimeUnit.MILLISECONDS.toMinutes(time); } /** Gets the remaining time the prison has in minutes except only in int format. */ diff --git a/src/main/java/com/graywolf336/jail/enums/Lang.java b/src/main/java/com/graywolf336/jail/enums/Lang.java index 57fd568..1ff0e98 100644 --- a/src/main/java/com/graywolf336/jail/enums/Lang.java +++ b/src/main/java/com/graywolf336/jail/enums/Lang.java @@ -125,10 +125,14 @@ public enum Lang { // General section, used by different parts /** Part message of any messages which require 'all the jails' or such. */ ALLJAILS("general"), + /** The one line on signs when the cell is empty. */ + CELLEMPTYSIGN("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 line on a cell's sign when the prisoner is jailed forever. */ + JAILEDFOREVERSIGN("general"), /** The simple word jailing to be put in other parts. */ JAILING("general"), /** The message sent to the sender when they try to remove a jail but there are still prisoners in there. */ diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 227ac4c..d41f80a 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -9,6 +9,7 @@ public enum Settings { BLOCKPLACEPENALTY("jailing.during.blockPlacePenalty"), BLOCKPLACEPROTECTION("jailing.during.blockPlaceProtection"), BLOCKPLACEWHITELIST("jailing.during.blockPlaceWhiteList"), + CELLSIGNLINES("jailing.during.cellsign"), CLOTHINGENABLED("jailing.jail.clothing.enabled"), CLOTHINGHELMET("jailing.jail.clothing.helmet"), CLOTHINGCHEST("jailing.jail.clothing.chest"), diff --git a/src/main/java/com/graywolf336/jail/listeners/CellSignListener.java b/src/main/java/com/graywolf336/jail/listeners/CellSignListener.java new file mode 100644 index 0000000..b164ced --- /dev/null +++ b/src/main/java/com/graywolf336/jail/listeners/CellSignListener.java @@ -0,0 +1,122 @@ +package com.graywolf336.jail.listeners; + +import java.util.HashSet; +import java.util.List; + +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import com.graywolf336.jail.JailMain; +import com.graywolf336.jail.Util; +import com.graywolf336.jail.beans.SimpleLocation; +import com.graywolf336.jail.enums.Lang; +import com.graywolf336.jail.enums.Settings; +import com.graywolf336.jail.events.PrisonerReleasedEvent; +import com.graywolf336.jail.events.PrisonerTimeChangeEvent; +import com.graywolf336.jail.events.PrisonerTransferredEvent; + +public class CellSignListener implements Listener { + private String lineOne, lineTwo, lineThree, lineFour; + + public CellSignListener(JailMain plugin) { + List lines = plugin.getConfig().getStringList(Settings.CELLSIGNLINES.getPath()); + lineOne = lines.get(0); + lineTwo = lines.get(1); + lineThree = lines.get(2); + lineFour = lines.get(3); + } + + @EventHandler + public void changeTheCellSigns(PrisonerTimeChangeEvent event) { + if(event.hasCell() && event.getCell().hasSigns()) { + HashSet signs = event.getCell().getSigns(); + String s1 = Util.replaceAllVariables(event.getPrisoner(), lineOne); + String s2 = Util.replaceAllVariables(event.getPrisoner(), lineTwo); + String s3 = Util.replaceAllVariables(event.getPrisoner(), lineThree); + String s4 = Util.replaceAllVariables(event.getPrisoner(), lineFour); + + for(SimpleLocation s : signs) { + if(s.getLocation().getBlock().getState() instanceof Sign) { + Sign sign = (Sign) s.getLocation().getBlock().getState(); + sign.setLine(0, s1); + sign.setLine(1, s2); + sign.setLine(2, s3); + sign.setLine(3, s4); + sign.update(); + }else { + //Remove the sign from the cell since it isn't + //a valid sign + event.getCell().getSigns().remove(s); + } + } + } + } + + @EventHandler + public void clearTheCellSigns(PrisonerReleasedEvent event) { + if(event.hasCell() && event.getCell().hasSigns()) { + HashSet signs = event.getCell().getSigns(); + + for(SimpleLocation s : signs) { + if(s.getLocation().getBlock().getState() instanceof Sign) { + Sign sign = (Sign) s.getLocation().getBlock().getState(); + sign.setLine(0, ""); + sign.setLine(1, Lang.CELLEMPTYSIGN.get()); + sign.setLine(2, ""); + sign.setLine(3, ""); + sign.update(); + }else { + //Remove the sign from the cell since it isn't + //a valid sign + event.getCell().getSigns().remove(s); + } + } + } + } + + @EventHandler + public void handleSignsOnTransfer(PrisonerTransferredEvent event) { + if(event.hasOriginalCell() && event.getOriginalCell().hasSigns()) { + HashSet signs = event.getOriginalCell().getSigns(); + + for(SimpleLocation s : signs) { + if(s.getLocation().getBlock().getState() instanceof Sign) { + Sign sign = (Sign) s.getLocation().getBlock().getState(); + sign.setLine(0, ""); + sign.setLine(1, Lang.CELLEMPTYSIGN.get()); + sign.setLine(2, ""); + sign.setLine(3, ""); + sign.update(); + }else { + //Remove the sign from the cell since it isn't + //a valid sign + event.getOriginalCell().getSigns().remove(s); + } + } + } + + if(event.hasTargetCell() && event.getTargetCell().hasSigns()) { + HashSet signs = event.getTargetCell().getSigns(); + String s1 = Util.replaceAllVariables(event.getPrisoner(), lineOne); + String s2 = Util.replaceAllVariables(event.getPrisoner(), lineTwo); + String s3 = Util.replaceAllVariables(event.getPrisoner(), lineThree); + String s4 = Util.replaceAllVariables(event.getPrisoner(), lineFour); + + for(SimpleLocation s : signs) { + if(s.getLocation().getBlock().getState() instanceof Sign) { + Sign sign = (Sign) s.getLocation().getBlock().getState(); + sign.setLine(0, s1); + sign.setLine(1, s2); + sign.setLine(2, s3); + sign.setLine(3, s4); + sign.update(); + }else { + //Remove the sign from the cell since it isn't + //a valid sign + event.getTargetCell().getSigns().remove(s); + } + } + } + } +} diff --git a/src/main/java/com/graywolf336/jail/steps/CellCreationSteps.java b/src/main/java/com/graywolf336/jail/steps/CellCreationSteps.java index f57915d..6f60d6f 100644 --- a/src/main/java/com/graywolf336/jail/steps/CellCreationSteps.java +++ b/src/main/java/com/graywolf336/jail/steps/CellCreationSteps.java @@ -4,6 +4,7 @@ import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; @@ -14,6 +15,7 @@ import com.graywolf336.jail.beans.Cell; import com.graywolf336.jail.beans.CreationPlayer; import com.graywolf336.jail.beans.Jail; import com.graywolf336.jail.beans.SimpleLocation; +import com.graywolf336.jail.enums.Lang; /** * Class for stepping a player through the Cell creation process, instance is stored in {@link JailManager}. @@ -93,6 +95,15 @@ public class CellCreationSteps { private void secondStep(CreationPlayer cp, Player player, Block block) { if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) { cp.addSign(new SimpleLocation(block.getLocation())); + + //Set the sign's first text + Sign sign = (Sign) block.getState(); + sign.setLine(0, ""); + sign.setLine(1, Lang.CELLEMPTYSIGN.get()); + sign.setLine(2, ""); + sign.setLine(3, ""); + sign.update(); + player.sendMessage(ChatColor.GREEN + "Sign added, if you want to select another go ahead otherwise right click on any non-sign block."); }else { player.sendMessage(ChatColor.AQUA + "---------- Jail Cell Creation (chest) ----------"); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index ecfe8f4..0c3ad9b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -24,6 +24,11 @@ jailing: blockPlacePenalty: 5m blockPlaceProtection: true blockPlaceWhiteList: ['crops', 'carrot', 'potato'] # these blocks can be placed at any time by prisoners + cellsign: + - '%player%' + - '%timeinminutes% mins' + - 'with a reason' + - '%reason%' commandPenalty: 5m commandProtection: true commandWhitelist: ['/ping', '/list', '/jail status', '/jail pay'] diff --git a/src/main/resources/en.yml b/src/main/resources/en.yml index d7a3c87..979ada6 100644 --- a/src/main/resources/en.yml +++ b/src/main/resources/en.yml @@ -14,8 +14,10 @@ language: start: "&cPlease type '&b/jail confirm&c' to confirm we should continue." general: alljails: 'all the jails' + cellemptysign: 'Empty cell' 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%.' + jailedforeversign: '&cfor life' jailing: '&9jailing' jailremovalunsuccessful: '&cThe removal of the jail %0% was unsuccessful because there are prisoners in there, please release or transfer them first.' jailremoved: '&9Jail %0% has been successfully deleted.'