Skip to content

Commit

Permalink
Update to OO tag system.
Browse files Browse the repository at this point in the history
  • Loading branch information
aufdemrand committed Apr 5, 2013
1 parent 3bb178b commit 2e14172
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 27 deletions.
Expand Up @@ -5,6 +5,7 @@

import net.aufdemrand.denizen.npc.dNPC;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.citizensnpcs.api.CitizensAPI;
Expand Down Expand Up @@ -87,7 +88,9 @@ public class ReplaceableTagEvent extends Event {
// Component pattern that matches groups of characters that are not
// [] or . and that optionally contain [] and a . at the end
Pattern componentRegex = Pattern.compile("[^\\[\\]\\.]+(\\[.*?\\])?(\\.)?");


public String raw_tag;

public ReplaceableTagEvent(OfflinePlayer player, dNPC npc, String tag) {
this(player, npc, tag, null);
}
Expand Down Expand Up @@ -133,6 +136,10 @@ public ReplaceableTagEvent(OfflinePlayer player, dNPC npc, String tag, ScriptEnt
// and any trailing spaces
}

// Alternatives are stripped, base context is stripped, let's remember the raw tag for
// the attributer.
raw_tag = tag;

// Get value
Matcher bracketMatcher = null;
Matcher valueMatcher = valueRegex.matcher(tag);
Expand All @@ -158,7 +165,7 @@ public ReplaceableTagEvent(OfflinePlayer player, dNPC npc, String tag, ScriptEnt
String[] contexts = new String[4];
String tagPart = null;
int n = 0;

Matcher componentMatcher = componentRegex.matcher(tag);

while (componentMatcher.find() && n < 4)
Expand Down
48 changes: 30 additions & 18 deletions src/main/java/net/aufdemrand/denizen/tags/Attribute.java
@@ -1,13 +1,7 @@
package net.aufdemrand.denizen.tags;

import net.aufdemrand.denizen.Denizen;
import net.aufdemrand.denizen.events.ReplaceableTagEvent;
import net.aufdemrand.denizen.npc.dNPC;

import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.tags.core.*;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -22,28 +16,47 @@

public class Attribute {

String attribute;
List<String> attributes;

ScriptEntry scriptEntry;

public ScriptEntry getScriptEntry() {
return scriptEntry;
}

public Attribute(String attributes, ScriptEntry scriptEntry) {

this.scriptEntry = scriptEntry;

Pattern attributer = Pattern.compile("[^\\[\\]\\.]+(\\[.*?\\])?");
List<String> matches = new ArrayList<String>();
Matcher matcher = attributer.matcher(attributes);

while (matcher.find()) {
matches.add(matcher.group());
}

public Attribute(String attribute) {
this.attribute = attribute.toLowerCase();
this.attributes = matches;
}

public boolean startsWith(String string) {
string = string.toLowerCase();
if (attribute.startsWith(string)) return true;
if (attributes.isEmpty()) return false;
if (attributes.get(0).toLowerCase().startsWith(string)) return true;
return false;
}

public boolean startsWith(String string, int attribute) {
string = string.toLowerCase();
if (this.attribute.split("\\.")[attribute - 1].startsWith(string)) return true;
if (attributes.isEmpty()) return false;
if (attributes.size() < attribute) return false;
if (attributes.get(attribute - 1).toLowerCase().startsWith(string)) return true;
return false;
}

public Attribute fulfill(int attributes) {
if (attribute.split("\\.").length >= attributes)
attribute = "";
else attribute = attribute.split("\\.", attributes + 1)[attributes];
for (int x = attributes; x > 0; x--)
this.attributes.remove(0);
return this;
}

Expand All @@ -68,9 +81,8 @@ public int getIntContext(int attribute) {
}

private String getAttribute(int num) {
int size = attribute.split("\\.").length;
if (num > size) return "";
else return attribute.split("\\.")[num - 1];
if (attributes.size() < num) return "";
else return attributes.get(num - 1);
}

}
Expand Up @@ -3,7 +3,9 @@
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.util.StringUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
Expand Down Expand Up @@ -93,8 +95,8 @@ public String getAttribute(Attribute attribute) {
.getAttribute(attribute.fulfill(1));

if (attribute.startsWith("substring")) { // suibstring[2|8]
int beginning_index = Integer.valueOf(attribute.getContext(1).split("\\|")[0]) - 1;
int ending_index = Integer.valueOf(attribute.getContext(1).split("\\|")[1]) - 1;
int beginning_index = Integer.valueOf(attribute.getContext(1).split(",")[0]) - 1;
int ending_index = Integer.valueOf(attribute.getContext(1).split(",")[1]) - 1;
return new Element(String.valueOf(element.substring(beginning_index, ending_index)))
.getAttribute(attribute.fulfill(1));
}
Expand Down Expand Up @@ -122,12 +124,17 @@ public String getAttribute(Attribute attribute) {
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);
return new dList(Arrays.asList(element.split(split_string, limit))).getAttribute(attribute.fulfill(2));
}
if (split_string.toUpperCase().startsWith("regex:"))
return new dList(Arrays.asList(element.split(split_string.split(":", 2)[1], limit))).getAttribute(attribute.fulfill(1));
else
return new dList(Arrays.asList(StringUtils.split(element, split_string, limit))).getAttribute(attribute.fulfill(1)); }

if (attribute.startsWith("split")) {
String split_string = (attribute.hasContext(1) ? attribute.getContext(1) : " ");
return new dList(Arrays.asList(element.split(split_string))).getAttribute(attribute.fulfill(1));
if (split_string.toUpperCase().startsWith("regex:"))
return new dList(Arrays.asList(element.split(split_string.split(":", 2)[1]))).getAttribute(attribute.fulfill(1));
else
return new dList(Arrays.asList(StringUtils.split(element, split_string))).getAttribute(attribute.fulfill(1));
}

return element;
Expand Down
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class dList extends ArrayList<String> implements dScriptArgument {

Expand All @@ -32,13 +33,13 @@ public dList(String prefix, String items) {
addAll(Arrays.asList(items.split("\\|")));
}

public dList(String prefix, java.util.List<String> items) {
public dList(String prefix, List<String> items) {
if (prefix == null) this.prefix = "list";
else this.prefix = prefix;
addAll(items);
}

public dList(java.util.List<String> items) {
public dList(List<String> items) {
this.prefix = "list";
addAll(items);
}
Expand Down Expand Up @@ -85,6 +86,7 @@ public String getAttribute(Attribute attribute) {
if (attribute == null) return null;

if (attribute.startsWith("ascslist")) {
if (isEmpty()) return new Element("").getAttribute(attribute.fulfill(1));
StringBuilder dScriptArg = new StringBuilder();
for (String item : this)
dScriptArg.append(item + ", ");
Expand All @@ -93,6 +95,7 @@ public String getAttribute(Attribute attribute) {
}

if (attribute.startsWith("get")) {
if (isEmpty()) return new Element("").getAttribute(attribute.fulfill(1));
int index = attribute.getIntContext(1);
if (index > size()) return null;
String item;
Expand Down

0 comments on commit 2e14172

Please sign in to comment.