Added flag value checks

This commit is contained in:
boy0001 2014-10-19 21:27:27 +11:00
parent e767834144
commit 3796f41be3
4 changed files with 144 additions and 34 deletions

View File

@ -26,6 +26,15 @@ public class AbstractFlag {
this.key = key.toLowerCase(); this.key = key.toLowerCase();
} }
public String parseValue(String value) {
return value;
}
public String getValueDesc() {
return "Flag value must be alphanumeric";
}
/** /**
* AbstractFlag key * AbstractFlag key
* *

View File

@ -1254,34 +1254,132 @@ public class PlotMain extends JavaPlugin {
// Material.CHEST, Material.TRAPPED_CHEST, Material.TRAP_DOOR, // Material.CHEST, Material.TRAPPED_CHEST, Material.TRAP_DOOR,
// Material.WOOD_DOOR, Material.WOODEN_DOOR, // Material.WOOD_DOOR, Material.WOODEN_DOOR,
// Material.DISPENSER, Material.DROPPER // Material.DISPENSER, Material.DROPPER
public static HashMap<Material, String> materialFlags = new HashMap<>(); public static HashMap<Material, String> booleanFlags = new HashMap<>();
static { static {
materialFlags.put(Material.WOODEN_DOOR, "wooden_door"); booleanFlags.put(Material.WOODEN_DOOR, "wooden_door");
materialFlags.put(Material.IRON_DOOR, "iron_door"); booleanFlags.put(Material.IRON_DOOR, "iron_door");
materialFlags.put(Material.STONE_BUTTON, "stone_button"); booleanFlags.put(Material.STONE_BUTTON, "stone_button");
materialFlags.put(Material.WOOD_BUTTON, "wooden_button"); booleanFlags.put(Material.WOOD_BUTTON, "wooden_button");
materialFlags.put(Material.LEVER, "lever"); booleanFlags.put(Material.LEVER, "lever");
materialFlags.put(Material.WOOD_PLATE, "wooden_plate"); booleanFlags.put(Material.WOOD_PLATE, "wooden_plate");
materialFlags.put(Material.STONE_PLATE, "stone_plate"); booleanFlags.put(Material.STONE_PLATE, "stone_plate");
materialFlags.put(Material.CHEST, "chest"); booleanFlags.put(Material.CHEST, "chest");
materialFlags.put(Material.TRAPPED_CHEST, "trapped_chest"); booleanFlags.put(Material.TRAPPED_CHEST, "trapped_chest");
materialFlags.put(Material.TRAP_DOOR, "trap_door"); booleanFlags.put(Material.TRAP_DOOR, "trap_door");
materialFlags.put(Material.DISPENSER, "dispenser"); booleanFlags.put(Material.DISPENSER, "dispenser");
materialFlags.put(Material.DROPPER, "dropper"); booleanFlags.put(Material.DROPPER, "dropper");
} }
private static void defaultFlags() { private static void defaultFlags() {
for(String str : materialFlags.values()) { for(String str : booleanFlags.values()) {
FlagManager.addFlag(new AbstractFlag(str)); FlagManager.addFlag(new AbstractFlag(str) {
}
List<String> otherFlags = Arrays.asList( @Override
"gamemode", public String parseValue(String value) {
"weather", switch (value) {
"time" case "true":
); return "true";
for(String str : otherFlags) { case "yes":
FlagManager.addFlag(new AbstractFlag(str)); 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) { public static void addPlotWorld(String world, PlotWorld plotworld, PlotManager manager) {

View File

@ -134,10 +134,18 @@ public class Set extends SubCommand {
} }
try { try {
String value = StringUtils.join(Arrays.copyOfRange(args, 2, args.length), " "); 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)) { 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], value);
return false; return false;
} }
Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value); Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value);
PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot); PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot);
Bukkit.getServer().getPluginManager().callEvent(event); Bukkit.getServer().getPluginManager().callEvent(event);

View File

@ -141,8 +141,7 @@ public class PlayerEvents implements Listener {
private WeatherType getWeatherType(String str) { private WeatherType getWeatherType(String str) {
str = str.toLowerCase(); str = str.toLowerCase();
List<String> storm = Arrays.asList("storm", "rain", "on"); if(str.equals("rain")) {
if(storm.contains(str)) {
return WeatherType.DOWNFALL; return WeatherType.DOWNFALL;
} else { } else {
return WeatherType.CLEAR; return WeatherType.CLEAR;
@ -150,15 +149,11 @@ public class PlayerEvents implements Listener {
} }
private GameMode getGameMode(String str) { private GameMode getGameMode(String str) {
str = str.toLowerCase(); if (str.equals("creative")) {
List<String> creative = Arrays.asList("creative" , "cr", "1");
List<String> survival = Arrays.asList("survival" , "su", "0");
List<String> adventure = Arrays.asList("adventure", "ad", "2");
if (creative.contains(str)) {
return GameMode.CREATIVE; return GameMode.CREATIVE;
} else if (survival.contains(str)) { } else if (str.equals("survival")) {
return GameMode.SURVIVAL; return GameMode.SURVIVAL;
} else if (adventure.contains(str)) { } else if (str.equals("adventure")) {
return GameMode.ADVENTURE; return GameMode.ADVENTURE;
} else { } else {
return Bukkit.getDefaultGameMode(); return Bukkit.getDefaultGameMode();
@ -564,8 +559,8 @@ public class PlayerEvents implements Listener {
// return; // return;
// } // }
if(PlotMain.materialFlags.containsKey(event.getClickedBlock().getType())) { if(PlotMain.booleanFlags.containsKey(event.getClickedBlock().getType())) {
String flag = PlotMain.materialFlags.get(event.getClickedBlock().getType()); String flag = PlotMain.booleanFlags.get(event.getClickedBlock().getType());
if(plot.settings.getFlag(flag) != null && getFlagValue(plot.settings.getFlag(flag).getValue())) if(plot.settings.getFlag(flag) != null && getFlagValue(plot.settings.getFlag(flag).getValue()))
return; return;
} }