Skip to content

Commit

Permalink
Add server.flag[name] tag. Make unfilled tag attributes return an error.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Jul 1, 2013
1 parent 97866a3 commit 01fc4a3
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 29 deletions.
23 changes: 17 additions & 6 deletions src/main/java/net/aufdemrand/denizen/objects/Element.java
Expand Up @@ -132,8 +132,8 @@ public String getAttribute(Attribute attribute) {
|| attribute.startsWith("as_money")) {
try {
DecimalFormat d = new DecimalFormat("0.00");
return new Element(String.valueOf(d.format(Double.valueOf(element))))
.getAttribute(attribute.fulfill(1)); }
return new Element(String.valueOf(d.format(Double.valueOf(element))))
.getAttribute(attribute.fulfill(1)); }
catch (NumberFormatException e) {
dB.echoError("'" + element + "' is not a valid Money format.");
return null;
Expand Down Expand Up @@ -176,12 +176,12 @@ public String getAttribute(Attribute attribute) {
if (attribute.startsWith("asduration")
|| attribute.startsWith("as_duration"))
return Duration.valueOf(element).getAttribute(attribute.fulfill(1));

if (attribute.startsWith("contains")) {
String contains = attribute.getContext(1);

if (contains.toLowerCase().startsWith("regex:")) {

if (Pattern.compile(contains.substring(("regex:").length()), Pattern.CASE_INSENSITIVE).matcher(element).matches())
return new Element("true").getAttribute(attribute.fulfill(1));
else return new Element("false").getAttribute(attribute.fulfill(1));
Expand All @@ -204,7 +204,7 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))

if (attribute.startsWith("strip_color"))
return new Element(String.valueOf(ChatColor.stripColor(element))).getAttribute(attribute.fulfill(1));

if (attribute.startsWith("split") && attribute.startsWith("limit", 2)) {
String split_string = (attribute.hasContext(1) ? attribute.getContext(1) : " ");
Integer limit = (attribute.hasContext(2) ? attribute.getIntContext(2) : 1);
Expand Down Expand Up @@ -256,7 +256,18 @@ else if (element.toLowerCase().contains(contains.toLowerCase()))
.getAttribute(attribute.fulfill(1));
}

return element;
// Unfilled attributes past this point probably means the tag is spelled
// incorrectly. So instead of just passing through what's been resolved
// so far, 'null' shall be returned with an error message.

if (attribute.attributes.size() > 0) {
dB.echoError("Unfilled attributes '" + attribute.attributes.toString() + "'" +
"for tag <" + attribute.getOrigin() + ">!");
return "null";
} else {
dB.log("Filled tag <" + attribute.getOrigin() + "> with '" + element + "'.");
return element;
}
}

}
7 changes: 7 additions & 0 deletions src/main/java/net/aufdemrand/denizen/objects/dPlayer.java
Expand Up @@ -396,6 +396,13 @@ else if ((float) getPlayerEntity().getFoodLevel() / maxHunger < 1)
return new dItem(getPlayerEntity().getItemOnCursor())
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("selected_npc")) {
if (getPlayerEntity().hasMetadata("selected"))
return dNPC.valueOf(String.valueOf(getPlayerEntity().getMetadata("selected").get(0)))
.getAttribute(attribute.fulfill(1));
else return "null";
}

