From 030f2df21ccd9bf7b820271ba17c6f7962c89736 Mon Sep 17 00:00:00 2001 From: Jesse Boyd Date: Sat, 24 Jan 2015 21:40:50 -0700 Subject: [PATCH] [UNSTABLE] Working on plot protection system --- .../intellectualcrafters/plot/PlotMain.java | 161 +--------- .../plot/commands/Buy.java | 2 +- .../plot/commands/Set.java | 8 +- .../plot/commands/list.java | 2 +- .../plot/database/SQLManager.java | 6 +- .../plot/flag/AbstractFlag.java | 15 +- .../intellectualcrafters/plot/flag/Flag.java | 20 +- .../plot/flag/FlagValue.java | 302 +++++++++++++++++- .../plot/listeners/ForceFieldListener.java | 2 +- .../plot/listeners/PlayerEvents.java | 8 +- .../plot/listeners/PlotListener.java | 57 ++-- .../plot/listeners/PlotPlusListener.java | 16 +- .../plot/object/PlotSettings.java | 4 +- 13 files changed, 377 insertions(+), 226 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java index 929b41e30..d93137f00 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotMain.java @@ -80,6 +80,7 @@ import com.intellectualcrafters.plot.events.PlayerTeleportToPlotEvent; import com.intellectualcrafters.plot.events.PlotDeleteEvent; import com.intellectualcrafters.plot.flag.AbstractFlag; import com.intellectualcrafters.plot.flag.FlagManager; +import com.intellectualcrafters.plot.flag.FlagValue; import com.intellectualcrafters.plot.generator.HybridGen; import com.intellectualcrafters.plot.generator.HybridPlotManager; import com.intellectualcrafters.plot.generator.HybridPlotWorld; @@ -1030,134 +1031,27 @@ public class PlotMain extends JavaPlugin implements Listener { final List intervalFlags = Arrays.asList("feed", "heal"); final List stringFlags = Arrays.asList("greeting", "farewell"); for (final String flag : stringFlags) { - FlagManager.addFlag(new AbstractFlag(flag) { - @Override - public String parseValue(final String value) { - return value; - } - - @Override - public String getValueDesc() { - return "Value must be a string, supports color codes (&)"; - } - }); + FlagManager.addFlag(new AbstractFlag(flag)); } for (final String flag : intervalFlags) { - FlagManager.addFlag(new AbstractFlag(flag) { - @Override - public String parseValue(final String value) { - int seconds; - int amount; - final String[] values = value.split(" "); - if (values.length < 2) { - seconds = 1; - try { - amount = Integer.parseInt(values[0]); - } catch (final Exception e) { - return null; - } - } else { - try { - amount = Integer.parseInt(values[0]); - seconds = Integer.parseInt(values[1]); - } catch (final Exception e) { - return null; - } - } - return amount + " " + seconds; - } - - @Override - public String getValueDesc() { - return "Value(s) must be numeric. /plot set flag {flag} {amount} [seconds]"; - } - }); + FlagManager.addFlag(new AbstractFlag(flag, new FlagValue.IntervalValue())); } for (final String flag : booleanFlags) { - FlagManager.addFlag(new AbstractFlag(flag) { - @Override - public String parseValue(final String value) { - switch (value) { - case "on": - case "1": - case "true": - case "enabled": - return "true"; - case "off": - case "0": - case "false": - case "disabled": - return "false"; - default: - return null; - } - } - - @Override - public String getValueDesc() { - return "Value must be true/false, 1/0, on/off, enabled/disabled"; - } - }); + FlagManager.addFlag(new AbstractFlag(flag, new FlagValue.BooleanValue())); } } private static void defaultFlags() { addPlusFlags(); - FlagManager.addFlag(new AbstractFlag("fly") { - @Override - public String parseValue(final String value) { - switch (value) { - case "on": - case "enabled": - case "true": - case "1": - return "true"; - case "off": - case "disabled": - case "false": - case "0": - return "false"; - default: - return null; - } - } - - @Override - public String getValueDesc() { - return "Flag value must be a boolean: true, false, enabled, disabled"; - } - }); + FlagManager.addFlag(new AbstractFlag("fly", new FlagValue.BooleanValue())); for (final String str : booleanFlags.values()) { - FlagManager.addFlag(new AbstractFlag(str) { - - @Override - public String parseValue(final String value) { - switch (value) { - case "true": - case "1": - case "yes": - return "true"; - case "false": - case "off": - case "0": - return "false"; - default: - return null; - } - } - - @Override - public String getValueDesc() { - return "Flag value must be a boolean: true, false"; - } - - }); + FlagManager.addFlag(new AbstractFlag(str, new FlagValue.BooleanValue())); } FlagManager.addFlag(new AbstractFlag("gamemode") { @Override - public String parseValue(final String value) { + public String parseValueRaw(final String value) { switch (value) { case "creative": case "c": @@ -1182,48 +1076,13 @@ public class PlotMain extends JavaPlugin implements Listener { } }); - FlagManager.addFlag(new AbstractFlag("price") { - @Override - public String parseValue(final String value) { - try { - double price = Double.parseDouble(value); - if (price >= 0) { - return price + ""; - } - else { - return null; - } - } - catch (Exception e) { - return null; - } - } + FlagManager.addFlag(new AbstractFlag("price", new FlagValue.UnsignedDoubleValue())); - @Override - public String getValueDesc() { - return "Flag value must be a positive number representing the price of the plot'"; - } - }); - - FlagManager.addFlag(new AbstractFlag("time") { - @Override - public String parseValue(final String value) { - try { - return Long.parseLong(value) + ""; - } catch (final Exception e) { - return null; - } - } - - @Override - public String getValueDesc() { - return "Flag value must be a time in ticks: 0=sunrise 12000=noon 18000=sunset 24000=night"; - } - }); + FlagManager.addFlag(new AbstractFlag("time", new FlagValue.LongValue())); FlagManager.addFlag(new AbstractFlag("weather") { @Override - public String parseValue(final String value) { + public String parseValueRaw(final String value) { switch (value) { case "rain": case "storm": diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Buy.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Buy.java index 9a3f30d81..180e0d056 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Buy.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Buy.java @@ -86,7 +86,7 @@ public class Buy extends SubCommand { if (flag == null) { return sendMessage(plr, C.NOT_FOR_SALE); } - double initPrice = Double.parseDouble(flag.getValue()); + double initPrice = (double) flag.getValue(); double price = initPrice; PlotId id = plot.id; PlotId id2 = PlayerFunctions.getTopPlot(world, plot).id; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java index 1cc77b811..c65fd50ed 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -149,18 +149,18 @@ public class Set extends SubCommand { } try { String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " "); - value = af.parseValue(value); - if (value == null) { + Object parsed_value = af.parseValueRaw(value); + if (parsed_value == null) { PlayerFunctions.sendMessage(plr, af.getValueDesc()); return false; } if ((FlagManager.getFlag(args[1].toLowerCase()) == null) && (PlotMain.worldGuardListener != null)) { - PlotMain.worldGuardListener.addFlag(plr, plr.getWorld(), plot, args[1], value); + PlotMain.worldGuardListener.addFlag(plr, plr.getWorld(), plot, args[1], af.toString(parsed_value)); return false; } - final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value); + final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), parsed_value); boolean result = FlagManager.addPlotFlag(plot, flag); if (!result) { PlayerFunctions.sendMessage(plr, C.FLAG_NOT_ADDED); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java index 1af387afa..4da3617a3 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/list.java @@ -80,7 +80,7 @@ public class list extends SubCommand { for (final Plot p : PlotMain.getPlots(plr.getWorld()).values()) { Flag price = FlagManager.getPlotFlag(p, "price"); if (price != null) { - string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", idx + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", price.getValue()).replaceAll("%owner", getName(p.owner))).append("\n"); + string.append(C.PLOT_LIST_ITEM_ORDERED.s().replaceAll("%in", idx + 1 + "").replaceAll("%id", p.id.toString()).replaceAll("%world", price.getValueString()).replaceAll("%owner", getName(p.owner))).append("\n"); idx++; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index e52645db9..52c293fa6 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -683,7 +683,9 @@ public class SQLManager implements AbstractDB { if (element.contains(":")) { final String[] split = element.split(":"); try { - flags.add(new Flag(FlagManager.getFlag(split[0], true), split[1].replaceAll("\u00AF", ":").replaceAll("�", ","))); + String flag_str = split[1].replaceAll("\u00AF", ":").replaceAll("�", ","); + Flag flag = new Flag(FlagManager.getFlag(split[0], true), flag_str); + flags.add(flag); } catch (final Exception e) { e.printStackTrace(); exception = true; @@ -756,7 +758,7 @@ public class SQLManager implements AbstractDB { if (i != 0) { flag_string.append(","); } - flag_string.append(flag.getKey() + ":" + flag.getValue().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4")); + flag_string.append(flag.getKey() + ":" + flag.getValueString().replaceAll(":", "\u00AF").replaceAll(",", "\u00B4")); i++; } TaskManager.runTask(new Runnable() { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java index 09a6a8d7d..fc1608f98 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/AbstractFlag.java @@ -37,11 +37,10 @@ public class AbstractFlag { public AbstractFlag(final String key) { this(key, new FlagValue.StringValue()); } - /** - * AbstractFlag is a parameter used in creating a new Flag - * - * @param key The key must be alphabetical characters and <= 16 characters in length + * AbstractFlag is a parameter used in creating a new Flag
+ * The key must be alphabetical characters and <= 16 characters in length + * @param key */ public AbstractFlag(final String key, final FlagValue value) { if (!StringUtils.isAlpha(key.replaceAll("_", "").replaceAll("-", ""))) { @@ -58,10 +57,14 @@ public class AbstractFlag { } } - public String parseValue(final String value) { + public Object parseValueRaw(final String value) { return this.value.parse(value); } - + + public String toString(final Object t) { + return this.value.toString(t); + } + public String getValueDesc() { return this.value.getDescription(); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/Flag.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/Flag.java index a5ec4551b..361e065a9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/Flag.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/Flag.java @@ -25,7 +25,7 @@ import org.apache.commons.lang.StringUtils; public class Flag { private final AbstractFlag key; - private final String value; + private final Object value; /** * Flag object used to store basic information for a Plot. Flags are a key/value pair. For a flag to be usable by a @@ -49,12 +49,20 @@ public class Flag { throw new IllegalArgumentException("Value must be <= 48 characters"); } this.key = key; - this.value = key.parseValue(value); + this.value = key.parseValueRaw(value); if (this.value == null) { throw new IllegalArgumentException(key.getValueDesc()); } } + /** + * Warning: Unchecked + */ + public Flag(final AbstractFlag key, final Object value) { + this.key = key; + this.value = value; + } + /** * Get the AbstractFlag used in creating the flag * @@ -78,16 +86,20 @@ public class Flag { * * @return String */ - public String getValue() { + public Object getValue() { return this.value; } + + public String getValueString() { + return this.key.toString(this.value); + } @Override public String toString() { if (this.value.equals("")) { return this.key.getKey(); } - return this.key + ":" + this.value; + return this.key + ":" + getValueString(); } @Override diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java index 1d169c952..5a9a7c009 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagValue.java @@ -1,5 +1,11 @@ package com.intellectualcrafters.plot.flag; +import java.util.HashSet; + +import org.apache.commons.lang.StringUtils; + +import com.intellectualcrafters.plot.object.PlotBlock; + /** * Created 2014-11-17 for PlotSquared * @@ -9,7 +15,8 @@ public abstract class FlagValue { private final Class clazz; - public FlagValue() { + @SuppressWarnings("unchecked") + public FlagValue() { this.clazz = (Class) getClass(); } @@ -23,28 +30,299 @@ public abstract class FlagValue { public boolean validValue(final Object value) { return (value != null) && (value.getClass() == this.clazz); } + + public String toString(Object t) { + return t.toString(); + } - public abstract T getValue(String t); + public abstract T getValue(Object t); - public abstract String parse(String t); + public abstract T parse(String t); public abstract String getDescription(); public static class BooleanValue extends FlagValue { - + @Override - public Boolean getValue(final String t) { - return null; + public Boolean getValue(final Object t) { + return (Boolean) t; } @Override - public String parse(final String t) { - return null; + public Boolean parse(final String t) { + try { + return Boolean.parseBoolean(t); + } + catch (IllegalArgumentException e) { + return null; + } } @Override public String getDescription() { - return null; + return "Flag value must be a boolean (true|false)"; + } + } + + public static class IntegerValue extends FlagValue { + + @Override + public Integer getValue(final Object t) { + return (Integer) t; + } + + @Override + public Integer parse(final String t) { + try { + return Integer.parseInt(t); + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a whole number"; + } + } + + public static class IntervalValue extends FlagValue { + + @Override + public String toString(Object t) { + Integer[] value = ((Integer[]) t); + return value[0] + " " + value[1]; + } + + @Override + public Integer[] getValue(final Object t) { + return (Integer[]) t; + } + + @Override + public Integer[] parse(final String t) { + int seconds; + int amount; + final String[] values = t.split(" "); + if (values.length < 2) { + seconds = 1; + try { + amount = Integer.parseInt(values[0]); + } catch (final Exception e) { + return null; + } + } else { + try { + amount = Integer.parseInt(values[0]); + seconds = Integer.parseInt(values[1]); + } catch (final Exception e) { + return null; + } + } + return new Integer[]{amount, seconds}; + } + + @Override + public String getDescription() { + return "Value(s) must be numeric. /plot set flag {flag} {amount} [seconds]"; + } + } + + public static class UnsignedIntegerValue extends FlagValue { + + @Override + public Integer getValue(final Object t) { + return (Integer) t; + } + + @Override + public Integer parse(final String t) { + try { + int value = Integer.parseInt(t); + if (value >= 0) { + return null; + } + return value; + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a positive whole number (includes 0)"; + } + } + + public static class DoubleValue extends FlagValue { + + @Override + public Double getValue(final Object t) { + return (Double) t; + } + + @Override + public Double parse(final String t) { + try { + return Double.parseDouble(t); + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a number (negative decimals are allowed)"; + } + } + + public static class LongValue extends FlagValue { + + @Override + public Long getValue(final Object t) { + return (Long) t; + } + + @Override + public Long parse(final String t) { + try { + return Long.parseLong(t); + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a whole number (large numbers allowed)"; + } + } + + public static class UnsignedLongValue extends FlagValue { + + @Override + public Long getValue(final Object t) { + return (Long) t; + } + + @Override + public Long parse(final String t) { + try { + long value = Long.parseLong(t); + if (value < 0) { + return null; + } + return value; + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a positive whole number (large numbers allowed)"; + } + } + + public static class UnsignedDoubleValue extends FlagValue { + + @Override + public Double getValue(final Object t) { + return (Double) t; + } + + @Override + public Double parse(final String t) { + try { + double value = Double.parseDouble(t); + if (value < 0) { + return null; + } + return value; + } + catch (IllegalArgumentException e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a positive number (decimals allowed)"; + } + } + + public static class PlotBlockValue extends FlagValue { + + @Override + public PlotBlock getValue(final Object t) { + return (PlotBlock) t; + } + + @Override + public PlotBlock parse(final String t) { + try { + String[] split = t.split(":"); + byte data; + if (split.length == 2) { + data = Byte.parseByte(split[1]); + } + else { + data = 0; + } + short id = Short.parseShort(split[0]); + return new PlotBlock(id, data); + } + catch (Exception e) { + return null; + } + } + + @Override + public String getDescription() { + return "Flag value must be a number (negative decimals are allowed)"; + } + } + + public static class PlotBlockListValue extends FlagValue> { + + @Override + public String toString(Object t) { + return StringUtils.join((HashSet) t, ","); + } + + @SuppressWarnings("unchecked") + @Override + public HashSet getValue(final Object t) { + return (HashSet)t; + } + + @Override + public HashSet parse(final String t) { + HashSet list = new HashSet(); + for (String item : t.split(",")) { + String[] split = item.split(":"); + byte data; + if (split.length == 2) { + data = Byte.parseByte(split[1]); + } + else { + data = 0; + } + short id = Short.parseShort(split[0]); + PlotBlock block = new PlotBlock(id, data); + list.add(block); + } + return list; + } + + @Override + public String getDescription() { + return "Flag value must be a block list"; } } @@ -57,12 +335,12 @@ public abstract class FlagValue { @Override public String getDescription() { - return "Flag value must be alphanumeric"; + return "Flag value must be alphanumeric. Some special characters are allowed."; } @Override - public String getValue(final String t) { - return t; + public String getValue(final Object t) { + return t.toString(); } } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java index 8be57a19e..4baed4e9a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java @@ -104,7 +104,7 @@ public class ForceFieldListener implements Listener { } final Plot plot = PlayerFunctions.getCurrentPlot(player); if ((FlagManager.getPlotFlag(plot, "forcefield") != null) && FlagManager.getPlotFlag(plot, "forcefield").getValue().equals("true")) { - if (!PlotListener.booleanFlag(plot, "forcefield")) { + if (!PlotListener.booleanFlag(plot, "forcefield", false)) { if (plot.hasRights(player)) { final Set players = getNearbyPlayers(player, plot); for (final Player oPlayer : players) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 61f907389..02a39122a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -468,8 +468,8 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } if (PlotMain.booleanFlags.containsKey(event.getClickedBlock().getType())) { final String flag = PlotMain.booleanFlags.get(event.getClickedBlock().getType()); - if ((FlagManager.getPlotFlag(plot, flag) != null) && getFlagValue(FlagManager.getPlotFlag(plot, flag).getValue())) { - return; + if (PlotListener.booleanFlag(plot, flag, false)) { + return; } } if (!plot.hasRights(event.getPlayer())) { @@ -782,10 +782,10 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi e.setCancelled(true); return; } - } else if (aPlr && !booleanFlag(plot, "pvp")) { + } else if (aPlr && !booleanFlag(plot, "pvp", false)) { return; } - if (!aPlr && !booleanFlag(plot, "pve")) { + if (!aPlr && !booleanFlag(plot, "pve", false)) { return; } assert plot != null; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java index 7b866b2ab..94765cb33 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java @@ -41,6 +41,7 @@ import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent; import com.intellectualcrafters.plot.events.PlayerLeavePlotEvent; +import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.PlotId; @@ -49,6 +50,7 @@ import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.titles.AbstractTitle; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.UUIDHandler; +import com.sk89q.worldguard.protection.flags.BooleanFlag; /** * @author Citymonstret @@ -61,6 +63,18 @@ import com.intellectualcrafters.plot.util.UUIDHandler; p.setResourcePack(Settings.PLOT_SPECIFIC_RESOURCE_PACK); } } + + public static boolean booleanFlag(Plot plot, String key, boolean defaultValue) { + Flag flag = FlagManager.getPlotFlag(plot, key); + if (flag == null) { + return defaultValue; + } + Object value = flag.getValue(); + if (value instanceof Boolean) { + return (boolean) value; + } + return defaultValue; + } public static boolean isInPlot(final Player player) { return PlayerFunctions.isInPlot(player); @@ -149,27 +163,6 @@ import com.intellectualcrafters.plot.util.UUIDHandler; } } - public static boolean booleanFlag(final Plot plot, final String flag) { - return (FlagManager.getPlotFlag(plot, flag) != null) && getBooleanFlag(FlagManager.getPlotFlag(plot, flag).getValue()).equals("true"); - } - - private static String getBooleanFlag(final String value) { - switch (value) { - case "on": - case "1": - case "true": - case "enabled": - return "true"; - case "off": - case "0": - case "false": - case "disabled": - return "false"; - default: - return null; - } - } - private static GameMode getGameMode(final String str) { switch (str) { case "creative": @@ -185,24 +178,28 @@ import com.intellectualcrafters.plot.util.UUIDHandler; public static void plotEntry(final Player player, final Plot plot) { if (plot.hasOwner()) { - if (FlagManager.getPlotFlag(plot, "gamemode") != null) { - player.setGameMode(getGameMode(FlagManager.getPlotFlag(plot, "gamemode").getValue())); + Flag gamemodeFlag = FlagManager.getPlotFlag(plot, "gamemode"); + if (gamemodeFlag != null) { + player.setGameMode(getGameMode(gamemodeFlag.getValueString())); } - if (FlagManager.getPlotFlag(plot, "fly") != null) { - player.setAllowFlight(getFlagValue(FlagManager.getPlotFlag(plot, "fly").getValue())); + Flag flyFlag = FlagManager.getPlotFlag(plot, "fly"); + if (flyFlag != null) { + player.setAllowFlight((boolean) flyFlag.getValue()); } - if (FlagManager.getPlotFlag(plot, "time") != null) { + Flag timeFlag = FlagManager.getPlotFlag(plot, "time"); + if (timeFlag != null) { try { - final Long time = Long.parseLong(FlagManager.getPlotFlag(plot, "time").getValue()); + long time = (long) timeFlag.getValue(); player.setPlayerTime(time, true); } catch (final Exception e) { FlagManager.removePlotFlag(plot, "time"); } } - if (FlagManager.getPlotFlag(plot, "weather") != null) { - player.setPlayerWeather(getWeatherType(FlagManager.getPlotFlag(plot, "weather").getValue())); + Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather"); + if (weatherFlag != null) { + player.setPlayerWeather(getWeatherType(weatherFlag.getValueString())); } - if ((booleanFlag(plot, "titles") || Settings.TITLES) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) { + if ((booleanFlag(plot, "titles", false) || Settings.TITLES) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) { final String sTitleMain = C.TITLE_ENTERED_PLOT.s().replaceAll("%x%", plot.id.x + "").replaceAll("%z%", plot.id.y + "").replaceAll("%world%", plot.world + ""); final String sTitleSub = C.TITLE_ENTERED_PLOT_SUB.s().replaceFirst("%s", getName(plot.owner)); final ChatColor sTitleMainColor = ChatColor.valueOf(C.TITLE_ENTERED_PLOT_COLOR.s()); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java index f9e7bda2b..0ae36944e 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java @@ -145,7 +145,7 @@ import com.intellectualcrafters.plot.util.UUIDHandler; return; } final Plot plot = getPlot(player); - if (booleanFlag(plot, "instabreak")) { + if (booleanFlag(plot, "instabreak", false)) { event.getBlock().breakNaturally(); } } @@ -159,21 +159,21 @@ import com.intellectualcrafters.plot.util.UUIDHandler; if (!isInPlot(player)) { return; } - if (booleanFlag(getPlot(player), "invincible")) { + if (booleanFlag(getPlot(player), "invincible", false)) { event.setCancelled(true); } } @EventHandler public void onItemPickup(final PlayerPickupItemEvent event) { - if (isInPlot(event.getPlayer()) && !getPlot(event.getPlayer()).hasRights(event.getPlayer()) && booleanFlag(getPlot(event.getPlayer()), "drop-protection")) { + if (isInPlot(event.getPlayer()) && !getPlot(event.getPlayer()).hasRights(event.getPlayer()) && booleanFlag(getPlot(event.getPlayer()), "drop-protection", false)) { event.setCancelled(true); } } @EventHandler public void onItemDrop(final PlayerDropItemEvent event) { - if (isInPlot(event.getPlayer()) && !getPlot(event.getPlayer()).hasRights(event.getPlayer()) && booleanFlag(getPlot(event.getPlayer()), "item-drop")) { + if (isInPlot(event.getPlayer()) && !getPlot(event.getPlayer()).hasRights(event.getPlayer()) && booleanFlag(getPlot(event.getPlayer()), "item-drop", false)) { event.setCancelled(true); } } @@ -182,9 +182,9 @@ import com.intellectualcrafters.plot.util.UUIDHandler; public void onPlotEnter(final PlayerEnterPlotEvent event) { final Plot plot = event.getPlot(); if (FlagManager.getPlotFlag(plot, "greeting") != null) { - event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "greeting").getValue())); + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "greeting").getValueString())); } - if (booleanFlag(plot, "notify-enter")) { + if (booleanFlag(plot, "notify-enter", false)) { if (plot.hasOwner()) { final Player player = UUIDHandler.uuidWrapper.getPlayer(plot.getOwner()); @@ -216,7 +216,7 @@ import com.intellectualcrafters.plot.util.UUIDHandler; event.getPlayer().playEffect(event.getPlayer().getLocation(), Effect.RECORD_PLAY, 0); final Plot plot = event.getPlot(); if (FlagManager.getPlotFlag(plot, "farewell") != null) { - event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "farewell").getValue())); + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "farewell").getValueString())); } if (feedRunnable.containsKey(event.getPlayer().getName())) { feedRunnable.remove(event.getPlayer().getName()); @@ -224,7 +224,7 @@ import com.intellectualcrafters.plot.util.UUIDHandler; if (healRunnable.containsKey(event.getPlayer().getName())) { healRunnable.remove(event.getPlayer().getName()); } - if (booleanFlag(plot, "notify-leave")) { + if (booleanFlag(plot, "notify-leave", false)) { if (plot.hasOwner()) { final Player player = UUIDHandler.uuidWrapper.getPlayer(plot.getOwner()); if (player == null) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSettings.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSettings.java index 4b2003101..e0a39f0ad 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSettings.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotSettings.java @@ -136,7 +136,7 @@ import com.intellectualcrafters.plot.util.PlotHelper; public String getJoinMessage() { final Flag greeting = FlagManager.getPlotFlag(plot, "greeting"); if (greeting != null) { - return greeting.getValue(); + return greeting.getValueString(); } return ""; } @@ -149,7 +149,7 @@ import com.intellectualcrafters.plot.util.PlotHelper; public String getLeaveMessage() { final Flag farewell = FlagManager.getPlotFlag(plot, "farewell"); if (farewell != null) { - return farewell.getValue(); + return farewell.getValueString(); } return ""; }