-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ScriptContainer.java
278 lines (232 loc) · 8.29 KB
/
ScriptContainer.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package net.aufdemrand.denizencore.scripts.containers;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.scripts.*;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import net.aufdemrand.denizencore.utilities.debugging.Debuggable;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ScriptContainer implements Debuggable {
// <--[language]
// @name Script Container
// @group Script Container System
// @description
// Script Containers are the basic structure that Denizen uses inside its YAML-based scripting files found in your
// plugins/Denizen/scripts/ folder. Regardless of type, all script containers have basic parts that can usually be
// described as keys, list keys, parent keys, child keys, values, and list values. While specific container types
// probably have more specific names, just remember that no matter how complicated a script, this basic structure
// still applies.
//
// It's important to keep in mind that all child keys, including all the main keys of the script, must line up with
// one another, hierarchically. If you are familiar with YAML, great, because all script containers use it at the
// core. Every value, in one way or another, belongs to some kind of 'key'. To define a key, use a string value plus
// a colon (:). Keys can have a single value, a list value, or own another key:
//
// <code>
// script name:
// key: value
// list key:
// - list value
// - ...
// parent key:
// child key: value
// </code>
//
// And here's a container, put into a more familiar context:
//
// <code>
// a haiku script:
// type: task
// script:
// - narrate "A simple script,"
// - narrate "with a key value relationship."
// - narrate "Oh look, a list!"
// </code>
//
// -->
public ScriptContainer(YamlConfiguration configurationSection, String scriptContainerName) {
if (configurationSection == null) {
dB.echoError("Null configuration section while generating a ScriptContainer?!");
}
contents = configurationSection;
this.name = scriptContainerName.toUpperCase();
}
// The contents of the script container
YamlConfiguration contents;
/**
* Gets the contents of the container.
*
* @return a ConfigurationSection object
*/
public YamlConfiguration getContents() {
return contents;
}
/**
* Casts this container to a specify type of script container. Must be a valid
* container type of the type casting to.
*
* @param type the class of the ScriptContainer casting to
* @param <T> the ScriptContainer object
* @return a ScriptContainer of the type specified
*/
public <T extends ScriptContainer> T getAsContainerType(Class<T> type) {
return type.cast(this);
}
// <--[language]
// @name Script Name
// @group Script Container System
// @description
// Typically refers to the name of a script container. When using the object fetcher with dScript objects,
// (s@script_name), the script_name referred to is the name of the container.
//
// <code>
// script name: <--- script name
// type: script_type
// script: <--- base script
// - script entries
// - ...
// local script: <--- local script path
// - script entries
// - ...
// </code>
//
// -->
// The name of the script container
private String name;
/**
* Gets the name of the script container.
*
* @return the script container name.
*/
public String getName() {
return name;
}
public String getFileName() {
return ScriptHelper.getSource(getName());
}
public String getOriginalName() {
return ScriptHelper.getOriginalName(getName());
}
/**
* Gets a dScript object that represents this container.
*
* @return a dScript object linking this script container.
*/
public dScript getAsScriptArg() {
return dScript.valueOf(name);
}
// <--[language]
// @name Script Type
// @group Script Container System
// @description
// The type of container that a script is in. For example, 'task script' is a script type that has some sort of
// utility script or
//
// <code>
// script name:
// type: script_type <--- script type
// script:
// - script entries
// - ...
// </code>
//
// -->
/**
* Gets the value of the type: node specified in the script container structure.
*
* @return the type of container
*/
public String getContainerType() {
return contents.contains("TYPE")
? contents.getString("TYPE").toUpperCase()
: null;
}
/**
* Checks the ConfigurationSection for the key/path to key specified.
*
* @param path the path of the key
* @return true if the key exists
*/
public boolean contains(String path) {
return contents.contains(path.toUpperCase());
}
public String getString(String path) {
return contents.getString(path.toUpperCase());
}
public String getString(String path, String def) {
return contents.getString(path.toUpperCase(), def);
}
public List<String> getStringList(String path) {
return contents.getStringList(path.toUpperCase());
}
public YamlConfiguration getConfigurationSection(String path) {
if (path.length() == 0) {
return contents;
}
return contents.getConfigurationSection(path.toUpperCase());
}
public void set(String path, Object object) {
contents.set(path.toUpperCase(), object);
}
public ScriptEntrySet baseEntries = null;
public List<ScriptEntry> getBaseEntries(ScriptEntryData data) {
if (baseEntries == null) {
baseEntries = getSetFor("script");
}
return cleanDup(data, baseEntries);
}
public static List<ScriptEntry> cleanDup(ScriptEntryData data, ScriptEntrySet set) {
if (set == null) {
return new ArrayList<ScriptEntry>();
}
set = set.duplicate();
for (ScriptEntry entry : set.entries) {
entry.entryData = data.clone();
}
return set.entries;
}
public List<ScriptEntry> getEntries(ScriptEntryData data, String path) {
if (path == null) {
path = "script";
}
return cleanDup(data, getSetFor(path));
}
public ScriptEntrySet getSetFor(String path) {
ScriptEntrySet got = scriptsMap.get(path);
if (got != null) {
return got;
}
List<Object> stringEntries = contents.getList(path);
if (stringEntries == null || stringEntries.size() == 0) {
return null;
}
List<ScriptEntry> entries = ScriptBuilder.buildScriptEntries(stringEntries, this, null);
got = new ScriptEntrySet(entries);
scriptsMap.put(path, got);
return got;
}
private Map<String, ScriptEntrySet> scriptsMap = new HashMap<String, ScriptEntrySet>();
/////////////
// DEBUGGABLE
/////////
// Cached debug value to avoid repeated complex YAML calls
private Boolean shouldDebug = null;
@Override
public boolean shouldDebug() {
if (shouldDebug == null) {
shouldDebug = (!(contents.contains("debug") && contents.getString("debug").equalsIgnoreCase("false")));
}
return shouldDebug;
}
@Override
public boolean shouldFilter(String criteria) throws Exception {
return name.equalsIgnoreCase(criteria.replace("s@", ""));
}
@Override
public String toString() {
return "s@" + CoreUtilities.toLowerCase(getName());
}
}