if (attribute.startsWith("allowed_flight"))
return new Element(String.valueOf(getPlayerEntity().getAllowFlight()))
.getAttribute(attribute.fulfill(1));
Expand Down
Expand Up @@ -112,9 +112,6 @@ else if (aH.matchesValueArg("npcid, npc", arg, aH.ArgumentType.String)) {
if (scriptEntry.has_tags)
scriptEntry.setArguments(TagManager.fillArguments(scriptEntry.getArguments(), scriptEntry, false));

// dBug the filled arguments
dB.echoDebug(ChatColor.AQUA + "+> " + ChatColor.DARK_GRAY + "Filled tags: " + scriptEntry.getArguments().toString());

// Parse the rest of the arguments for execution.
command.parseArgs(scriptEntry);

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/aufdemrand/denizen/tags/Attribute.java
Expand Up @@ -23,13 +23,19 @@ public class Attribute {
ScriptEntry scriptEntry;

String raw_tag;
String origin;

public ScriptEntry getScriptEntry() {
return scriptEntry;
}

public String getOrigin() {
return origin;
}

public Attribute(String attributes, ScriptEntry scriptEntry) {
raw_tag = attributes;
origin = attributes;
this.scriptEntry = scriptEntry;

if (attributes == null) {
Expand Down
60 changes: 40 additions & 20 deletions src/main/java/net/aufdemrand/denizen/tags/core/UtilTags.java
Expand Up @@ -7,7 +7,9 @@

import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.events.ReplaceableTagEvent;
import net.aufdemrand.denizen.flags.FlagManager;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
Expand All @@ -24,13 +26,31 @@ public UtilTags(Denizen denizen) {
denizen.getServer().getPluginManager().registerEvents(this, denizen);
}


@EventHandler
public void mathTags(ReplaceableTagEvent event) {
if (!(event.matches("m") || event.matches("math"))) return;
Double evalulation = new DoubleEvaluator().evaluate(event.getValue());
event.setReplaced(String.valueOf(evalulation));
}

@EventHandler
public void serverTags(ReplaceableTagEvent event) {
if (!event.matches("server, svr")) return;
Attribute attribute = new Attribute(event.raw_tag, event.getScriptEntry()).fulfill(1);
if (attribute.startsWith("flag")) {
if (attribute.hasContext(1)) {
if (FlagManager.serverHasFlag(attribute.getContext(1)))
event.setReplaced(new dList(DenizenAPI.getCurrentInstance().flagManager()
.getGlobalFlag(attribute.getContext(1)))
.getAttribute(attribute.fulfill(1)));
event.setReplaced("null");
}
else event.setReplaced("null");
}
}


@EventHandler
public void utilTags(ReplaceableTagEvent event) {
if (!event.matches("UTIL, U")) return;
Expand All @@ -41,7 +61,7 @@ public void utilTags(ReplaceableTagEvent event) {
String subTypeContext = event.getSubTypeContext() != null ? event.getSubTypeContext().toUpperCase() : "";
String specifier = event.getSpecifier() != null ? event.getSpecifier() : "";
String specifierContext = event.getSpecifierContext() != null ? event.getSpecifierContext().toUpperCase() : "";

if (type.equalsIgnoreCase("RANDOM")) {
if (subType.equalsIgnoreCase("INT")) {
if (specifier.equalsIgnoreCase("TO")) {
Expand All @@ -60,10 +80,10 @@ public void utilTags(ReplaceableTagEvent event) {
}
}
}

else if (subType.equalsIgnoreCase("ELEMENT")) {
dList list = dList.valueOf(subTypeContext);
event.setReplaced(list.get(new Random().nextInt(list.size())));
dList list = dList.valueOf(subTypeContext);
event.setReplaced(list.get(new Random().nextInt(list.size())));
}

else if (subType.equalsIgnoreCase("UUID"))
Expand All @@ -76,20 +96,20 @@ else if (type.equalsIgnoreCase("SUBSTR")
String text = event.getTypeContext();
int from = 1;
int to = text.length() + 1;

if (subType.equalsIgnoreCase("AFTER")) {
from = text.toUpperCase().indexOf(subTypeContext) + subTypeContext.length() + 1;
}

if (subType.equalsIgnoreCase("BEFORE")) {
to = text.toUpperCase().indexOf(subTypeContext) + 1;
}

try {
if (subType.equalsIgnoreCase("FROM"))
from = Integer.valueOf(subTypeContext);
} catch (NumberFormatException e) { }

try {
if (specifier.equalsIgnoreCase("TO"))
to = Integer.valueOf(specifierContext);
Expand Down Expand Up @@ -117,19 +137,19 @@ else if (type.equalsIgnoreCase("LOWERCASE")) {
String item_to_uppercase = event.getTypeContext();
event.setReplaced(item_to_uppercase.toLowerCase());
}

else if (type.equalsIgnoreCase("DATE")) {
Date currentDate = new Date();
SimpleDateFormat format = new SimpleDateFormat();
if (subType.equalsIgnoreCase("TIME")) {
if (specifier.equalsIgnoreCase("24HOUR")) {
format.applyPattern("k:mm");
} else format.applyPattern("K:mm a");
} else format.applyPattern("EEE, MMM d, yyyy");
event.setReplaced(format.format(currentDate));
Date currentDate = new Date();
SimpleDateFormat format = new SimpleDateFormat();

if (subType.equalsIgnoreCase("TIME")) {
if (specifier.equalsIgnoreCase("24HOUR")) {
format.applyPattern("k:mm");
} else format.applyPattern("K:mm a");

} else format.applyPattern("EEE, MMM d, yyyy");

event.setReplaced(format.format(currentDate));
}

else if (type.equalsIgnoreCase("AS_ELEMENT")) {
Expand Down

0 comments on commit 01fc4a3

Please sign in to comment.