/
Element.java
104 lines (82 loc) · 3.2 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
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 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 List
*
*/
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")) {
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));
}
return element;
}
}