-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
dScript.java
137 lines (113 loc) · 3.81 KB
/
dScript.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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.ScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class dScript implements dObject {
final public static Pattern matchesScriptPtrn = Pattern.compile("(?:.+:|)(.+)", Pattern.CASE_INSENSITIVE);
/**
* Gets a Script Argument Object from a dScript argument.
*
* @param string the dScript argument String
* @return a Script, or null if incorrectly formatted
*/
public static dScript valueOf(String string) {
Matcher m = matchesScriptPtrn.matcher(string);
if (m.matches()) {
dScript script = new dScript(m.group(1));
// Make sure it's valid.
if (script.isValid()) return script;
}
return null;
}
public static boolean matches(String string) {
Matcher m = matchesScriptPtrn.matcher(string);
if (m.matches()) {
dScript script = new dScript(m.group(1));
// Make sure it's valid.
if (script.isValid()) return true;
}
return false;
}
private ScriptContainer container;
private String prefix = "Script";
private String name = null;
private boolean valid = false;
/**
* Creates a script object from a script name. If the script is valid, {@link #isValid()} will retrun true.
*
* @param scriptName
*/
public dScript(String scriptName) {
// Required for tests
if (DenizenAPI.getCurrentInstance() == null) return;
if (ScriptRegistry.getScriptContainer(scriptName) != null) {
container = ScriptRegistry.getScriptContainer(scriptName);
name = scriptName.toUpperCase();
valid = true;
}
}
/**
* Confirms that the script references a valid name and type in current loaded ScriptsContainers.
*
* @return true if the script is valid, false if the script was not found, or the type is missing
*/
public boolean isValid() {
return valid;
}
/**
* Gets the type of the ScriptContainer, as defined by the TYPE: key.
*
* @return the type of the Script Container
*/
public String getType() {
return (container != null ? container.getType() : "invalid");
}
@Override
public String identify() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
/**
* Gets the name of the ScriptContainer.
*
* @return script name
*/
public String getName() {
return name;
}
/**
* Gets the contents of the scriptContainer.
*
* @return ConfigurationSection of the script contents
*/
public ScriptContainer getContainer() {
return container;
}
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return "<G>" + prefix + "='<A>" + name + "<Y>(" + getType() + ")<G>' ";
}
@Override
public boolean isUnique() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}
public String dScriptArgValue() {
return name;
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
}