From 3796f41be321430c4af59b1d54d843b1fa4b7d94 Mon Sep 17 00:00:00 2001 From: boy0001 Date: Sun, 19 Oct 2014 21:27:27 +1100 Subject: [PATCH] Added flag value checks --- .../plot/AbstractFlag.java | 9 ++ .../intellectualcrafters/plot/PlotMain.java | 144 +++++++++++++++--- .../plot/commands/Set.java | 8 + .../plot/listeners/PlayerEvents.java | 17 +-- 4 files changed, 144 insertions(+), 34 deletions(-) diff --git a/PlotSquared/src/com/intellectualcrafters/plot/AbstractFlag.java b/PlotSquared/src/com/intellectualcrafters/plot/AbstractFlag.java index 3c5755a46..7e3c6004f 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/AbstractFlag.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/AbstractFlag.java @@ -25,6 +25,15 @@ public class AbstractFlag { } this.key = key.toLowerCase(); } + + public String parseValue(String value) { + return value; + } + + public String getValueDesc() { + return "Flag value must be alphanumeric"; + } + /** * AbstractFlag key diff --git a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java index b79c19f08..2ba638d83 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/PlotMain.java @@ -1254,34 +1254,132 @@ public class PlotMain extends JavaPlugin { // Material.CHEST, Material.TRAPPED_CHEST, Material.TRAP_DOOR, // Material.WOOD_DOOR, Material.WOODEN_DOOR, // Material.DISPENSER, Material.DROPPER - public static HashMap materialFlags = new HashMap<>(); + public static HashMap booleanFlags = new HashMap<>(); static { - materialFlags.put(Material.WOODEN_DOOR, "wooden_door"); - materialFlags.put(Material.IRON_DOOR, "iron_door"); - materialFlags.put(Material.STONE_BUTTON, "stone_button"); - materialFlags.put(Material.WOOD_BUTTON, "wooden_button"); - materialFlags.put(Material.LEVER, "lever"); - materialFlags.put(Material.WOOD_PLATE, "wooden_plate"); - materialFlags.put(Material.STONE_PLATE, "stone_plate"); - materialFlags.put(Material.CHEST, "chest"); - materialFlags.put(Material.TRAPPED_CHEST, "trapped_chest"); - materialFlags.put(Material.TRAP_DOOR, "trap_door"); - materialFlags.put(Material.DISPENSER, "dispenser"); - materialFlags.put(Material.DROPPER, "dropper"); + booleanFlags.put(Material.WOODEN_DOOR, "wooden_door"); + booleanFlags.put(Material.IRON_DOOR, "iron_door"); + booleanFlags.put(Material.STONE_BUTTON, "stone_button"); + booleanFlags.put(Material.WOOD_BUTTON, "wooden_button"); + booleanFlags.put(Material.LEVER, "lever"); + booleanFlags.put(Material.WOOD_PLATE, "wooden_plate"); + booleanFlags.put(Material.STONE_PLATE, "stone_plate"); + booleanFlags.put(Material.CHEST, "chest"); + booleanFlags.put(Material.TRAPPED_CHEST, "trapped_chest"); + booleanFlags.put(Material.TRAP_DOOR, "trap_door"); + booleanFlags.put(Material.DISPENSER, "dispenser"); + booleanFlags.put(Material.DROPPER, "dropper"); } private static void defaultFlags() { - for(String str : materialFlags.values()) { - FlagManager.addFlag(new AbstractFlag(str)); - } - List otherFlags = Arrays.asList( - "gamemode", - "weather", - "time" - ); - for(String str : otherFlags) { - FlagManager.addFlag(new AbstractFlag(str)); + for(String str : booleanFlags.values()) { + FlagManager.addFlag(new AbstractFlag(str) { + + @Override + public String parseValue(String value) { + switch (value) { + case "true": + return "true"; + case "yes": + return "true"; + case "1": + return "true"; + case "false": + return "false"; + case "no": + return "false"; + case "0": + return "false"; + default: + return null; + + } + } + + @Override + public String getValueDesc() { + return "Flag value must be a boolean: true, false"; + } + + + }); } + + FlagManager.addFlag(new AbstractFlag("gamemode") { + @Override + public String parseValue(String value) { + switch (value) { + case "creative": + return "creative"; + case "survival": + return "survival"; + case "adventure": + return "adventure"; + case "c": + return "creative"; + case "s": + return "survival"; + case "a": + return "adventure"; + case "1": + return "creative"; + case "0": + return "survival"; + case "2": + return "adventure"; + default: + return null; + } + } + + @Override + public String getValueDesc() { + return "Flag value must be a gamemode: 'creative' , 'survival' or 'adventure'"; + } + }); + + FlagManager.addFlag(new AbstractFlag("time") { + @Override + public String parseValue(String value) { + try { + return Long.parseLong(value)+""; + } + catch (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("weather") { + @Override + public String parseValue(String value) { + switch (value) { + case "rain": + return "rain"; + case "storm": + return "rain"; + case "on": + return "rain"; + case "clear": + return "clear"; + case "sun": + return "clear"; + case "off": + return "clear"; + default: + return null; + } + } + + @Override + public String getValueDesc() { + return "Flag value must be weather type: 'clear' or 'rain'"; + } + }); } public static void addPlotWorld(String world, PlotWorld plotworld, PlotManager manager) { diff --git a/PlotSquared/src/com/intellectualcrafters/plot/commands/Set.java b/PlotSquared/src/com/intellectualcrafters/plot/commands/Set.java index b2776fb7e..f18105ca4 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/commands/Set.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/commands/Set.java @@ -134,10 +134,18 @@ public class Set extends SubCommand { } try { String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " "); + value = af.parseValue(value); + + if (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); return false; } + Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value); PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot); Bukkit.getServer().getPluginManager().callEvent(event); diff --git a/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java index d98101cc7..878567606 100644 --- a/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -141,8 +141,7 @@ public class PlayerEvents implements Listener { private WeatherType getWeatherType(String str) { str = str.toLowerCase(); - List storm = Arrays.asList("storm", "rain", "on"); - if(storm.contains(str)) { + if(str.equals("rain")) { return WeatherType.DOWNFALL; } else { return WeatherType.CLEAR; @@ -150,15 +149,11 @@ public class PlayerEvents implements Listener { } private GameMode getGameMode(String str) { - str = str.toLowerCase(); - List creative = Arrays.asList("creative" , "cr", "1"); - List survival = Arrays.asList("survival" , "su", "0"); - List adventure = Arrays.asList("adventure", "ad", "2"); - if (creative.contains(str)) { + if (str.equals("creative")) { return GameMode.CREATIVE; - } else if (survival.contains(str)) { + } else if (str.equals("survival")) { return GameMode.SURVIVAL; - } else if (adventure.contains(str)) { + } else if (str.equals("adventure")) { return GameMode.ADVENTURE; } else { return Bukkit.getDefaultGameMode(); @@ -564,8 +559,8 @@ public class PlayerEvents implements Listener { // return; // } - if(PlotMain.materialFlags.containsKey(event.getClickedBlock().getType())) { - String flag = PlotMain.materialFlags.get(event.getClickedBlock().getType()); + if(PlotMain.booleanFlags.containsKey(event.getClickedBlock().getType())) { + String flag = PlotMain.booleanFlags.get(event.getClickedBlock().getType()); if(plot.settings.getFlag(flag) != null && getFlagValue(plot.settings.getFlag(flag).getValue())) return; }