diff --git a/src/main/java/net/aufdemrand/denizen/objects/Duration.java b/src/main/java/net/aufdemrand/denizen/objects/Duration.java index cbb454cf76..228c50c29c 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Duration.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Duration.java @@ -19,6 +19,11 @@ */ public class Duration implements dObject { + + ///////////////////// + // STATIC METHODS AND FIELDS + ///////////////// + // Use regex pattern matching to easily determine if a string // value is a valid Duration. final static Pattern match = @@ -32,6 +37,19 @@ public class Duration implements dObject { final public static Duration ZERO = new Duration(0); + ///////////////////// + // OBJECT FETCHER + ///////////////// + + // <--[language] + // @name d@ + // @description + // d@ refers to the object type of a 'Duration'. The 'd@' 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. + // --> + /** * Gets a Duration Object from a dScript argument. Durations must be a positive * number. Can specify the unit of time by using one of the following: T=ticks, M=minutes, diff --git a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java index 4ea99039a4..312a2720b4 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dEntity.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dEntity.java @@ -4,6 +4,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import net.aufdemrand.denizen.objects.properties.EntityAge; import net.aufdemrand.denizen.objects.properties.EntityInfected; import net.aufdemrand.denizen.objects.properties.EntityProfessional; import net.aufdemrand.denizen.objects.properties.Property; @@ -59,42 +60,6 @@ public class dEntity implements dObject { Pattern.CASE_INSENSITIVE); - ///////////////////// - // STATIC METHODS - ///////////////// - - public static Map uniqueObjects = new HashMap(); - - public static boolean isSaved(String id) { - return uniqueObjects.containsKey(id.toUpperCase()); - } - - public static boolean isSaved(dEntity entity) { - return uniqueObjects.containsValue(entity); - } - - public static dEntity getSaved(String id) { - if (uniqueObjects.containsKey(id.toUpperCase())) - return uniqueObjects.get(id.toUpperCase()); - else return null; - } - - 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 //////////////// @@ -192,8 +157,8 @@ else if (entityGroup.matches("P@")) { if (entity != null) return new dEntity(entity); } - else if (isSaved(m.group(2))) - return getSaved(m.group(2)); +// else if (isSaved(m.group(2))) +// return getSaved(m.group(2)); } } @@ -512,27 +477,6 @@ public boolean hasShooter() { return isProjectile() && getProjectile().getShooter() != null; } - /** - * Get this dEntity as an Ageable - * - * @return The Ageable - */ - - public Ageable getAgeable() { - - return (Ageable) entity; - } - - /** - * Check whether this dEntity is an Ageable - * - * @return true or false - */ - - public boolean isAgeable() { - return entity instanceof Ageable; - } - /** * Whether this entity identifies as a generic * entity type, for instance "e@cow", instead of @@ -804,7 +748,6 @@ public boolean isSpawned() { } public boolean isValid() { - return entity.isValid(); } @@ -812,11 +755,6 @@ public void remove() { entity.remove(); } - public dEntity rememberAs(String id) { - dEntity.saveAs(this, id); - return this; - } - public void teleport(Location location) { if (isNPC()) getNPC().teleport(location, TeleportCause.PLUGIN); @@ -912,6 +850,24 @@ public DespawnedEntity(dEntity entity) { + public int comparesTo(dEntity entity) { + // If provided is unique, and both are the same unique entity, return 1. + if (entity.isUnique() && entity.identify().equals(identify())) return 1; + + // If provided isn't unique... + if (!entity.isUnique()) { + // Return 1 if this object isn't unique either, but matches + if (!isUnique() && entity.identify().equals(identify())) + return 1; + // Return 1 if the provided object isn't unique, but whose entity_type + // matches this object, even if this object is unique. + if (entity_type == entity.entity_type) return 1; + } + + return 0; + } + + /////////////// // Properties @@ -946,11 +902,19 @@ public EntityProfessional getProfessional() { return EntityProfessional.getFrom(this); } + public boolean isAgeable() { + return EntityAge.describes(this); + } + + public EntityAge getAgeable() { + return EntityAge.getFrom(this); + } + - ////////////////////////////// - // DSCRIPT ARGUMENT METHODS - ///////////////////////// + ///////////////////// + // dObject Methods + /////////////////// private String prefix = "Entity"; @@ -975,23 +939,6 @@ public String debug() { return "" + prefix + "='" + identify() + "' "; } - public int comparesTo(dEntity entity) { - // If provided is unique, and both are the same unique entity, return 1. - if (entity.isUnique() && entity.identify().equals(identify())) return 1; - - // If provided isn't unique... - if (!entity.isUnique()) { - // Return 1 if this object isn't unique either, but matches - if (!isUnique() && entity.identify().equals(identify())) - return 1; - // Return 1 if the provided object isn't unique, but whose entity_type - // matches this object, even if this object is unique. - if (entity_type == entity.entity_type) return 1; - } - - return 0; - } - @Override public String identify() { @@ -1003,10 +950,9 @@ else if (isPlayer()) return "p@" + getPlayer().getName(); } - // Check if entity is a 'saved entity' - if (isSaved(this)) - return "e@" + getSaved(this); - +// // Check if entity is a 'notable entity' +// if (isSaved(this)) +// return "e@" + getSaved(this); else if (isSpawned()) return "e@" + entity.getEntityId(); @@ -1025,7 +971,7 @@ public String toString() { @Override public boolean isUnique() { - return (isPlayer() || isNPC() || isSaved(this) || isSpawned()); + return (isPlayer() || isNPC() || isSpawned()); // || isSaved() } @Override @@ -1656,7 +1602,34 @@ else if ((float) getLivingEntity().getHealth() / maxHealth < 1) return new Element(entity instanceof Tameable) .getAttribute(attribute.fulfill(1)); + + + ///////////////////// + // PROPERTY ATTRIBUTES + ///////////////// + + if (attribute.startsWith("infected")) { + if (EntityInfected.describes(this)) + return EntityInfected.getFrom(this) + .getAttribute(attribute.fulfill(1)); + else return Attribute.RETURN_NULL; + } + + if (attribute.startsWith("professional")) { + if (EntityProfessional.describes(this)) + return EntityProfessional.getFrom(this) + .getAttribute(attribute.fulfill(1)); + else return Attribute.RETURN_NULL; + } + + + + + + return new Element(identify()).getAttribute(attribute); } + + } diff --git a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java index 7b7fbc6009..f92b91e3b4 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dPlayer.java @@ -39,6 +39,15 @@ public static dPlayer mirrorBukkitPlayer(OfflinePlayer player) { // OBJECT FETCHER ///////////////// + // <--[language] + // @name p@ + // @description + // p@ refers to the object type 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. + // --> + @ObjectFetcher("p") public static dPlayer valueOf(String string) { if (string == null) return null; diff --git a/src/main/java/net/aufdemrand/denizen/objects/dWorld.java b/src/main/java/net/aufdemrand/denizen/objects/dWorld.java index 65e4eaf9e4..36cf79ac0f 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dWorld.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dWorld.java @@ -16,13 +16,33 @@ public class dWorld implements dObject { + + ///////////////////// + // STATIC METHODS + ///////////////// + static Map worlds = new HashMap(); public static dWorld mirrorBukkitWorld(World world) { + if (world == null) return null; if (worlds.containsKey(world.getName())) return worlds.get(world.getName()); else return new dWorld(world); } + + ///////////////////// + // OBJECT FETCHER + ///////////////// + + // <--[language] + // @name w@ + // @description + // w@ refers to the object type 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. + // --> + @ObjectFetcher("w") public static dWorld valueOf(String string) { if (string == null) return null; diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/EntityAge.java b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityAge.java new file mode 100644 index 0000000000..e94dd872fd --- /dev/null +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityAge.java @@ -0,0 +1,131 @@ +package net.aufdemrand.denizen.objects.properties; + + +import net.aufdemrand.denizen.objects.Element; +import net.aufdemrand.denizen.objects.dEntity; +import net.aufdemrand.denizen.tags.Attribute; +import net.citizensnpcs.api.npc.NPC; +import net.citizensnpcs.trait.Age; +import org.bukkit.entity.*; + +public class EntityAge implements Property { + + public static boolean describes(dEntity entity) { + return (entity.getBukkitEntity() instanceof Ageable) + || entity.getBukkitEntity().getType() == EntityType.ZOMBIE; + } + + public static EntityAge getFrom(dEntity entity) { + if (!describes(entity)) return null; + + else return new EntityAge(entity); + } + + + /////////////////// + // Instance Fields and Methods + ///////////// + + private EntityAge(dEntity entity) { + ageable = entity; + } + + dEntity ageable; + + public boolean isBaby() { + if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) + return ((Zombie) ageable.getBukkitEntity()).isBaby(); + else + return !((Ageable) ageable.getBukkitEntity()).isAdult(); + + } + + public void setBaby(boolean bool) { + if (ageable.isNPC()) { + NPC ageable_npc = ageable.getNPC(); + ageable_npc.getTrait(Age.class).setAge(bool ? 0 : 1); + + } else { + if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) + ((Zombie) ageable.getBukkitEntity()).setBaby(bool); + + else if (bool) + ((Ageable) ageable.getBukkitEntity()).setBaby(); + + else + ((Ageable) ageable.getBukkitEntity()).setAge(1); + } + } + + public void setAge(int val) { + if (ageable.isNPC()) { + NPC ageable_npc = ageable.getNPC(); + ageable_npc.getTrait(Age.class).setAge(val); + + } else { + if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) + ((Zombie) ageable.getBukkitEntity()).setBaby(val < 1 ? false : true); + else + ((Ageable) ageable.getBukkitEntity()).setAge(val); + } + } + + public int getAge() { + if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) + return ((Zombie) ageable.getBukkitEntity()).isBaby() ? 0 : 1; + else + return ((Ageable) ageable.getBukkitEntity()).getAge(); + } + + public void setLock(boolean bool) { + if (ageable.getBukkitEntity().getType() != EntityType.ZOMBIE) + ((Ageable) ageable.getBukkitEntity()).setAgeLock(bool); + } + + public boolean getLock() { + if (ageable.getBukkitEntity().getType() == EntityType.ZOMBIE) + return true; + else return ((Ageable) ageable.getBukkitEntity()).getAgeLock(); + } + + + ///////// + // Property Methods + /////// + + @Override + public String getPropertyString() { + return getPropertyId() + '=' + getAge() + ';'; + } + + @Override + public String getPropertyId() { + return "age"; + } + + + /////////// + // dObject Attributes + //////// + + @Override + public String getAttribute(Attribute attribute) { + + if (attribute == null) return "null"; + + // <--[tag] + // @attribute + // @returns Element(Boolean) + // @description + // Returns 'true' if the entity is 'infected', otherwise false. + // Currently only 'Zombie' or 'Villager' entities can be infected. + // --> + if (attribute.startsWith("is_baby")) + return new Element(isBaby()) + .getAttribute(attribute.fulfill(1)); + + + return new Element(getAge()).getAttribute(attribute); + } + +} diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/EntityInfected.java b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityInfected.java index e323d82aa4..80bab2ab82 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/properties/EntityInfected.java +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityInfected.java @@ -99,7 +99,7 @@ public String getPropertyId() { //////// @Override - public String getAttributes(Attribute attribute) { + public String getAttribute(Attribute attribute) { if (attribute == null) return "null"; diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/EntityProfessional.java b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityProfessional.java index 652e12ed73..bedb697488 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/properties/EntityProfessional.java +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/EntityProfessional.java @@ -63,7 +63,7 @@ public String getPropertyId() { //////// @Override - public String getAttributes(Attribute attribute) { + public String getAttribute(Attribute attribute) { if (attribute == null) return "null"; diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/ItemColor.java b/src/main/java/net/aufdemrand/denizen/objects/properties/ItemColor.java index 9272dedad7..eaab2e2862 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/properties/ItemColor.java +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/ItemColor.java @@ -35,7 +35,7 @@ public dColor getColor() { } @Override - public String getAttributes(Attribute attribute) { + public String getAttribute(Attribute attribute) { if (attribute == null) return "null"; diff --git a/src/main/java/net/aufdemrand/denizen/objects/properties/Property.java b/src/main/java/net/aufdemrand/denizen/objects/properties/Property.java index a9c98ce87a..8016d29c8a 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/properties/Property.java +++ b/src/main/java/net/aufdemrand/denizen/objects/properties/Property.java @@ -8,6 +8,6 @@ public interface Property { public String getPropertyId(); - public String getAttributes(Attribute attribute); + public String getAttribute(Attribute attribute); } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/AgeCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/AgeCommand.java index ecec44fc1a..427e3d3756 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/AgeCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/AgeCommand.java @@ -91,12 +91,12 @@ public void execute(final ScriptEntry scriptEntry) throws CommandExecutionExcept if (entity.isAgeable()) { if (ageType != null) { if (ageType.equals(ageType.BABY)) - entity.getAgeable().setBaby(); - else entity.getAgeable().setAdult(); + entity.getAgeable().setBaby(true); + else entity.getAgeable().setBaby(false); } else entity.getAgeable().setAge(age); - if (lock) entity.getAgeable().setAgeLock(true); + if (lock) entity.getAgeable().setLock(true); } // Zombies are not ageable, but can be babies 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 2e008d8a51..0b4f289dfd 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 @@ -20,6 +20,36 @@ public class ItemScriptContainer extends ScriptContainer { + // <--[language] + // @name Item Script Containers + // @description + // Item script containers are an easy way to pre-define custom items for use within scripts. Item + // scripts work with the dItem object, and can be fetched with the Object Fetcher by using the + // dItem constructor i@item_script_name. Example: + // - drop i@super_dooper_diamond + // + // The following is the format for the container. All keys are optional. + // + // Item Script Name: + // type: item + // + // display name: custom name + // lore: + // - item + // - ... + // enchantments: + // - enchantment_name:level + // - ... + // recipe: + // - m@material|m@material|m@material + // - m@material|m@material|m@material + // - m@material|m@material|m@material + // bound: true # Bound items cannot be dropped + // color: c@color # Only colorable items (such as leather) + // book: book_script_name # Only i@written_book types + // + // --> + dNPC npc = null; dPlayer player = null; public boolean bound = false; @@ -131,7 +161,7 @@ public dItem getItemFrom(dPlayer player, dNPC npc) { // Set Book if (contains("BOOK")) { BookScriptContainer book = ScriptRegistry - .getScriptContainerAs(getString("BOOK"), BookScriptContainer.class); + .getScriptContainerAs(getString("BOOK").replace("s@", ""), BookScriptContainer.class); stack = book.writeBookTo(stack, player, npc); } diff --git a/src/main/java/net/aufdemrand/denizen/tags/Attribute.java b/src/main/java/net/aufdemrand/denizen/tags/Attribute.java index 5ed3f30202..eea3f09326 100644 --- a/src/main/java/net/aufdemrand/denizen/tags/Attribute.java +++ b/src/main/java/net/aufdemrand/denizen/tags/Attribute.java @@ -17,7 +17,8 @@ public class Attribute { - // TODO: Make this private. It's public right now to enable easy debugging. + public static String RETURN_NULL = "null"; + public List attributes; ScriptEntry scriptEntry;