/
Element.java
143 lines (115 loc) · 5.08 KB
/
Element.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package net.aufdemrand.denizen.utilities.arguments;
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;
import java.util.Arrays;
public class Element implements dScriptArgument {
/**
*
* @param string the string or dScript argument String
* @return a dScript dList
*
*/
public static Element valueOf(String string) {
if (string == null) return null;
String prefix = null;
// Strip prefix (ie. targets:...)
if (string.split(":").length > 1) {
prefix = string.split(":", 2)[0];
string = string.split(":", 2)[1];
}
return new Element(prefix, string);
}
private String prefix;
private String element;
public Element(String string) {
this.prefix = "element";
this.element = string;
}
public Element(String prefix, String string) {
if (prefix == null) this.prefix = "element";
else this.prefix = prefix;
this.element = string;
}
@Override
public String getDefaultPrefix() {
return prefix;
}
@Override
public String debug() {
return (prefix + "='<A>" + element + "<G>' ");
}
@Override
public String as_dScriptArg() {
return prefix + ":" + element;
}
@Override
public dScriptArgument setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
if (attribute.startsWith("asint"))
try { return new Element(String.valueOf(Integer.valueOf(element)))
.getAttribute(attribute.fulfill(1)); }
catch (NumberFormatException e) { dB.echoError("'" + element + "' is not a valid Integer."); }
if (attribute.startsWith("asdouble"))
try { return new Element(String.valueOf(Double.valueOf(element)))
.getAttribute(attribute.fulfill(1)); }
catch (NumberFormatException e) { dB.echoError("'" + element + "' is not a valid Double."); }
if (attribute.startsWith("asmoney")) {
try {
DecimalFormat d = new DecimalFormat("0.00");
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."); }
}
if (attribute.startsWith("asboolean"))
return new Element(Boolean.valueOf(element).toString())
.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;
return new Element(String.valueOf(element.substring(beginning_index, ending_index)))
.getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug")) {
return new Element(debug())
.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);
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) : " ");
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;
}
}