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 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
===

View File

@ -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.
*
* <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. */
public static String getColorfulMessage(String message) {
return message.replaceAll("(?i)&([0-9abcdefklmnor])", "\u00A7$1");

View File

@ -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"),

View File

@ -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);
}
}
}
}

View File

@ -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