From b35587cc10fc6a9679927c1ed419a1ded7d258cb Mon Sep 17 00:00:00 2001 From: Talamar1 Date: Thu, 23 Jul 2015 16:57:02 -0400 Subject: [PATCH] Rewrote dPlugin tags in new registerTag format. --- .../java/net/aufdemrand/denizen/Denizen.java | 1 + .../aufdemrand/denizen/objects/dPlugin.java | 98 +++++++++++++------ 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/Denizen.java b/src/main/java/net/aufdemrand/denizen/Denizen.java index 7760717f77..5e2e3f7d9c 100644 --- a/src/main/java/net/aufdemrand/denizen/Denizen.java +++ b/src/main/java/net/aufdemrand/denizen/Denizen.java @@ -720,6 +720,7 @@ public void onEnable() { ObjectFetcher.registerWithObjectFetcher(dNPC.class); // n@ ObjectFetcher.registerWithObjectFetcher(dPlayer.class); // p@ ObjectFetcher.registerWithObjectFetcher(dPlugin.class); // pl@ + dPlugin.registerTags(); // TODO: Automate this once all classes have tag registries ObjectFetcher.registerWithObjectFetcher(dWorld.class); // w@ diff --git a/src/main/java/net/aufdemrand/denizen/objects/dPlugin.java b/src/main/java/net/aufdemrand/denizen/objects/dPlugin.java index ca91937fee..827fb23bae 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/dPlugin.java +++ b/src/main/java/net/aufdemrand/denizen/objects/dPlugin.java @@ -1,17 +1,17 @@ package net.aufdemrand.denizen.objects; import net.aufdemrand.denizen.utilities.debugging.dB; -import net.aufdemrand.denizencore.objects.Element; -import net.aufdemrand.denizencore.objects.Fetchable; -import net.aufdemrand.denizencore.objects.dList; -import net.aufdemrand.denizencore.objects.dObject; +import net.aufdemrand.denizencore.objects.*; import net.aufdemrand.denizencore.objects.properties.Property; 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.Bukkit; import org.bukkit.plugin.Plugin; +import java.util.HashMap; + public class dPlugin implements dObject { @@ -126,15 +126,7 @@ public dPlugin setPrefix(String prefix) { return this; } - - ///////////////// - // Attributes - ///////// - - @Override - public String getAttribute(Attribute attribute) { - - if (attribute == null) return "null"; + public static void registerTags() { // <--[tag] // @attribute @@ -142,10 +134,13 @@ public String getAttribute(Attribute attribute) { // @description // Gets the name of this plugin. // --> - if (attribute.startsWith("name")) { - return new Element(plugin.getName()) - .getAttribute(attribute.fulfill(1)); - } + registerTag("name", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dPlugin) object).plugin.getName()) + .getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -153,10 +148,13 @@ public String getAttribute(Attribute attribute) { // @description // Gets the version for the plugin specified. // --> - if (attribute.startsWith("version")) { - return new Element(plugin.getDescription().getVersion()) - .getAttribute(attribute.fulfill(1)); - } + registerTag("version", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dPlugin) object).plugin.getDescription().getVersion()) + .getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -164,10 +162,13 @@ public String getAttribute(Attribute attribute) { // @description // Gets the description for the plugin specified. // --> - if (attribute.startsWith("description")) { - return new Element(plugin.getDescription().getDescription()) - .getAttribute(attribute.fulfill(1)); - } + registerTag("description", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element(((dPlugin) object).plugin.getDescription().getDescription()) + .getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -175,10 +176,13 @@ public String getAttribute(Attribute attribute) { // @description // Gets the list of authors for the plugin specified. // --> - if (attribute.startsWith("authors")) { - return new dList(plugin.getDescription().getAuthors()) - .getAttribute(attribute.fulfill(1)); - } + registerTag("authors", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new dList(((dPlugin) object).plugin.getDescription().getAuthors()) + .getAttribute(attribute.fulfill(1)); + } + }); // <--[tag] // @attribute @@ -187,10 +191,42 @@ public String getAttribute(Attribute attribute) { // Always returns 'Plugin' for dPlugin 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("Plugin").getAttribute(attribute.fulfill(1)); + registerTag("type", new TagRunnable() { + @Override + public String run(Attribute attribute, dObject object) { + return new Element("Plugin").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); + } + ///////////////// + // Attributes + ///////// + + @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);