From ce7c479bc13465f332750d8c310c41e9a4166e2d Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Mon, 8 Apr 2013 14:41:55 -0400 Subject: [PATCH] Clean up argumentHelper, add/fix attributes in dEntity/dWorld --- .../denizen/utilities/arguments/aH.java | 64 ++++++++------- .../denizen/utilities/arguments/dEntity.java | 77 +++++++++++++++++-- .../denizen/utilities/arguments/dWorld.java | 29 ++++--- 3 files changed, 124 insertions(+), 46 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/aH.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/aH.java index d390e5d52a..f0060f49c2 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/aH.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/aH.java @@ -7,9 +7,12 @@ import net.aufdemrand.denizen.utilities.debugging.dB; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; +import net.minecraft.server.v1_5_R2.Entity; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.OfflinePlayer; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_5_R2.CraftWorld; import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -42,17 +45,20 @@ public static String debugUniqueObj(String prefix, String id, String value) { final static Pattern doublePtrn = Pattern.compile("(-)?(?:(?:\\d+)|)(?:(?:\\.\\d+)|)"); final static Pattern floatPtrn = Pattern.compile("^[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?$"); final static Pattern integerPtrn = Pattern.compile("(-)?\\d+"); - final static Pattern locationPattern = Pattern.compile("location:((-)?\\d+(\\.\\d+)?,){3}\\w+", Pattern.CASE_INSENSITIVE); + final static Pattern locationPattern = + Pattern.compile("location:((-)?\\d+(\\.\\d+)?,){3}\\w+", Pattern.CASE_INSENSITIVE); final static Pattern wordPtrn = Pattern.compile("\\w+"); - final static Pattern matchesDurationPtrn = Pattern.compile("duration:(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)", Pattern.CASE_INSENSITIVE); + final static Pattern matchesDurationPtrn = + Pattern.compile("duration:(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)", Pattern.CASE_INSENSITIVE); final static Pattern matchesEntityPtrn = Pattern.compile("(?:.+?|):((ENTITY\\.|PLAYER\\.|NPC\\.).+)|(PLAYER|NPC)", Pattern.CASE_INSENSITIVE); final static Pattern matchesQuantityPtrn = Pattern.compile("qty:(-)?\\d+", Pattern.CASE_INSENSITIVE); final static Pattern matchesQueuePtrn = Pattern.compile("queue:(.+)", Pattern.CASE_INSENSITIVE); - + + /** *

Used to determine if a argument string matches a non-valued custom argument. - * If a dScript valued argument (such as PLAYER:NAME) is passed, this method + * If a dScript valued argument (such as TARGET:NAME) is passed, this method * will always return false. Also supports multiple argument names, separated by a * comma (,) character. This method will trim() each name specified.

* @@ -182,7 +188,7 @@ public static boolean matchesValueArg(String names, String arg, ArgumentType typ dB.echoError("While parsing '" + arg + "', Denizen has run into a problem. While the " + "prefix is correct, the value is not valid. Check documentation for valid value." + - "Perhaps a replaceable Tag has failed to fill in a value?"); + "Perhaps a replaceable tag has failed to fill in a value?"); return false; } @@ -199,8 +205,8 @@ public static boolean matchesValueArg(String names, String arg, ArgumentType typ * 'arg' will return false. * * - * @param arg the argument to check - * @return true or false + * @param arg the argument to check + * @return true or false * */ public static boolean getBooleanFrom(String arg) { @@ -215,10 +221,10 @@ public static boolean getBooleanFrom(String arg) { * * Examples: *
    - * 'LEVEL:3.5' will return '3.5D'.
    - * 'INT:1' will return '1D'.
    - * '1950' will return '1950D'.
    - * '-.377' will return '-0.377D'.
    + * 'LEVEL:3.5' will return '3.5'.
    + * 'INT:1' will return '1.0'.
    + * '1950' will return '1950.0'.
    + * '-.377' will return '-0.377'.
    *
* * @param arg the argument to check @@ -236,15 +242,12 @@ public static double getDoubleFrom(String arg) { /** *

Returns a Bukkit EntityType from a dScript argument string. Also accounts * for the argument prefix being passed along, for convenience. Though the - * matchesEntity(...) requires an ITEM: prefix, this method + * matchesEntity(...) requires an ENTITY: prefix, this method * does not, so it can be used in a CustomValueArg.

* - *

Provides a line of dB output if returning null. For getting saved entities - * make with the 'NEW ENTITY Command', use {@link #getLivingEntityFrom(String)}

- * * Examples: *
    - * 'zombie' will return 'EntityType.Zombie'.
    + * 'zombie' will return 'EntityType.ZOMBIE'.
    * 'monster:skeleton' will return 'EntityType.SKELETON'.
    * '1983' will return 'null'.
    *
@@ -273,25 +276,32 @@ public static LivingEntity getLivingEntityFrom(String arg) { Matcher m = matchesEntityPtrn.matcher(arg); if (m.matches()) { String entityGroup = m.group(1); - String entityGroupUpper =entityGroup.toUpperCase(); - if (entityGroupUpper.startsWith("ENTITY.")) { - LivingEntity returnable = ((Denizen) Bukkit.getPluginManager().getPlugin("Denizen")) - .getCommandRegistry().get(NewCommand.class).getEntity(entityGroup.split("\\.")[1]); - if (returnable != null) return returnable; - else dB.echoError("Invalid entity! '" + entityGroup + "' could not be found."); + String entityGroupUpper = entityGroup.toUpperCase(); + if (entityGroupUpper.startsWith("ENTITY.") + || entityGroupUpper.startsWith("@E.")) { + int entityID = Integer.valueOf(entityGroup.split("\\.")[1]); + Entity entity = null; + for (World world : Bukkit.getWorlds()) { + entity = ((CraftWorld) world).getHandle().getEntity(entityID); + if (entity != null) break; + } + if (entity != null) return (LivingEntity) entity.getBukkitEntity(); + else dB.echoError("Invalid entity! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); } - else if (entityGroupUpper.startsWith("NPC.") || - entityGroupUpper.startsWith("NPCID.")) { + else if (entityGroupUpper.startsWith("NPC.") + || entityGroupUpper.startsWith("NPCID.") + || entityGroupUpper.startsWith("@N.")) { LivingEntity returnable = CitizensAPI.getNPCRegistry().getById(Integer.valueOf(entityGroup.split("\\.")[1])).getBukkitEntity(); if (returnable != null) return returnable; - else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found."); + else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); } - else if (entityGroupUpper.startsWith("PLAYER.")) { + else if (entityGroupUpper.startsWith("PLAYER.") + || entityGroupUpper.startsWith("@P.")) { LivingEntity returnable = getPlayerFrom(entityGroup.split("\\.")[1]); if (returnable != null) return returnable; - else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found."); + else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?"); } } diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java index 6c819bb2ca..79b4381380 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java @@ -4,12 +4,19 @@ import net.aufdemrand.denizen.tags.Attribute; import net.aufdemrand.denizen.utilities.DenizenAPI; import net.aufdemrand.denizen.utilities.debugging.dB; +import net.citizensnpcs.api.CitizensAPI; +import net.minecraft.server.v1_5_R2.Entity; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_5_R2.CraftWorld; import org.bukkit.entity.LivingEntity; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class dEntity implements dScriptArgument { @@ -23,7 +30,7 @@ public class dEntity implements dScriptArgument { */ public static dEntity getSavedEntity(String id) { if (entities.containsKey(id.toUpperCase())) - return entities.get(id.toUpperCase()); + return entities.get(id.toUpperCase()); else return null; } @@ -60,15 +67,69 @@ public static void _saveEntities() { } /** - * Gets a Item Object from a string form. + * Gets a dEntity Object from a string form.
+ *
+ * n@13 will return NPC 13
+ * e@5884 will return the entity with the entityid of 5884
+ * p@aufdemrand will return the player object of aufdemrand
+ *
+ * Note that the NPCs, Entities, and Players must be spawned, + * one coincidentally Players must be logged in.
+ * * * @param string the string or dScript argument String - * @return an Item, or null if incorrectly formatted + * @return a dEntity, or null * */ + @ObjectFetcher("e") public static dEntity valueOf(String string) { - // Create entity! + // Make sure string matches what this interpreter can accept. + final Pattern matchesEntityPtrn = + Pattern.compile("(?:.+?:|)((n@|e@|p@|)(.+))", + Pattern.CASE_INSENSITIVE); + + Matcher m = matchesEntityPtrn.matcher(string); + + if (m.matches()) { + String entityGroup = m.group(1); + String entityGroupUpper = entityGroup.toUpperCase(); + + // TODO: Deprecate NPC./NPCID. + if (entityGroupUpper.startsWith("N@")) { + LivingEntity returnable = CitizensAPI.getNPCRegistry() + .getById(Integer.valueOf(m.group(4))).getBukkitEntity(); + + if (returnable != null) return new dEntity(returnable); + else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); + } + + // TODO: Deprecate PLAYER. + else if (entityGroupUpper.startsWith("P@")) { + LivingEntity returnable = aH.getPlayerFrom(m.group(4)); + + if (returnable != null) new dEntity(returnable); + else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?"); + } + + // Assume entity + else { + + if (aH.matchesInteger(m.group(4))) { + int entityID = Integer.valueOf(m.group(4)); + Entity entity = null; + + for (World world : Bukkit.getWorlds()) { + entity = ((CraftWorld) world).getHandle().getEntity(entityID); + if (entity != null) break; + } + + if (entity != null) return new dEntity((LivingEntity) entity.getBukkitEntity()); + } + // Got this far? Invalid entity. + dB.echoError("Invalid entity! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); + } + } return null; } @@ -139,7 +200,7 @@ public dScriptArgument setPrefix(String prefix) { } public LivingEntity getEntity(String string) { - return entity; + return entity; } @Override @@ -147,8 +208,10 @@ public String getAttribute(Attribute attribute) { if (attribute == null) return null; - // Desensitize the attribute for comparison - String id = this.id.toLowerCase(); + if (entity == null) { + dB.echoDebug("dEntity has returned null."); + return "null"; + } if (attribute.startsWith("name")) return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1)); diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dWorld.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dWorld.java index 2f42d31073..9517fa9ef6 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dWorld.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dWorld.java @@ -2,17 +2,16 @@ import net.aufdemrand.denizen.interfaces.dScriptArgument; import net.aufdemrand.denizen.tags.Attribute; -import net.aufdemrand.denizen.tags.core.PlayerTags; import net.aufdemrand.denizen.utilities.debugging.dB; -import net.aufdemrand.denizen.utilities.depends.Depends; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class dWorld implements dScriptArgument { @@ -22,20 +21,26 @@ public class dWorld implements dScriptArgument { * @return a dScript dList * */ + @ObjectFetcher("w") public static dWorld valueOf(String string) { if (string == null) return null; - String prefix = null; - // Strip prefix (ie. targets:...) - if (string.split(":").length > 1) { - prefix = string.split(":", 2)[0]; - string = string.split(":", 2)[1]; - } + // Make sure string matches what this interpreter can accept. + final Pattern world_object_pattern = + Pattern.compile("(.+?:|)((w@|)(.+))", + Pattern.CASE_INSENSITIVE); + + Matcher m = world_object_pattern.matcher(string); - for (World world : Bukkit.getWorlds()) - if (world.getName().equalsIgnoreCase(string)) return new dWorld(prefix, world); + if (m.matches()) { + String prefix = m.group(1); + String world_name = m.group(2).split("@")[1]; + + for (World world : Bukkit.getWorlds()) + if (world.getName().equalsIgnoreCase(world_name)) return new dWorld(prefix, world); + } - dB.echoError("World '" + string + "' is invalid or does not exist."); + dB.echoError("World '" + m.group(2) + "' is invalid or does not exist."); return null; }