diff --git a/src/main/java/net/aufdemrand/denizen/Denizen.java b/src/main/java/net/aufdemrand/denizen/Denizen.java index 37e2831df7..189d483180 100644 --- a/src/main/java/net/aufdemrand/denizen/Denizen.java +++ b/src/main/java/net/aufdemrand/denizen/Denizen.java @@ -702,22 +702,23 @@ public void onEnable() { ScriptEvent.registerScriptEvent(new WorldUnloadsScriptEvent()); - ObjectFetcher.registerWithObjectFetcher(dItem.class); // i@ + ObjectFetcher.registerWithObjectFetcher(dBiome.class); // b@ + dBiome.registerTags(); // TODO: Automate this once all classes have tag registries + ObjectFetcher.registerWithObjectFetcher(dChunk.class); // ch@ + dChunk.registerTags(); // TODO: Automate this once all classes have tag registries + ObjectFetcher.registerWithObjectFetcher(dColor.class); // co@ ObjectFetcher.registerWithObjectFetcher(dCuboid.class); // cu@ + ObjectFetcher.registerWithObjectFetcher(dEllipsoid.class); // ellipsoid@ ObjectFetcher.registerWithObjectFetcher(dEntity.class); // e@ ObjectFetcher.registerWithObjectFetcher(dInventory.class); // in@ - ObjectFetcher.registerWithObjectFetcher(dColor.class); // co@ + ObjectFetcher.registerWithObjectFetcher(dItem.class); // i@ ObjectFetcher.registerWithObjectFetcher(dLocation.class); // l@ ObjectFetcher.registerWithObjectFetcher(dMaterial.class); // m@ if (Depends.citizens != null) ObjectFetcher.registerWithObjectFetcher(dNPC.class); // n@ ObjectFetcher.registerWithObjectFetcher(dPlayer.class); // p@ - ObjectFetcher.registerWithObjectFetcher(dWorld.class); // w@ - ObjectFetcher.registerWithObjectFetcher(dChunk.class); // ch@ ObjectFetcher.registerWithObjectFetcher(dPlugin.class); // pl@ - ObjectFetcher.registerWithObjectFetcher(dEllipsoid.class); // ellipsoid@ - ObjectFetcher.registerWithObjectFetcher(dBiome.class); // b@ - dBiome.registerTags(); // TODO: Automate this once all classes have tag registries + ObjectFetcher.registerWithObjectFetcher(dWorld.class); // w@ // Register Core dObjects with the ObjectFetcher diff --git a/src/main/java/net/aufdemrand/denizen/objects/dBiome.java b/src/main/java/net/aufdemrand/denizen/objects/dBiome.java index f87924a847..a54608971f 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dBiome.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dBiome.java @@ -233,7 +233,7 @@ else if (attribute.startsWith("water")) } }); } - + public static HashMap registeredTags = new HashMap(); public static void registerTag(String name, TagRunnable runnable) { diff --git a/src/main/java/net/aufdemrand/denizen/objects/dChunk.java b/src/main/java/net/aufdemrand/denizen/objects/dChunk.java index 6e60fca8e0..486ea07f9e 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dChunk.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dChunk.java @@ -8,6 +8,7 @@ import net.aufdemrand.denizencore.objects.properties.PropertyParser; import net.aufdemrand.denizencore.tags.Attribute; import net.aufdemrand.denizencore.tags.TagContext; +import net.aufdemrand.denizencore.utilities.CoreUtilities; import org.bukkit.Chunk; import org.bukkit.ChunkSnapshot; import org.bukkit.Location; @@ -17,10 +18,7 @@ import org.bukkit.entity.LivingEntity; import org.bukkit.scheduler.BukkitRunnable; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Random; +import java.util.*; public class dChunk implements dObject, Adjustable { @@ -163,9 +161,7 @@ public String toString() { return identify(); } - @Override - public String getAttribute(Attribute attribute) { - if (attribute == null) return null; + public static void registerTags() { // <--[tag] // @attribute @@ -173,8 +169,12 @@ public String getAttribute(Attribute attribute) { // @description // returns true if the chunk is currently loaded into memory. // --> - if (attribute.startsWith("is_loaded")) - return new Element(chunk.isLoaded()).getAttribute(attribute.fulfill(1)); + registerTag("is_loaded", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dChunk) object).chunk.isLoaded()).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -182,8 +182,12 @@ public String getAttribute(Attribute attribute) { // @description // returns the x coordinate of the chunk. // --> - if (attribute.startsWith("x")) - return new Element(getX()).getAttribute(attribute.fulfill(1)); + registerTag("x", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dChunk) object).chunk.getX()).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -191,8 +195,12 @@ public String getAttribute(Attribute attribute) { // @description // returns the z coordinate of the chunk. // --> - if (attribute.startsWith("z")) - return new Element(getZ()).getAttribute(attribute.fulfill(1)); + registerTag("z", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dChunk) object).chunk.getZ()).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -200,8 +208,12 @@ public String getAttribute(Attribute attribute) { // @description // returns the world associated with the chunk. // --> - if (attribute.startsWith("world")) - return dWorld.mirrorBukkitWorld(getWorld()).getAttribute(attribute.fulfill(1)); + registerTag("world", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return dWorld.mirrorBukkitWorld(((dChunk) object).chunk.getWorld()).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -209,11 +221,15 @@ public String getAttribute(Attribute attribute) { // @description // returns a cuboid of this chunk. // --> - if (attribute.startsWith("cuboid")) { - return new dCuboid(new Location(getWorld(), getX() * 16, 0, getZ() * 16), - new Location(getWorld(), getX() * 16 + 15, 255, getZ() * 16 + 15)) - .getAttribute(attribute.fulfill(1)); - } + registerTag("cuboid", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dChunk chunk = (dChunk) object; + return new dCuboid(new Location(chunk.getWorld(), chunk.getX() * 16, 0, chunk.getZ() * 16), + new Location(chunk.getWorld(), chunk.getX() * 16 + 15, 255, chunk.getZ() * 16 + 15)) + .getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -221,13 +237,16 @@ public String getAttribute(Attribute attribute) { // @description // returns a list of entities in the chunk. // --> - if (attribute.startsWith("entities")) { - dList entities = new dList(); - for (Entity ent : chunk.getEntities()) - entities.add(new dEntity(ent).identify()); + registerTag("entities", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dList entities = new dList(); + for (Entity ent : ((dChunk) object).chunk.getEntities()) + entities.add(new dEntity(ent).identify()); - return entities.getAttribute(attribute.fulfill(1)); - } + return entities.getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -236,14 +255,17 @@ public String getAttribute(Attribute attribute) { // returns a list of living entities in the chunk. This includes Players, mobs, NPCs, etc., but excludes // dropped items, experience orbs, etc. // --> - if (attribute.startsWith("living_entities")) { - dList entities = new dList(); - for (Entity ent : chunk.getEntities()) - if (ent instanceof LivingEntity) - entities.add(new dEntity(ent).identify()); - - return entities.getAttribute(attribute.fulfill(1)); - } + registerTag("living_entities", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dList entities = new dList(); + for (Entity ent : ((dChunk) object).chunk.getEntities()) + if (ent instanceof LivingEntity) + entities.add(new dEntity(ent).identify()); + + return entities.getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -251,14 +273,17 @@ public String getAttribute(Attribute attribute) { // @description // returns a list of players in the chunk. // --> - if (attribute.startsWith("players")) { - dList entities = new dList(); - for (Entity ent : chunk.getEntities()) - if (dEntity.isPlayer(ent)) - entities.add(dEntity.getPlayerFrom(ent).identify()); - - return entities.getAttribute(attribute.fulfill(1)); - } + registerTag("players", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dList entities = new dList(); + for (Entity ent : ((dChunk) object).chunk.getEntities()) + if (dEntity.isPlayer(ent)) + entities.add(new dEntity(ent).identify()); + + return entities.getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -266,12 +291,15 @@ public String getAttribute(Attribute attribute) { // @description // returns a list of the height of each block in the chunk. // --> - if (attribute.startsWith("height_map")) { - List height_map = new ArrayList(chunk.getHandle().heightMap.length); - for (int i : chunk.getHandle().heightMap) - height_map.add(String.valueOf(i)); - return new dList(height_map).getAttribute(attribute.fulfill(1)); - } + registerTag("height_map", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + List height_map = new ArrayList(((dChunk) object).chunk.getHandle().heightMap.length); + for (int i : ((dChunk) object).chunk.getHandle().heightMap) + height_map.add(String.valueOf(i)); + return new dList(height_map).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -279,11 +307,14 @@ public String getAttribute(Attribute attribute) { // @description // returns the average height of the blocks in the chunk. // --> - if (attribute.startsWith("average_height")) { - int sum = 0; - for (int i : chunk.getHandle().heightMap) sum += i; - return new Element(((double) sum) / chunk.getHandle().heightMap.length).getAttribute(attribute.fulfill(1)); - } + registerTag("average_height", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + int sum = 0; + for (int i : ((dChunk) object).chunk.getHandle().heightMap) sum += i; + return new Element(((double) sum) / ((dChunk) object).chunk.getHandle().heightMap.length).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -293,16 +324,19 @@ public String getAttribute(Attribute attribute) { // true if all the blocks are less than 2 blocks apart in height. Specifying a number will modify the number // criteria for determining if it is flat. // --> - if (attribute.startsWith("is_flat")) { - int tolerance = attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1)) ? - Integer.valueOf(attribute.getContext(1)) : 2; - int x = chunk.getHandle().heightMap[0]; - for (int i : chunk.getHandle().heightMap) - if (Math.abs(x - i) > tolerance) - return Element.FALSE.getAttribute(attribute.fulfill(1)); - - return Element.TRUE.getAttribute(attribute.fulfill(1)); - } + registerTag("is_flat", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + int tolerance = attribute.hasContext(1) && aH.matchesInteger(attribute.getContext(1)) ? + Integer.valueOf(attribute.getContext(1)) : 2; + int x = ((dChunk) object).chunk.getHandle().heightMap[0]; + for (int i : ((dChunk) object).chunk.getHandle().heightMap) + if (Math.abs(x - i) > tolerance) + return Element.FALSE.getAttribute(attribute.fulfill(1)); + + return Element.TRUE.getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -310,15 +344,18 @@ public String getAttribute(Attribute attribute) { // @description // returns a list of the highest non-air surface blocks in the chunk. // --> - if (attribute.startsWith("surface_blocks")) { - dList surface_blocks = new dList(); - for (int x = 0; x < 16; x++) - for (int z = 0; z < 16; z++) - surface_blocks.add(new dLocation(chunk.getBlock(x, getSnapshot().getHighestBlockYAt(x, z) - 1, z) - .getLocation()).identify()); - - return surface_blocks.getAttribute(attribute.fulfill(1)); - } + registerTag("surface_blocks", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dList surface_blocks = new dList(); + for (int x = 0; x < 16; x++) + for (int z = 0; z < 16; z++) + surface_blocks.add(new dLocation(((dChunk) object).chunk.getBlock(x, ((dChunk) object) + .getSnapshot().getHighestBlockYAt(x, z) - 1, z).getLocation()).identify()); + + return surface_blocks.getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -326,14 +363,18 @@ public String getAttribute(Attribute attribute) { // @description // returns whether the chunk is a specially located 'slime spawner' chunk. // --> - if (attribute.startsWith("spawn_slimes")) { - Random random = new Random(getWorld().getSeed() + - getX() * getX() * 4987142 + - getX() * 5947611 + - getZ() * getZ() * 4392871L + - getZ() * 389711 ^ 0x3AD8025F); - return new Element(random.nextInt(10) == 0).getAttribute(attribute.fulfill(1)); - } + registerTag("spawn_slimes", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + dChunk chunk = (dChunk) object; + Random random = new Random(chunk.getWorld().getSeed() + + chunk.getX() * chunk.getX() * 4987142 + + chunk.getX() * 5947611 + + chunk.getZ() * chunk.getZ() * 4392871L + + chunk.getZ() * 389711 ^ 0x3AD8025F); + return new Element(random.nextInt(10) == 0).getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -342,16 +383,45 @@ public String getAttribute(Attribute attribute) { // Always returns 'Chunk' for dChunk objects. All objects fetchable by the Object Fetcher will return the // type of object that is fulfilling this attribute. // --> - if (attribute.startsWith("type")) { - return new Element("Chunk").getAttribute(attribute.fulfill(1)); + registerTag("type", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element("Chunk").getAttribute(attribute.fulfill(1)); + } + }); + + } + + public static HashMap registeredTags = new HashMap(); + + public static void registerTag(String name, TagRunnable runnable) { + if (runnable.name == null) { + runnable.name = name; } + registeredTags.put(name, runnable); + } + + @Override + public String getAttribute(Attribute attribute) { + if (attribute == null) return null; + + // TODO: Scrap getAttribute, make this functionality a core system + String attrLow = CoreUtilities.toLowerCase(attribute.getAttributeWithoutContext(1)); + TagRunnable tr = registeredTags.get(attrLow); + if (tr != null) { + if (!tr.name.equals(attrLow)) { + net.aufdemrand.denizencore.utilities.debugging.dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null, + "Using deprecated form of tag '" + tr.name + "': '" + attrLow + "'."); + } + return tr.run(attribute, this); + } + // Iterate through this object's properties' attributes for (Property property : PropertyParser.getProperties(this)) { String returned = property.getAttribute(attribute); if (returned != null) return returned; } - return new Element(identify()).getAttribute(attribute); }