Enable checking the whitelist of breaking and placing.

This commit is contained in:
graywolf336 2014-01-19 15:53:42 -06:00
parent da26bc172e
commit f41ee77092
5 changed files with 72 additions and 41 deletions

View File

@ -11,13 +11,11 @@ With this being version 3 of this plugin, a couple things have changed but the c
* New language system * New language system
* New config system (per jail configs are going to make a come back) * 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 * 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 ToDo
=== ===
* About everything * About everything
* Enable a whitelist of blocks which can be broke
* Enabled a whitelist of blocks which can be placed
Done Done
=== ===

View File

@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -73,6 +74,24 @@ public class Util {
return (point1 <= loc) && (loc <= point2); return (point1 <= loc) && (loc <= point2);
} }
/**
* Checks if the given string is inside the list, ignoring the casing.
*
* <p />
*
* @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<String> 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. */ /** Returns a colorful message from the color codes. */
public static String getColorfulMessage(String message) { public static String getColorfulMessage(String message) {
return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1"); return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1");

View File

@ -4,8 +4,10 @@ public enum Settings {
BROADCASTJAILING("jailing.jail.broadcastJailing"), BROADCASTJAILING("jailing.jail.broadcastJailing"),
BLOCKBREAKPENALTY("jailing.during.blockBreakPenalty"), BLOCKBREAKPENALTY("jailing.during.blockBreakPenalty"),
BLOCKBREAKPROTECTION("jailing.during.blockBreakProtection"), BLOCKBREAKPROTECTION("jailing.during.blockBreakProtection"),
BLOCKBREAKWHITELIST("jailing.during.blockBreakWhiteList"),
BLOCKPLACEPENALTY("jailing.during.blockPlacePenalty"), BLOCKPLACEPENALTY("jailing.during.blockPlacePenalty"),
BLOCKPLACEPROTECTION("jailing.during.blockPlaceProtection"), BLOCKPLACEPROTECTION("jailing.during.blockPlaceProtection"),
BLOCKPLACEWHITELIST("jailing.during.blockPlaceWhiteList"),
COMMANDSONJAIL("jailing.jail.commands"), COMMANDSONJAIL("jailing.jail.commands"),
COMMANDSONRELEASE("jailing.release.commands"), COMMANDSONRELEASE("jailing.release.commands"),
COUNTDOWNTIMEOFFLINE("jailing.during.countDownTimeWhileOffline"), COUNTDOWNTIMEOFFLINE("jailing.during.countDownTimeWhileOffline"),

View File

@ -28,25 +28,30 @@ public class ProtectionListener implements Listener {
//in the BlockListener class will take care of protecting inside //in the BlockListener class will take care of protecting inside
//of the jails. //of the jails.
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//As our Util.getTime throws an exception when the time is in an //Get the breaking whitelist, check if the current item is in there
//incorrect format, we catch the exception and don't add any time //the list must be lowercase, need to stress that
//as a fail safe, don't want us to go crazy adding tons of time. if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKBREAKWHITELIST.getPath()),
try { event.getBlock().getType().toString().toLowerCase())) {
long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath())); //As our Util.getTime throws an exception when the time is in an
pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); //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.
//Generate the protection message, provide the method with two arguments try {
//First is the time in minutes and second is the thing we are protecting against long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKBREAKPENALTY.getPath()));
String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add);
new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) }); //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
//Send the message and then stop the event from happening String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE,
event.getPlayer().sendMessage(msg); new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
event.setCancelled(true); pl.getJailIO().getLanguageString(LangString.BLOCKBREAKING) });
}catch (Exception e) {
pl.getLogger().severe("Block break penalty's time is in the wrong format, please fix."); //Send the message and then stop the event from happening
event.setCancelled(true); 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 //in the BlockListener class will take care of protecting inside
//of the jails. //of the jails.
if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) { if(pl.getJailManager().isPlayerJailed(event.getPlayer().getName())) {
//As our Util.getTime throws an exception when the time is in an //Get the placing whitelist, check if the current item is in there
//incorrect format, we catch the exception and don't add any time //the list must be lowercase, need to stress that
//as a fail safe, don't want us to go crazy adding tons of time. if(!Util.isStringInsideList(pl.getConfig().getStringList(Settings.BLOCKPLACEWHITELIST.getPath()),
try { event.getBlock().getType().toString().toLowerCase())) {
long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKPLACEPENALTY.getPath())); //As our Util.getTime throws an exception when the time is in an
pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add); //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.
//Generate the protection message, provide the method with two arguments try {
//First is the time in minutes and second is the thing we are protecting against long add = Util.getTime(pl.getConfig().getString(Settings.BLOCKPLACEPENALTY.getPath()));
String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE, pl.getJailManager().getPrisoner(event.getPlayer().getName()).addTime(add);
new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
pl.getJailIO().getLanguageString(LangString.BLOCKPLACING) }); //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
//Send the message and then stop the event from happening String msg = pl.getJailIO().getLanguageString(LangString.PROTECTIONMESSAGE,
event.getPlayer().sendMessage(msg); new String[] { String.valueOf(TimeUnit.MINUTES.convert(add, TimeUnit.MILLISECONDS)),
event.setCancelled(true); pl.getJailIO().getLanguageString(LangString.BLOCKPLACING) });
}catch (Exception e) {
pl.getLogger().severe("Block place penalty's time is in the wrong format, please fix."); //Send the message and then stop the event from happening
event.setCancelled(true); 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);
}
} }
} }
} }

View File

@ -15,8 +15,10 @@ jailing:
during: during:
blockBreakPenalty: 5m blockBreakPenalty: 5m
blockBreakProtection: true blockBreakProtection: true
blockBreakWhiteList: ['wheat', 'carrot'] # these blocks can be broken at any time by prisoners
blockPlacePenalty: 5m blockPlacePenalty: 5m
blockPlaceProtection: true blockPlaceProtection: true
blockPlaceWhiteList: ['seeds', 'carrot'] # these blocks can be placed at any time by prisoners
countDownTimeWhileOffline: false countDownTimeWhileOffline: false
explosionProtection: true explosionProtection: true
ignoreSleeping: true ignoreSleeping: true