Skip to content

Commit

Permalink
Rewrote dPlugin tags in new registerTag format.
Browse files Browse the repository at this point in the history
  • Loading branch information
Talamar1 committed Jul 23, 2015
1 parent f8e85fd commit b35587c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/main/java/net/aufdemrand/denizen/Denizen.java
Expand Up @@ -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@


Expand Down
98 changes: 67 additions & 31 deletions 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 {


Expand Down Expand Up @@ -126,59 +126,63 @@ 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 <pl@plugin.name>
// @returns Element
// @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 <pl@plugin.version>
// @returns Element
// @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 <pl@plugin.description>
// @returns Element
// @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 <pl@plugin.authors>
// @returns dList
// @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 <pl@plugin.type>
Expand All @@ -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<String, TagRunnable> registeredTags = new HashMap<String, TagRunnable>();

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);
Expand Down

0 comments on commit b35587c

Please sign in to comment.