/
dScript.java
201 lines (171 loc) · 6.24 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.core.CooldownCommand;
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 CONTAINER_PATTERN = Pattern.compile("(s@|)(.+)",
Pattern.CASE_INSENSITIVE);
/**
* Gets a dContainer Object from a dScript argument.
*
* @param string the dScript argument String
* @return a Script, or null if incorrectly formatted
*/
@ObjectFetcher("s")
public static dScript valueOf(String string) {
Matcher m = CONTAINER_PATTERN.matcher(string);
if (m.matches()) {
dScript script = new dScript(m.group(2));
// Make sure it's valid.
if (script.isValid()) return script;
}
return null;
}
public static boolean matches(String string) {
Matcher m = CONTAINER_PATTERN.matcher(string);
if (m.matches()) {
dScript script = new dScript(m.group(2));
// Make sure it's valid.
if (script.isValid()) return true;
}
return false;
}
private ScriptContainer container;
private String prefix = "Container";
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 getObjectType() {
return "Container";
}
@Override
public String identify() {
return "s@" + name;
}
@Override
public String toString() {
return identify();
}
/**
* 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 true;
}
@Override
public dObject setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
// <--[tag]
// @attribute <s@script.container_type>
// @returns Element
// @description
// Returns the container type of a dScript.
// -->
if (attribute.startsWith("container_type"))
return new Element(container.getType())
.getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <s@script.cooled_down[<player>]>
// @returns Element(Boolean)
// @description
// Returns true if the script has been cooled down for the
// player (defaults to current). Otherwise, returns false.
// -->
if (attribute.startsWith("cooled_down")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
return new Element(container.checkCooldown(player))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <s@script.requirements[<player>].check[<path>]>
// @returns Element
// @description
// Returns true if the player specified (defaults to current) has the
// requirement. Otherwise, returns false.
// -->
if (attribute.startsWith("requirements.check")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
if (attribute.hasContext(2))
return new Element(container.checkRequirements(player,
attribute.getScriptEntry().getNPC(),
attribute.getContext(2)))
.getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <s@script.cooldown[<player>]>
// @returns Duration
// @description
// Returns the time left for the player to cooldown for the script.
// -->
if (attribute.startsWith("cooldown")) {
dPlayer player = (attribute.hasContext(1) ? dPlayer.valueOf(attribute.getContext(1))
: attribute.getScriptEntry().getPlayer());
return CooldownCommand.getCooldownDuration((player != null ? player.getName() : null), container.getName())
.getAttribute(attribute.fulfill(1));
}
return new Element(identify()).getAttribute(attribute);
}
}