From 5dc3009e6b7e3bd013c773aca27c5137559c3d43 Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Sat, 12 Oct 2013 12:51:14 -0400 Subject: [PATCH] Fix potential problem in flag. --- .../aufdemrand/denizen/objects/Duration.java | 2 +- .../aufdemrand/denizen/objects/Element.java | 1 + .../aufdemrand/denizen/objects/dPlayer.java | 2 +- .../aufdemrand/denizen/objects/dScript.java | 2 +- .../aufdemrand/denizen/objects/dWorld.java | 2 +- .../scripts/commands/core/FlagCommand.java | 32 ++++++++++++------- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/Duration.java b/src/main/java/net/aufdemrand/denizen/objects/Duration.java index 88141e7f71..6431e5b14e 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Duration.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Duration.java @@ -68,7 +68,7 @@ public class Duration implements dObject { // <--[language] // @name d@ // @description - // d@ refers to the object type of a 'Duration'. The 'd@' is notation for Denizen's Object + // d@ refers to the 'object identifier' of a 'Duration'. The 'd@' is notation for Denizen's Object // Fetcher. Durations must be a positive number or range of numbers followed optionally by // a unit of time, and prefixed by d@. Examples: d@3s, d@1d, d@10s-20s. // diff --git a/src/main/java/net/aufdemrand/denizen/objects/Element.java b/src/main/java/net/aufdemrand/denizen/objects/Element.java index 0154b6ce54..45b30976da 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Element.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Element.java @@ -42,6 +42,7 @@ public class Element implements dObject { public final static Element TRUE = new Element(Boolean.TRUE); public final static Element FALSE = new Element(Boolean.FALSE); + public final static Element SERVER = new Element("server"); final static Pattern VALUE_PATTERN = Pattern.compile("el@val(?:ue)?\\[([^\\[\\]]+)\\].*", diff --git a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java index e94501cb71..c32dae5c68 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java @@ -42,7 +42,7 @@ public static dPlayer mirrorBukkitPlayer(OfflinePlayer player) { // <--[language] // @name p@ // @description - // p@ refers to the object type of a dPlayer. The 'p@' is notation for Denizen's Object + // p@ refers to the 'object identifier' of a dPlayer. The 'p@' is notation for Denizen's Object // Fetcher. The only valid constructor for a dPlayer is the name of the player the object should be // associated with. For example, to reference the player named 'mythan', use p@mythan. Player names // are case insensitive. diff --git a/src/main/java/net/aufdemrand/denizen/objects/dScript.java b/src/main/java/net/aufdemrand/denizen/objects/dScript.java index 168f9cab90..a6979d8dbe 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dScript.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dScript.java @@ -53,7 +53,7 @@ public class dScript implements dObject { // <--[language] // @name s@ // @description - // s@ refers to the object type of a dScript. The 's@' is notation for Denizen's Object + // s@ refers to the 'object identifier' of a dScript. The 's@' is notation for Denizen's Object // Fetcher. The only valid constructor for a dScript is the name of the script container that it should be // associated with. For example, if my script container is called 'cool_script', the dScript object for that script // would be able to be referenced (fetched) with s@cool_script. diff --git a/src/main/java/net/aufdemrand/denizen/objects/dWorld.java b/src/main/java/net/aufdemrand/denizen/objects/dWorld.java index 94a9bd5ff4..a2b7705a3b 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dWorld.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dWorld.java @@ -37,7 +37,7 @@ public static dWorld mirrorBukkitWorld(World world) { // <--[language] // @name w@ // @description - // w@ refers to the object type of a dWorld. The 'w@' is notation for Denizen's Object + // w@ refers to the 'object identifier' of a dWorld. The 'w@' is notation for Denizen's Object // Fetcher. The only valid constructor for a dWorld is the name of the world it should be // associated with. For example, to reference the world named 'world1', use w@world1. // World names are case insensitive. diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/FlagCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/FlagCommand.java index 34903b40ad..7b4f58a2ad 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/FlagCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/FlagCommand.java @@ -21,6 +21,12 @@ public class FlagCommand extends AbstractCommand implements Listener { + // <--[example] + // + // + // + // + @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { @@ -45,7 +51,7 @@ else if (!scriptEntry.hasObject("flag_target") } else if (!scriptEntry.hasObject("flag_target") && arg.matches("global, server")) { specified_target = true; - scriptEntry.addObject("flag_target", new Element("server")); + scriptEntry.addObject("flag_target", Element.SERVER); } else if (!scriptEntry.hasObject("flag_target") && arg.matches("player")) { @@ -53,32 +59,35 @@ else if (!scriptEntry.hasObject("flag_target") scriptEntry.addObject("flag_target", scriptEntry.getPlayer()); } - // Allow a p@player or n@npc entity to specify the target - // to be flagged + // Allow a p@player or n@npc entity to specify the target to be flagged. + // Don't check if the player/npc is valid until after the argument + // is being process to make sure the objects don't accidently get set + // as the name of the flag.. else if (!scriptEntry.hasObject("flag_target") - && arg.matchesArgumentType(dNPC.class) && arg.startsWith("n@")) { + if (dNPC.valueOf(arg.getValue()) == null) + throw new InvalidArgumentsException("Invalid NPC target."); specified_target = true; scriptEntry.addObject("flag_target", arg.asType(dNPC.class)); } else if (!scriptEntry.hasObject("flag_target") - && arg.matchesArgumentType(dPlayer.class) && arg.startsWith("p@")) { + if (dPlayer.valueOf(arg.getValue()) == null) + throw new InvalidArgumentsException("Invalid Player target."); specified_target = true; scriptEntry.addObject("flag_target", arg.asType(dPlayer.class)); } + // Check if setting a boolean - else if (!scriptEntry.hasObject("action") - && arg.raw_value.split(":", 3).length == 1) { + else if (arg.raw_value.split(":", 3).length == 1) { scriptEntry.addObject("action", FlagManager.Action.SET_BOOLEAN); scriptEntry.addObject("value", Element.TRUE); scriptEntry.addObject("flag_name", arg.asElement()); } // Check for flag_name:value/action - else if (!scriptEntry.hasObject("action") - && arg.raw_value.split(":", 3).length == 2) { + else if (arg.raw_value.split(":", 3).length == 2) { String[] flagArgs = arg.raw_value.split(":", 2); scriptEntry.addObject("flag_name", new Element(flagArgs[0].toUpperCase())); @@ -107,8 +116,7 @@ else if (flagArgs[1].equals("<-")) { } // Check for flag_name:action:value - else if (!scriptEntry.hasObject("action") - && arg.raw_value.split(":", 3).length == 3) { + else if (arg.raw_value.split(":", 3).length == 3) { String[] flagArgs = arg.raw_value.split(":", 3); scriptEntry.addObject("flag_name", new Element(flagArgs[0].toUpperCase())); scriptEntry.addObject("value", new Element(flagArgs[2])); @@ -135,7 +143,7 @@ else if (flagArgs[1].startsWith("/")) scriptEntry.addObject("action", FlagManager.Action.DIVIDE); } - else dB.echoDebug("Unhandled argument: " + arg.raw_value); + else arg.reportUnhandled(); } // Set defaults