diff --git a/README.md b/README.md index 93a9f3d..b3937a8 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,11 @@ With this being version 3 of this plugin, a couple things have changed but the c * New language system * New config system (per jail configs are going to make a come back) * Only prisoners will be able to place/break blocks on the whitelist -* Items in the config use item names now, not the ids +* Items in the config use item names now, **not** the ids ToDo === * About everything -* Enable a whitelist of blocks which can be broke -* Enabled a whitelist of blocks which can be placed Done === diff --git a/src/main/java/com/graywolf336/jail/Util.java b/src/main/java/com/graywolf336/jail/Util.java index 64028ac..8fa5efd 100644 --- a/src/main/java/com/graywolf336/jail/Util.java +++ b/src/main/java/com/graywolf336/jail/Util.java @@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.LinkedList; +import java.util.List; import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -73,6 +74,24 @@ public class Util { return (point1 <= loc) && (loc <= point2); } + /** + * Checks if the given string is inside the list, ignoring the casing. + * + *

+ * + * @param list of strings to check + * @param value to check + * @return true if the list contains the provided value, false if it doesn't + */ + public static boolean isStringInsideList(List list, String value) { + boolean r = false; + for(String s : list) + if(s.equalsIgnoreCase(value)) + return true; + + return r; + } + /** Returns a colorful message from the color codes. */ public static String getColorfulMessage(String message) { return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1"); diff --git a/src/main/java/com/graywolf336/jail/enums/Settings.java b/src/main/java/com/graywolf336/jail/enums/Settings.java index 8224fe9..5a7c740 100644 --- a/src/main/java/com/graywolf336/jail/enums/Settings.java +++ b/src/main/java/com/graywolf336/jail/enums/Settings.java @@ -4,8 +4,10 @@ public enum Settings { BROADCASTJAILING("jailing.jail.broadcastJailing"), BLOCKBREAKPENALTY("jailing.during.blockBreakPenalty"), BLOCKBREAKPROTECTION("jailing.during.blockBreakProtection"), + BLOCKBREAKWHITELIST("jailing.during.blockBreakWhiteList"), BLOCKPLACEPENALTY("jailing.during.blockPlacePenalty"), BLOCKPLACEPROTECTION("jailing.during.blockPlaceProtection"), + BLOCKPLACEWHITELIST("jailing.during.blockPlaceWhiteList"), COMMANDSONJAIL("jailing.jail.commands"), COMMANDSONRELEASE("jailing.release.commands"), COUNTDOWNTIMEOFFLINE("jailing.during.countDownTimeWhileOffline"), diff --git a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java index fa5e7d5..862463f 100644 --- a/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java +++ b/src/main/java/com/graywolf336/jail/listeners/ProtectionListener.java @@ -28,25 +28,30 @@ public class ProtectionListener implements Listener { //in the BlockListener class will take care of protecting inside //of the jails. if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { - //As our Util.getTime throws an exception when the time is in an - //incorrect format, we catch the exception and don't add any time - //as a fail safe, don't want us to go crazy adding tons of time. - try { - long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath())); - pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); - - //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 - String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, - new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), - pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) }); - - //Send the message and then stop the event from happening - event.getPlayer().sendMessage(msg); - event.setCancelled(true); - }catch (Exception e) { - pl.getLogger().severe("Block break penalty's time is in the wrong format, please fix."); - event.setCancelled(true); + //Get the breaking whitelist, check if the current item is in there + //the list must be lowercase, need to stress that + if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKBREAKWHITELIST.getPath()), + event.getBlock().getType().toString().toLowerCase())) { + //As our Util.getTime throws an exception when the time is in an + //incorrect format, we catch the exception and don't add any time + //as a fail safe, don't want us to go crazy adding tons of time. + try { + long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath())); + pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); + + //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 + String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, + new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), + pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) }); + + //Send the message and then stop the event from happening + event.getPlayer().sendMessage(msg); + event.setCancelled(true); + }catch (Exception e) { + pl.getLogger().severe("Block break penalty's time is in the wrong format, please fix."); + event.setCancelled(true); + } } } } @@ -61,25 +66,30 @@ public class ProtectionListener implements Listener { //in the BlockListener class will take care of protecting inside //of the jails. if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { - //As our Util.getTime throws an exception when the time is in an - //incorrect format, we catch the exception and don't add any time - //as a fail safe, don't want us to go crazy adding tons of time. - try { - long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKPLACEPENALTY.getPath())); - pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); - - //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 - String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, - new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), - pl.getJailIO().getLanguageString(LangString.BLOCKPLACING) }); - - //Send the message and then stop the event from happening - event.getPlayer().sendMessage(msg); - event.setCancelled(true); - }catch (Exception e) { - pl.getLogger().severe("Block place penalty's time is in the wrong format, please fix."); - event.setCancelled(true); + //Get the placing whitelist, check if the current item is in there + //the list must be lowercase, need to stress that + if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKPLACEWHITELIST.getPath()), + event.getBlock().getType().toString().toLowerCase())) { + //As our Util.getTime throws an exception when the time is in an + //incorrect format, we catch the exception and don't add any time + //as a fail safe, don't want us to go crazy adding tons of time. + try { + long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKPLACEPENALTY.getPath())); + pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); + + //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 + String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, + new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)), + pl.getJailIO().getLanguageString(LangString.BLOCKPLACING) }); + + //Send the message and then stop the event from happening + event.getPlayer().sendMessage(msg); + event.setCancelled(true); + }catch (Exception e) { + pl.getLogger().severe("Block place penalty's time is in the wrong format, please fix."); + event.setCancelled(true); + } } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index baee63d..e7a45e5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -15,8 +15,10 @@ jailing: during: blockBreakPenalty: 5m blockBreakProtection: true + blockBreakWhiteList: ['wheat', 'carrot'] # these blocks can be broken at any time by prisoners blockPlacePenalty: 5m blockPlaceProtection: true + blockPlaceWhiteList: ['seeds', 'carrot'] # these blocks can be placed at any time by prisoners countDownTimeWhileOffline: false explosionProtection: true ignoreSleeping: true