From 07287f5ca7df04040d34a10ca50307dd0c938507 Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Thu, 16 May 2013 21:56:02 -0400 Subject: [PATCH] More updates to OO tag system --- .../aufdemrand/denizen/flags/FlagManager.java | 2 + .../denizen/interfaces/dScriptArgument.java | 24 +- .../scripts/commands/core/EngraveCommand.java | 22 +- .../scripts/commands/core/GiveCommand.java | 4 +- .../core/EntityScriptContainer.java | 6 +- .../containers/core/ItemScriptContainer.java | 4 +- .../denizen/tags/core/PlayerTags.java | 12 +- .../denizen/utilities/arguments/dEntity.java | 215 ++++++++++++------ .../denizen/utilities/arguments/dItem.java | 117 +++++----- .../denizen/utilities/arguments/dList.java | 58 +++-- .../utilities/arguments/dLocation.java | 91 +++++--- .../nbt/{NBTItem.java => CustomNBT.java} | 2 +- 12 files changed, 345 insertions(+), 212 deletions(-) rename src/main/java/net/aufdemrand/denizen/utilities/nbt/{NBTItem.java => CustomNBT.java} (99%) diff --git a/src/main/java/net/aufdemrand/denizen/flags/FlagManager.java b/src/main/java/net/aufdemrand/denizen/flags/FlagManager.java index 766bf8fc06..38bdeb0b80 100644 --- a/src/main/java/net/aufdemrand/denizen/flags/FlagManager.java +++ b/src/main/java/net/aufdemrand/denizen/flags/FlagManager.java @@ -354,6 +354,8 @@ public void save() { @Override public String toString() { checkExpired(); + // Possibly use reflection to check whether dList or dElement is calling this? + // If dList, return fl@..., if dElement, return f@... return (flagOwner == null ? "fl@" + flagName : "fl[" + flagOwner + "]@" + flagName); } diff --git a/src/main/java/net/aufdemrand/denizen/interfaces/dScriptArgument.java b/src/main/java/net/aufdemrand/denizen/interfaces/dScriptArgument.java index 7e96550ded..59a529bbe4 100644 --- a/src/main/java/net/aufdemrand/denizen/interfaces/dScriptArgument.java +++ b/src/main/java/net/aufdemrand/denizen/interfaces/dScriptArgument.java @@ -11,6 +11,7 @@ public interface dScriptArgument { */ public String getPrefix(); + /** *

Gets a standard dB representation of this argument.

* @@ -25,10 +26,26 @@ public interface dScriptArgument { */ public String debug(); + + /** + * Determines if this argument object is unique. This typically stipulates + * that this object has been named, or has some unique identifier that + * Denizen can use to recall it. + * + * @return true if this object is unique, false if it is a 'singleton generic argument/object' + */ public boolean isUnique(); + + /** + * Returns the string type of the object. This is fairly verbose and crude, but used with + * a basic dScriptArg attribute. + * + * @return a straight-up string description of the type of dScriptArg. ie. dList, dLocation + */ public String getType(); + /** * Gets an ugly, but exact, string representation of this dScriptArgument. * While not specified in the dScriptArgument Interface, this value should be @@ -38,6 +55,7 @@ public interface dScriptArgument { */ public String identify(); + /** * Sets the prefix for this argument, otherwise uses the default. * @@ -45,12 +63,14 @@ public interface dScriptArgument { */ public dScriptArgument setPrefix(String prefix); + /** - * Gets a string-friendly specific attribute of this object. + * Gets a specific attribute using this object to fetch the necessary data. * * @param attribute the name of the attribute - * @return a string attribute + * @return a string result of the fetched attribute */ public String getAttribute(Attribute attribute); + } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/EngraveCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/EngraveCommand.java index 390e842606..1f5805d0a3 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/EngraveCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/EngraveCommand.java @@ -3,7 +3,7 @@ import java.util.HashMap; import java.util.Map; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.ItemDespawnEvent; @@ -77,13 +77,13 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { switch (action) { case ADD: dB.echoDebug("Engraving '" + item.getType() + "' with an inscription of '" + target + "'."); - NBTItem.addCustomNBT(item, "owner", target); - dB.echoDebug(NBTItem.getCustomNBT(item, "owner")); + CustomNBT.addCustomNBT(item, "owner", target); + dB.echoDebug(CustomNBT.getCustomNBT(item, "owner")); return; case REMOVE: dB.echoDebug("Removing engraving on '" + item.getType() + "'."); - NBTItem.removeCustomNBT(item, "owner"); + CustomNBT.removeCustomNBT(item, "owner"); return; } } @@ -103,9 +103,9 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { @EventHandler public void checkOwner(PlayerPickupItemEvent event) { ItemStack item = event.getItem().getItemStack(); - if (NBTItem.hasCustomNBT(item, "owner") - && !NBTItem.getCustomNBT(item, "owner").equalsIgnoreCase(event.getPlayer().getName())) { - dB.echoDebug(NBTItem.getCustomNBT(item, "owner")); + if (CustomNBT.hasCustomNBT(item, "owner") + && !CustomNBT.getCustomNBT(item, "owner").equalsIgnoreCase(event.getPlayer().getName())) { + dB.echoDebug(CustomNBT.getCustomNBT(item, "owner")); // See why item isn't being picked up if sneaking. if (event.getPlayer().isSneaking()) { @@ -140,8 +140,8 @@ public void checkOwner(PlayerPickupItemEvent event) { public void stopDespawn(ItemDespawnEvent event) { ItemStack item = event.getEntity().getItemStack(); // Check if the item has an engraving, otherwise carry on. - if (NBTItem.hasCustomNBT(item, "owner")) { - dB.echoDebug(NBTItem.getCustomNBT(item, "owner")); + if (CustomNBT.hasCustomNBT(item, "owner")) { + dB.echoDebug(CustomNBT.getCustomNBT(item, "owner")); // If in the delay map if (despawnDelay.containsKey(event.getEntity().getEntityId())) { @@ -150,12 +150,12 @@ public void stopDespawn(ItemDespawnEvent event) { event.setCancelled(true); else // If cooled, remove from map. - dB.echoDebug("Removed an ENGRAVED '" + item.getType().name() + "' which belonged to '" + NBTItem.getCustomNBT(item, "owner") + "'."); + dB.echoDebug("Removed an ENGRAVED '" + item.getType().name() + "' which belonged to '" + CustomNBT.getCustomNBT(item, "owner") + "'."); despawnDelay.remove(event.getEntity().getEntityId()); } else { // If not in delay map, add to delay map and cancel despawn. event.setCancelled(true); - dB.echoDebug("Stopped despawn of an ENGRAVED '" + item + "' which belonged to '" + NBTItem.getCustomNBT(item, "owner") + "'. Will remove from world in 10 minutes."); + dB.echoDebug("Stopped despawn of an ENGRAVED '" + item + "' which belonged to '" + CustomNBT.getCustomNBT(item, "owner") + "'. Will remove from world in 10 minutes."); despawnDelay.put(event.getEntity().getEntityId(), System.currentTimeMillis() + (1000 * 60 * 10)); } } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java index c34d5b60dc..eace394dfe 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/GiveCommand.java @@ -8,7 +8,7 @@ import net.aufdemrand.denizen.utilities.arguments.aH; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.depends.Depends; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import org.bukkit.inventory.ItemStack; @@ -108,7 +108,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { case ITEM: ItemStack is = item.getItemStack(); is.setAmount(amt.intValue()); - if(engrave) is = NBTItem.addCustomNBT(item.getItemStack(), "owner", scriptEntry.getPlayer().getName()); + if(engrave) is = CustomNBT.addCustomNBT(item.getItemStack(), "owner", scriptEntry.getPlayer().getName()); HashMap leftovers = scriptEntry.getPlayer().getInventory().addItem(is); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/EntityScriptContainer.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/EntityScriptContainer.java index 16151e79e8..d0139ebf07 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/EntityScriptContainer.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/EntityScriptContainer.java @@ -25,7 +25,11 @@ public dEntity getEntityFrom(Player player, dNPC npc) { dEntity entity = null; try { // Check validity of material - if (contains("TYPE")){ } + if (contains("TYPE")){ + + // TODO: + + } // Set Id of the stack diff --git a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/ItemScriptContainer.java b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/ItemScriptContainer.java index 627d599386..40e48fdb17 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/containers/core/ItemScriptContainer.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/containers/core/ItemScriptContainer.java @@ -9,8 +9,8 @@ import net.aufdemrand.denizen.tags.TagManager; import net.aufdemrand.denizen.utilities.arguments.dItem; import net.aufdemrand.denizen.utilities.debugging.dB; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import net.aufdemrand.denizen.utilities.nbt.LeatherColorer; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; @@ -100,7 +100,7 @@ public dItem getItemFrom(Player player, dNPC npc) { } // Set Id of the stack - stack.setItemStack(NBTItem.addCustomNBT(stack.getItemStack(), "denizen-script-id", getName())); + stack.setItemStack(CustomNBT.addCustomNBT(stack.getItemStack(), "denizen-script-id", getName())); } catch (Exception e) { dB.echoError("Woah! An exception has been called with this item script!"); diff --git a/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java b/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java index 05388fcfdd..2981d59d80 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java +++ b/src/main/java/net/aufdemrand/denizen/tags/core/PlayerTags.java @@ -8,7 +8,7 @@ import net.aufdemrand.denizen.utilities.arguments.*; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.depends.Depends; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.World; @@ -185,19 +185,19 @@ else if (subType.equalsIgnoreCase("DATA")) else if (subType.equalsIgnoreCase("MAX_STACK")) event.setReplaced(String.valueOf(p.getItemInHand().getMaxStackSize())); else if (subType.equalsIgnoreCase("OWNER")) { - if (NBTItem.hasCustomNBT(p.getItemInHand(), "owner")) - event.setReplaced(NBTItem.getCustomNBT(p.getItemInHand(), "owner")); + if (CustomNBT.hasCustomNBT(p.getItemInHand(), "owner")) + event.setReplaced(CustomNBT.getCustomNBT(p.getItemInHand(), "owner")); } else if (subType.equalsIgnoreCase("ENCHANTMENTS")) { String enchantments = null; if (specifier.equalsIgnoreCase("LEVELS")) - enchantments = NBTItem.getEnchantments(p.getItemInHand()).asDScriptListWithLevels(); + enchantments = CustomNBT.getEnchantments(p.getItemInHand()).asDScriptListWithLevels(); else if (specifier.equalsIgnoreCase("LEVELS_ONLY")) - enchantments = NBTItem.getEnchantments(p.getItemInHand()).asDScriptListLevelsOnly(); + enchantments = CustomNBT.getEnchantments(p.getItemInHand()).asDScriptListLevelsOnly(); else - enchantments = NBTItem.getEnchantments(p.getItemInHand()).asDScriptList(); + enchantments = CustomNBT.getEnchantments(p.getItemInHand()).asDScriptList(); if (enchantments != null && enchantments.length() > 0) event.setReplaced(enchantments); 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 399b7ebfb7..c55407958a 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dEntity.java @@ -3,10 +3,9 @@ import net.aufdemrand.denizen.interfaces.dScriptArgument; import net.aufdemrand.denizen.scripts.ScriptRegistry; import net.aufdemrand.denizen.scripts.containers.core.EntityScriptContainer; -import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer; import net.aufdemrand.denizen.tags.Attribute; import net.aufdemrand.denizen.utilities.debugging.dB; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import net.citizensnpcs.api.CitizensAPI; import net.minecraft.server.v1_5_R2.Entity; import org.bukkit.Bukkit; @@ -17,6 +16,8 @@ import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.inventory.EntityEquipment; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; @@ -25,59 +26,62 @@ public class dEntity implements dScriptArgument { + ///////////////////// // STATIC METHODS ///////////////// - public static Map entities = new HashMap(); + public static Map uniqueObjects = new HashMap(); - /** - * Gets a saved location based on an Id. - * - * @param id the Id key of the location - * @return the Location associated - */ - public static dEntity getSavedEntity(String id) { - if (entities.containsKey(id.toUpperCase())) - return entities.get(id.toUpperCase()); - else return null; + public static boolean isSaved(String id) { + return uniqueObjects.containsKey(id.toUpperCase()); } - public static void saveEntityAs(dEntity entity, String id) { - if (entity == null) return; - entities.put(id.toUpperCase(), entity); + public static boolean isSaved(dEntity entity) { + return uniqueObjects.containsValue(entity); } - /** - * Checks if there is a saved item with this Id. - * - * @param id the Id to check - * @return true if it exists, false if not - */ - public static boolean isSavedEntity(String id) { - return entities.containsKey(id.toUpperCase()); + public static dEntity getSaved(String id) { + if (uniqueObjects.containsKey(id.toUpperCase())) + return uniqueObjects.get(id.toUpperCase()); + else return null; } - public static String isSavedEntity(dEntity entity) { - for (Map.Entry i : entities.entrySet()) + public static String getSaved(dEntity entity) { + for (Map.Entry i : uniqueObjects.entrySet()) if (i.getValue() == entity) return i.getKey(); return null; } + public static void saveAs(dEntity entity, String id) { + if (entity == null) return; + uniqueObjects.put(id.toUpperCase(), entity); + } + + public static void remove(String id) { + uniqueObjects.remove(id.toUpperCase()); + } + + + ////////////////// + // OBJECT FETCHER + //////////////// + /** - * Gets a dEntity Object from a string form.
+ * Gets a dEntity Object from a string form.
*
- * n@13 will return the entity object of NPC 13
- * e@5884 will return the entity with the entityid of 5884
- * p@aufdemrand will return the entity object of aufdemrand
+ * Unique dEntities:
+ * n@13 will return the entity object of NPC 13
+ * e@5884 will return the entity object for the entity with the entityid of 5884
+ * e@jimmys_pet will return the saved entity object for the id 'jimmys pet'
+ * p@aufdemrand will return the entity object for aufdemrand
*
- * Note that the NPCs, Entities, and Players must be spawned, - * one coincidentally Players must be logged in.
- * + * New dEntities:
+ * zombie will return an unspawned Zombie dEntity
+ * super_creeper will return an unspawned custom 'Super_Creeper' dEntity
* * @param string the string or dScript argument String * @return a dEntity, or null - * */ @ObjectFetcher("e") public static dEntity valueOf(String string) { @@ -92,26 +96,29 @@ public static dEntity valueOf(String string) { Pattern.CASE_INSENSITIVE); Matcher m; - m = entity_by_id.matcher(string); if (m.matches()) { String entityGroup = m.group(1); String entityGroupUpper = entityGroup.toUpperCase(); + // NPC entity if (entityGroupUpper.startsWith("N@")) { LivingEntity returnable = CitizensAPI.getNPCRegistry() .getById(Integer.valueOf(m.group(3))).getBukkitEntity(); if (returnable != null) return new dEntity(returnable); - else dB.echoError("Invalid NPC! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); + else dB.echoError("Invalid NPC! '" + entityGroup + + "' could not be found. Has it been despawned or killed?"); } + // Player entity else if (entityGroupUpper.startsWith("P@")) { LivingEntity returnable = aH.getPlayerFrom(m.group(3)); if (returnable != null) new dEntity(returnable); - else dB.echoError("Invalid Player! '" + entityGroup + "' could not be found. Has the player logged off?"); + else dB.echoError("Invalid Player! '" + entityGroup + + "' could not be found. Has the player logged off?"); } // Assume entity @@ -127,26 +134,31 @@ else if (entityGroupUpper.startsWith("P@")) { if (entity != null) return new dEntity((LivingEntity) entity.getBukkitEntity()); } - else if (isSavedEntity(m.group(3))) - return getSavedEntity(m.group(3)); + else if (isSaved(m.group(3))) + return getSaved(m.group(3)); // Got this far? Invalid entity. - dB.echoError("Invalid entity! '" + entityGroup + "' could not be found. Has it been despawned or killed?"); + dB.echoError("Invalid entity! '" + entityGroup + + "' could not be found. Has it been despawned or killed?"); } } //////// - // Match EntityType + // Match Entity_Type string = string.replace("e@", ""); for (EntityType type : EntityType.values()) { if (type.name().equalsIgnoreCase(string)) + // Construct a new 'vanilla' unspawned dEntity return new dEntity(type); } - if (ScriptRegistry.containsScript(m.group(1), ItemScriptContainer.class)) { - // Get item from script + //////// + // Match Custom Entity + + if (ScriptRegistry.containsScript(m.group(1), EntityScriptContainer.class)) { + // Construct a new custom unspawned entity from script return ScriptRegistry.getScriptContainerAs(m.group(1), EntityScriptContainer.class).getEntityFrom(); } @@ -155,39 +167,108 @@ else if (isSavedEntity(m.group(3))) ///////////////////// - // INSTANCE METHODS - ///////////////// - - private LivingEntity entity; - private EntityType entity_type; + // CONSTRUCTORS + ////////////////// public dEntity(LivingEntity entity) { - this.entity = entity; + if (entity != null) { + this.entity = entity; + this.entity_type = entity.getType(); + } else dB.echoError("Entity referenced is null!"); } public dEntity(EntityType entityType) { - this.entity = null; - this.entity_type = entityType; + if (entityType != null) { + this.entity = null; + this.entity_type = entityType; + } else dB.echoError("Entity_type referenced is null!"); } - public void spawnAt(Location location) { - entity = (LivingEntity) location.getWorld().spawnEntity(location, entity_type); - } + + ///////////////////// + // INSTANCE FIELDS/METHODS + ///////////////// + + private LivingEntity entity = null; + private EntityType entity_type = null; + private DespawnedEntity despawned_entity = null; public LivingEntity getBukkitEntity() { return entity; } - public boolean isAlive() { + public void spawnAt(Location location) { + // If the entity is already spawned, teleport it. + if (entity != null) entity.teleport(location); + + else { + if (entity_type != null) { + if (despawned_entity != null) { + // If entity had a custom_script, use the script to rebuild the base entity. + if (despawned_entity.custom_script != null) + { } // Build entity from custom script + // Else, use the entity_type specified/remembered + else entity = (LivingEntity) location.getWorld().spawnEntity(location, entity_type); + + entity.teleport(location); + entity.getEquipment().setArmorContents(despawned_entity.equipment); + entity.setHealth(despawned_entity.health); + + despawned_entity = null; + } + + else entity = (LivingEntity) location.getWorld().spawnEntity(location, entity_type); + } + + else dB.echoError("Cannot spawn a null dEntity!"); + } + } + + public void despawn() { + despawned_entity = new DespawnedEntity(this); + entity.remove(); + } + + public void respawn() { + if (despawned_entity != null) + spawnAt(despawned_entity.location); + else if (entity != null) + dB.echoDebug("Entity " + identify() + " is already spawned!"); + else + dB.echoError("Cannot respawn a null dEntity!"); + + } + + public boolean isSpawned() { return entity != null; } public dEntity rememberAs(String id) { - dEntity.saveEntityAs(this, id); + dEntity.saveAs(this, id); return this; } + // Used to store some information about a livingEntity while it's despawned + private class DespawnedEntity { + + Integer health = null; + Location location = null; + ItemStack[] equipment = null; + String custom_script = null; + + public DespawnedEntity(dEntity entity) { + if (entity != null) { + // Save some important info to rebuild the entity + health = entity.getBukkitEntity().getHealth(); + location = entity.getBukkitEntity().getLocation(); + equipment = entity.getBukkitEntity().getEquipment().getArmorContents(); + + if (CustomNBT.hasCustomNBT(entity.getBukkitEntity(), "denizen-script-id")) + custom_script = CustomNBT.getCustomNBT(entity.getBukkitEntity(), "denizen-script-id"); + } + } + } ////////////////////////////// // DSCRIPT ARGUMENT METHODS @@ -195,6 +276,7 @@ public dEntity rememberAs(String id) { private String prefix = "Entity"; + @Override public String getType() { return "entity"; } @@ -217,6 +299,8 @@ public String debug() { @Override public String identify() { + + // Check if entity is a Player or NPC if (getBukkitEntity() != null) { if (CitizensAPI.getNPCRegistry().isNPC(getBukkitEntity())) return "n@" + CitizensAPI.getNPCRegistry().getNPC(getBukkitEntity()).getId(); @@ -224,12 +308,15 @@ else if (getBukkitEntity() instanceof Player) return "p@" + ((Player) getBukkitEntity()).getName(); } + // Check if entity is a 'saved entity' else if (isUnique()) - return "e@" + isSavedEntity(this); + return "e@" + getSaved(this); - else if (isAlive()) + // Check if entity is spawned, therefore having a bukkit entityId + else if (isSpawned()) return "e@" + getBukkitEntity().getEntityId(); + // Check if an entity_type is available else if (entity_type != null) return "e@" + entity_type.name(); @@ -238,13 +325,7 @@ else if (entity_type != null) @Override public boolean isUnique() { - if (isSavedEntity(this) != null) return true; - else return false; - } - - @Override - public String toString() { - return "e@" + entity.getEntityId(); + return isSaved(this); } @Override @@ -261,8 +342,8 @@ public String getAttribute(Attribute attribute) { return new Element(entity.getCustomName()).getAttribute(attribute.fulfill(1)); if (attribute.startsWith("type")) { - if (NBTItem.hasCustomNBT(getBukkitEntity(), "denizen-script-id")) - return new Script(NBTItem.getCustomNBT(getBukkitEntity(), "denizen-script-id")) + if (CustomNBT.hasCustomNBT(getBukkitEntity(), "denizen-script-id")) + return new Script(CustomNBT.getCustomNBT(getBukkitEntity(), "denizen-script-id")) .getAttribute(attribute.fulfill(1)); else return new Element(getBukkitEntity().getType().name()) diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dItem.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dItem.java index a063b4604a..4e66914f56 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dItem.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dItem.java @@ -5,7 +5,7 @@ import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer; import net.aufdemrand.denizen.tags.Attribute; import net.aufdemrand.denizen.utilities.debugging.dB; -import net.aufdemrand.denizen.utilities.nbt.NBTItem; +import net.aufdemrand.denizen.utilities.nbt.CustomNBT; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; @@ -22,45 +22,47 @@ public class dItem implements dScriptArgument { + ///////////////////// // STATIC METHODS ///////////////// - public static Map items = new HashMap(); + public static Map uniqueObjects = new HashMap(); - /** - * Gets a saved item based on an Id. - * - * @param id the Id key of the location - * @return the Location associated - */ - public static dItem getSavedItem(String id) { - if (items.containsKey(id.toLowerCase())) - return items.get(id.toLowerCase()); - else return null; + public static boolean isSaved(String id) { + return uniqueObjects.containsKey(id.toUpperCase()); } - public static void saveItemAs(dItem item, String id) { - if (item == null) return; - items.put(id.toLowerCase(), item); + public static boolean isSaved(dItem item) { + return uniqueObjects.containsValue(item); } - /** - * Checks if there is a saved item with this Id. - * - * @param id the Id to check - * @return true if it exists, false if not - */ - public static boolean isSavedItem(String id) { - return items.containsKey(id.toLowerCase()); + public static dItem getSaved(String id) { + if (uniqueObjects.containsKey(id.toUpperCase())) + return uniqueObjects.get(id.toUpperCase()); + else return null; } - public static String isSavedItem(dItem item) { - for (Map.Entry i : items.entrySet()) + public static String getSaved(dItem item) { + for (Map.Entry i : uniqueObjects.entrySet()) if (i.getValue() == item) return i.getKey(); return null; } + public static void saveAs(dItem item, String id) { + if (item == null) return; + uniqueObjects.put(id.toUpperCase(), item); + } + + public static void remove(String id) { + uniqueObjects.remove(id.toUpperCase()); + } + + + ////////////////// + // OBJECT FETCHER + //////////////// + /** * Gets a Item Object from a string form. * @@ -73,7 +75,6 @@ public static dItem valueOf(String string) { if (string == null) return null; Matcher m; - /////// // Match @object format for spawned Item entities @@ -87,17 +88,14 @@ public static dItem valueOf(String string) { if (entity.getEntityId() == Integer.valueOf(m.group(2))) return new dItem(((Item) entity).getItemStack()); - //////// // Match @object format for saved dItems final Pattern item_by_saved = Pattern.compile("(i@)(.+)"); m = item_by_saved.matcher(string); - // Check if it's an entity in the world if (m.matches()) - return items.get(m.group(2)); - + return getSaved(m.group(2)); /////// // Match item script custom items @@ -107,7 +105,6 @@ public static dItem valueOf(String string) { // Get item from script return ScriptRegistry.getScriptContainerAs(m.group(1), ItemScriptContainer.class).getItemFrom(); - /////// // Match bukkit/minecraft standard items format @@ -162,11 +159,9 @@ public static dItem valueOf(String string) { } - ///////////////////// - // INSTANCE METHODS - ///////////////// - - // Constructors + /////////////// + // Constructors + ///////////// public dItem(Material material) { item = new ItemStack(material); @@ -188,6 +183,11 @@ public dItem(ItemStack item) { this.item = item; } + + ///////////////////// + // INSTANCE FIELDS/METHODS + ///////////////// + // Bukkit itemstack associated private ItemStack item; @@ -321,24 +321,16 @@ public boolean isRepairable() { } } - public String getId() { - if (NBTItem.hasCustomNBT(getItemStack(), "denizen-script-id")) - return NBTItem.getCustomNBT(getItemStack(), "denizen-script-id"); - else - return getItemStack().getType().name().toLowerCase() + ":" + getItemStack().getData().getData(); - } - public void setItemStack(ItemStack item) { this.item = item; } public dItem rememberAs(String id) { - dItem.saveItemAs(this, id); + dItem.saveAs(this, id); return this; } - ////////////////////////////// // DSCRIPT ARGUMENT METHODS ///////////////////////// @@ -357,24 +349,29 @@ public String getPrefix() { @Override public String debug() { - return "" + prefix + "='" + getId() + "' "; + return "" + prefix + "='" + identify() + "' "; } @Override public String identify() { - if (isSavedItem(this) != null) - return "i@" + isSavedItem(this); - else if (item != null) { + // If saved item, return that + if (isSaved(this)) + return "i@" + getSaved(this); - } + // If not a saved item, but is a custom item, return the script id + else if (CustomNBT.hasCustomNBT(getItemStack(), "denizen-script-id")) + return CustomNBT.getCustomNBT(getItemStack(), "denizen-script-id"); - return null; + // Else, return the material name and data + else if (getItemStack() != null) + return getItemStack().getType().name().toLowerCase() + ":" + getItemStack().getData().getData(); + + return "null"; } @Override public boolean isUnique() { - if (isSavedItem(this) != null) return true; - else return false; + return isSaved(this); } @Override @@ -388,17 +385,10 @@ public String getAttribute(Attribute attribute) { if (attribute == null) return null; - // Desensitize the attribute for comparison - String id = getId().toLowerCase(); - if (attribute.startsWith("qty")) return new Element(String.valueOf(getItemStack().getAmount())) .getAttribute(attribute.fulfill(1)); - if (attribute.startsWith("id")) - return new Element(id) - .getAttribute(attribute.fulfill(1)); - if (attribute.startsWith("typeid")) return new Element(String.valueOf(getItemStack().getTypeId())) .getAttribute(attribute.fulfill(1)); @@ -421,6 +411,8 @@ public String getAttribute(Attribute attribute) { if (attribute.startsWith("material.formatted")) { + String id = item.getType().name().toLowerCase(); + if (id.equals("air")) return new Element("nothing") .getAttribute(attribute.fulfill(2)); @@ -495,6 +487,11 @@ public String getAttribute(Attribute attribute) { .getAttribute(attribute.fulfill(1)); } + if (attribute.startsWith("identify")) + return new Element(identify()) + .getAttribute(attribute.fulfill(1)); + + return new Element(identify()).getAttribute(attribute.fulfill(1)); } diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dList.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dList.java index 4852a33193..3b38efcc4d 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dList.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dList.java @@ -16,7 +16,7 @@ public class dList extends ArrayList implements dScriptArgument { - @ObjectFetcher("f") + @ObjectFetcher("li, fl") public static dList valueOf(String string) { if (string == null) return null; @@ -25,7 +25,7 @@ public static dList valueOf(String string) { // Make sure string matches what this interpreter can accept. final Pattern flag_by_id = - Pattern.compile("(f\\[((?:p@|n@)(.+?))\\]@|f@)(.+)", + Pattern.compile("(fl\\[((?:p@|n@)(.+?))\\]@|fl@)(.+)", Pattern.CASE_INSENSITIVE); Matcher m; @@ -36,15 +36,15 @@ public static dList valueOf(String string) { try { // Global - if (m.group(1).equalsIgnoreCase("f@")) { + if (m.group(1).equalsIgnoreCase("fl@")) { if (FlagManager.serverHasFlag(m.group(4))) return new dList(flag_manager.getGlobalFlag(m.group(4))); - } if (m.group(2).toLowerCase().startsWith("p@")) { + } else if (m.group(2).toLowerCase().startsWith("p@")) { if (FlagManager.playerHasFlag(aH.getPlayerFrom(m.group(3)), m.group(4))) return new dList(flag_manager.getPlayerFlag(m.group(3), m.group(4))); - } if (m.group(2).toLowerCase().startsWith("n@")) { + } else if (m.group(2).toLowerCase().startsWith("n@")) { if (FlagManager.npcHasFlag(aH.getdNPCFrom(m.group(3)), m.group(4))) return new dList(flag_manager.getNPCFlag(Integer.valueOf(m.group(3)), m.group(4))); } @@ -56,17 +56,15 @@ public static dList valueOf(String string) { } // Use value of string, which will seperate values by the use of a pipe (|) - return new dList(string.replaceFirst("a@", "")); + return new dList(string.replaceFirst("li@", "")); } ///////////// - // Instance Methods + // Constructors ////////// - private FlagManager.Flag flag = null; - public dList(String items) { addAll(Arrays.asList(items.split("\\|"))); } @@ -80,22 +78,16 @@ public dList(FlagManager.Flag flag) { addAll(flag.values()); } - private String getId() { - if (flag != null) - return flag.toString(); - if (isEmpty()) return "li@"; - StringBuilder dScriptArg = new StringBuilder(); - dScriptArg.append("li@"); - for (String item : this) - dScriptArg.append(item + "|"); - return dScriptArg.toString().substring(0, dScriptArg.length() - 1); + ///////////// + // Instance Fields/Methods + ////////// - } + private FlagManager.Flag flag = null; ////////////////////////////// - // DSCRIPT ARGUMENT METHODS + // DSCRIPT ARGUMENT METHODS ///////////////////////// @@ -114,17 +106,33 @@ public dList setPrefix(String prefix) { @Override public String debug() { - return "" + prefix + "='" + getId() + "' "; + return "" + prefix + "='" + identify() + "' "; } @Override - public String as_dScriptArgValue() { - return getId(); + public boolean isUnique() { + if (flag != null) return true; + else return false; } @Override - public String toString() { - return "l@" + getId(); + public String getType() { + return "list"; + } + + @Override + public String identify() { + if (flag != null) + return flag.toString(); + + if (isEmpty()) return "li@"; + + StringBuilder dScriptArg = new StringBuilder(); + dScriptArg.append("li@"); + for (String item : this) + dScriptArg.append(item + "|"); + + return dScriptArg.toString().substring(0, dScriptArg.length() - 1); } @Override diff --git a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dLocation.java b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dLocation.java index b243ffe995..38a828be1e 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/arguments/dLocation.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/arguments/dLocation.java @@ -10,73 +10,78 @@ import org.bukkit.World; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; public class dLocation extends org.bukkit.Location implements dScriptArgument { - public static Map locations = new ConcurrentHashMap(8, 0.9f, 1); - /** - * Gets a saved location based on an Id. - * - * @param id the Id key of the location - * @return the Location associated - */ - public static dLocation getSavedLocation(String id) { - return locations.get(id.toLowerCase()); + ///////////////////// + // STATIC METHODS + ///////////////// + + public static Map uniqueObjects = new HashMap(); + + public static boolean isSaved(String id) { + return uniqueObjects.containsKey(id.toUpperCase()); } - public static String isSavedLocation(Location location) { - for (Map.Entry entry : locations.entrySet()) { - if (Utilities.checkLocation(entry.getValue(), location, 1)) { - return entry.getKey(); - } - } + public static boolean isSaved(dLocation location) { + return uniqueObjects.containsValue(location); + } + + public static dLocation getSaved(String id) { + if (uniqueObjects.containsKey(id.toUpperCase())) + return uniqueObjects.get(id.toUpperCase()); + else return null; + } + + public static String getSaved(dLocation location) { + for (Map.Entry i : uniqueObjects.entrySet()) + if (i.getValue() == location) return i.getKey(); return null; } - public static dLocation saveLocationAs(dLocation location, String id) { - locations.put(id.toLowerCase(), location); - return location; + public static void saveAs(dLocation location, String id) { + if (location == null) return; + uniqueObjects.put(id.toUpperCase(), location); } - /** - * Checks if there is a saved location with this Id. - * - * @param id the Id to check - * @return true if it exists, false if not - */ - public static boolean isSavedLocation(String id) { - if (id == null) return false; - return locations.containsKey(id.toLowerCase()); + public static void remove(String id) { + uniqueObjects.remove(id.toUpperCase()); } - /** + /* * Called on server startup or /denizen reload locations. Should probably not be called manually. */ public static void _recallLocations() { List loclist = DenizenAPI.getCurrentInstance().getSaves().getStringList("dScript.Locations"); - locations.clear(); + uniqueObjects.clear(); for (String location : loclist) { dLocation loc = valueOf(location); // TODO: Finish this } } - /** + /* * Called by Denizen internally on a server shutdown or /denizen save. Should probably * not be called manually. */ public static void _saveLocations() { List loclist = new ArrayList(); - for (Map.Entry entry : locations.entrySet()) + for (Map.Entry entry : uniqueObjects.entrySet()) loclist.add(entry.getValue().toString()); DenizenAPI.getCurrentInstance().getSaves().set("dScript.Locations", loclist); } + + ////////////////// + // OBJECT FETCHER + //////////////// + /** * Gets a Location Object from a string form of id,x,y,z,world * or a dScript argument (location:)x,y,z,world. If including an Id, @@ -134,8 +139,7 @@ else if (split.length == 6) /** * Turns a Bukkit Location into a Location, which has some helpful methods - * for working with dScript. If working with temporary locations, this is - * a much better method to use than {@link #dLocation(Location)}. + * for working with dScript. * * @param location the Bukkit Location to reference */ @@ -163,10 +167,12 @@ public dLocation(World world, double x, double y, double z, float yaw, float pit } public dLocation rememberAs(String id) { - dLocation.saveLocationAs(this, id); + dLocation.saveAs(this, id); return this; } + + String prefix = "Location"; @Override @@ -176,12 +182,27 @@ public String getPrefix() { @Override public String debug() { - return (isSavedLocation(this) != null ? "" + prefix + "='" + isSavedLocation(this) + "(" + getX() + "," + getY() + return (isSaved(this) ? "" + prefix + "='" + getSaved(this) + "(" + getX() + "," + getY() + "," + getZ() + "," + getWorld().getName() + ")' " : "" + prefix + "='" + getX() + "," + getY() + "," + getZ() + "," + getWorld().getName() + "' "); } + @Override + public boolean isUnique() { + return false; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String getType() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public String identify() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + @Override public String as_dScriptArgValue() { return getX() + "," + getY() diff --git a/src/main/java/net/aufdemrand/denizen/utilities/nbt/NBTItem.java b/src/main/java/net/aufdemrand/denizen/utilities/nbt/CustomNBT.java similarity index 99% rename from src/main/java/net/aufdemrand/denizen/utilities/nbt/NBTItem.java rename to src/main/java/net/aufdemrand/denizen/utilities/nbt/CustomNBT.java index e55bdd2044..a997f7d062 100644 --- a/src/main/java/net/aufdemrand/denizen/utilities/nbt/NBTItem.java +++ b/src/main/java/net/aufdemrand/denizen/utilities/nbt/CustomNBT.java @@ -8,7 +8,7 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; -public class NBTItem { +public class CustomNBT { public static MapOfEnchantments getEnchantments(ItemStack item) { return new MapOfEnchantments(item);