/
YamlCommand.java
579 lines (505 loc) · 23.2 KB
/
YamlCommand.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.events.bukkit.ReplaceableTagEvent;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.scripts.ScriptHelper;
import net.aufdemrand.denizencore.utilities.YamlConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.json.JSONObject;
import java.io.*;
import java.net.URLDecoder;
import java.util.*;
public class YamlCommand extends AbstractCommand implements Listener {
@Override
public void onEnable() {
Bukkit.getServer().getPluginManager().registerEvents(this, DenizenAPI.getCurrentInstance());
}
Map<String, YamlConfiguration> yamls = new HashMap<String, YamlConfiguration>();
private YamlConfiguration getYaml(String id) {
if (id == null) return null;
return yamls.get(id.toUpperCase());
}
public static enum Action { LOAD, UNLOAD, CREATE, WRITE, SAVE, SET }
public static enum YAML_Action { SET_VALUE, INCREASE, DECREASE, MULTIPLY,
DIVIDE, INSERT, REMOVE, SPLIT, DELETE }
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean isSet = false;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("LOAD")) {
scriptEntry.addObject("action", new Element("LOAD"));
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("SAVEFILE", "FILESAVE")) {
scriptEntry.addObject("action", new Element("SAVE"));
scriptEntry.addObject("filename", arg.asElement());
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("CREATE")) {
scriptEntry.addObject("action", new Element("CREATE"));
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("SET")) {
scriptEntry.addObject("action", new Element("SET"));
isSet = true;
}
else if (!scriptEntry.hasObject("action") &&
arg.matches("UNLOAD")) {
scriptEntry.addObject("action", new Element("UNLOAD"));
}
else if (!scriptEntry.hasObject("action") &&
arg.matchesPrefix("WRITE")) {
scriptEntry.addObject("action", new Element("WRITE"));
scriptEntry.addObject("key", arg.asElement());
}
else if (!scriptEntry.hasObject("value") &&
arg.matchesPrefix("VALUE")) {
if (arg.matchesArgumentType(dList.class))
scriptEntry.addObject("value", arg.asType(dList.class));
else
scriptEntry.addObject("value", arg.asElement());
}
else if (!scriptEntry.hasObject("id") &&
arg.matchesPrefix("ID")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (!scriptEntry.hasObject("split") &&
arg.matches("split_list")) {
scriptEntry.addObject("split", Element.TRUE);
}
// Check for key:value/action
else if (isSet &&
!scriptEntry.hasObject("value") &&
arg.raw_value.split(":", 3).length == 2) {
String[] flagArgs = arg.raw_value.split(":", 2);
scriptEntry.addObject("key", new Element(flagArgs[0]));
if (flagArgs[1].equals("++") || flagArgs[1].equals("+")) {
scriptEntry.addObject("yaml_action", YAML_Action.INCREASE);
scriptEntry.addObject("value", new Element(1));
}
else if (flagArgs[1].equals("--") || flagArgs[1].equals("-")) {
scriptEntry.addObject("yaml_action", YAML_Action.DECREASE);
scriptEntry.addObject("value", new Element(1));
}
else if (flagArgs[1].equals("!")) {
scriptEntry.addObject("yaml_action", YAML_Action.DELETE);
scriptEntry.addObject("value", Element.FALSE);
}
else if (flagArgs[1].equals("<-")) {
scriptEntry.addObject("yaml_action", YAML_Action.REMOVE);
scriptEntry.addObject("value", Element.FALSE);
}
else {
// No ACTION, we're just setting a value...
scriptEntry.addObject("yaml_action", YAML_Action.SET_VALUE);
scriptEntry.addObject("value", new Element(flagArgs[1]));
}
}
// Check for key:action:value
else if (isSet &&
!scriptEntry.hasObject("value") &&
arg.raw_value.split(":", 3).length == 3) {
String[] flagArgs = arg.raw_value.split(":", 3);
scriptEntry.addObject("key", new Element(flagArgs[0]));
if (flagArgs[1].equals("->"))
scriptEntry.addObject("yaml_action", YAML_Action.INSERT);
else if (flagArgs[1].equals("<-"))
scriptEntry.addObject("yaml_action", YAML_Action.REMOVE);
else if (flagArgs[1].equals("||") || flagArgs[1].equals("|"))
scriptEntry.addObject("yaml_action", YAML_Action.SPLIT);
else if (flagArgs[1].equals("++") || flagArgs[1].equals("+"))
scriptEntry.addObject("yaml_action", YAML_Action.INCREASE);
else if (flagArgs[1].equals("--") || flagArgs[1].equals("-"))
scriptEntry.addObject("yaml_action", YAML_Action.DECREASE);
else if (flagArgs[1].equals("**") || flagArgs[1].equals("*"))
scriptEntry.addObject("yaml_action", YAML_Action.MULTIPLY);
else if (flagArgs[1].equals("//") || flagArgs[1].equals("/"))
scriptEntry.addObject("yaml_action", YAML_Action.DIVIDE);
else {
scriptEntry.addObject("yaml_action", YAML_Action.SET_VALUE);
scriptEntry.addObject("value", new Element(arg.raw_value.split(":", 2)[1]));
continue;
}
scriptEntry.addObject("value", new Element(flagArgs[2]));
}
else arg.reportUnhandled();
}
// Check for required arguments
if (!scriptEntry.hasObject("id"))
throw new InvalidArgumentsException("Must specify an id!");
if (!scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify an action!");
if (!scriptEntry.hasObject("key") &&
scriptEntry.getElement("action").asString().equalsIgnoreCase("write"))
throw new InvalidArgumentsException("Must specify a key!");
scriptEntry.defaultObject("value", new Element(""));
}
@Override
public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {
Element filename = scriptEntry.getElement("filename");
Element key = scriptEntry.getElement("key");
dObject value = scriptEntry.getdObject("value");
Element split = scriptEntry.getElement("split");
YAML_Action yaml_action = (YAML_Action) scriptEntry.getObject("yaml_action");
Element actionElement = scriptEntry.getElement("action");
Element idElement = scriptEntry.getElement("id");
YamlConfiguration yamlConfiguration;
dB.report(scriptEntry, getName(),
idElement.debug()
+ actionElement.debug()
+ (filename != null ? filename.debug() : "")
+ (yaml_action != null ? aH.debugObj("yaml_action", yaml_action.name()): "")
+ (key != null ? key.debug() : "")
+ (value != null ? value.debug() : "")
+ (split != null ? split.debug() : ""));
// Do action
Action action = Action.valueOf(actionElement.asString().toUpperCase());
String id = idElement.asString().toUpperCase();
switch (action) {
case LOAD:
File file = new File(DenizenAPI.getCurrentInstance().getDataFolder(), filename.asString());
if (!file.exists()) {
dB.echoError("File cannot be found!");
return;
}
try {
FileInputStream fis = new FileInputStream(file);
yamlConfiguration = YamlConfiguration.load(ScriptHelper.convertStreamToString(fis));
fis.close();
}
catch (Exception e) {
dB.echoError(e);
return;
}
if (yamls.containsKey(id))
yamls.remove(id);
if (yamlConfiguration != null)
yamls.put(id, yamlConfiguration);
break;
case UNLOAD:
if (yamls.containsKey(id))
yamls.remove(id);
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case SAVE:
if (yamls.containsKey(id)) {
try {
if (!Settings.allowStrangeYAMLSaves()) {
File fileObj = new File(DenizenAPI.getCurrentInstance().
getDataFolder().getAbsolutePath() + "/" + filename.asString());
String directory = URLDecoder.decode(System.getProperty("user.dir"));
if (!fileObj.getCanonicalPath().startsWith(directory)) {
dB.echoError("Outside-the-main-folder YAML saves disabled by administrator.");
return;
}
}
FileWriter fw = new FileWriter(DenizenAPI.getCurrentInstance()
.getDataFolder().getAbsolutePath() + "/" + filename.asString());
BufferedWriter writer = new BufferedWriter(fw);
writer.write(yamls.get(id).saveToString());
writer.close();
fw.close();
} catch (IOException e) {
dB.echoError(e);
}
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case WRITE:
if (yamls.containsKey(id)) {
if (value instanceof Element)
yamls.get(id).set(key.asString(), ((Element) value).asString());
else if (split != null && split.asBoolean())
yamls.get(id).set(key.asString(), value);
else
yamls.get(id).set(key.asString(), value.identify());
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case SET:
if (yamls.containsKey(id)) {
if (yaml_action == null || key == null || value == null) {
dB.echoError("Must specify a YAML action and value!");
return;
}
YamlConfiguration yaml = yamls.get(id);
int index = -1;
if (key.asString().contains("[")) {
try {
index = Integer.valueOf(key.asString().split("\\[")[1].replace("]", "")) - 1;
} catch (Exception e) { index = -1; }
key = Element.valueOf(key.asString().split("\\[")[0]);
}
String keyStr = key.asString();
String valueStr = value.identify();
switch (yaml_action) {
case INCREASE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) + aH.getIntegerFrom(valueStr)));
break;
case DECREASE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "0")) - aH.getIntegerFrom(valueStr)));
break;
case MULTIPLY:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) * aH.getIntegerFrom(valueStr)));
break;
case DIVIDE:
Set(yaml, index, keyStr, String.valueOf(aH.getIntegerFrom(Get(yaml, index, keyStr, "1")) / aH.getIntegerFrom(valueStr)));
break;
case DELETE:
yaml.set(keyStr, null);
break;
case SET_VALUE:
Set(yaml, index, keyStr, valueStr);
break;
case INSERT:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
list = new ArrayList<String>();
list.add(valueStr);
yaml.set(keyStr, list);
break;
}
case REMOVE:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
break;
if (index > -1 && index < list.size()) {
list.remove(index);
}
else {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equalsIgnoreCase(valueStr)) {
list.remove(i);
break;
}
}
yaml.set(keyStr, list);
break;
}
}
case SPLIT:
{
List<String> list = yaml.getStringList(keyStr);
if (list == null)
list = new ArrayList<String>();
list.addAll(dList.valueOf(valueStr));
yaml.set(keyStr, list);
break;
}
}
}
else
dB.echoError("Unknown YAML ID '" + id + "'");
break;
case CREATE:
if (yamls.containsKey(id))
yamls.remove(id);
yamlConfiguration = new YamlConfiguration();
yamls.put(id.toUpperCase(), yamlConfiguration);
break;
}
}
public String Get(YamlConfiguration yaml, int index, String key, String def) {
if (index == -1) {
return yaml.getString(key, def);
}
else {
List<String> list = yaml.getStringList(key);
if (index < 0)
index = 0;
if (index > list.size())
index = list.size() -1;
if (list.size() == 0)
return "";
return list.get(index);
}
}
public void Set(YamlConfiguration yaml, int index, String key, String value) {
if (index == -1) {
yaml.set(key, value);
}
else {
List<String> list = yaml.getStringList(key);
if (index < 0)
index = 0;
if (index > list.size())
index = list.size() -1;
if (list.size() == 0)
list.add("");
list.set(index, value);
yaml.set(key, list);
}
}
@EventHandler
public void yaml(ReplaceableTagEvent event) {
if (!event.matches("yaml")) return;
Attribute attribute = event.getAttributes();
// <--[tag]
// @attribute <yaml.list>
// @returns dList
// @description
// Returns a list of all currently loaded YAML ID's.
// -->
if (attribute.getAttribute(2).equalsIgnoreCase("list")) {
dList list = new dList();
list.addAll(yamls.keySet());
event.setReplaced(list.getAttribute(attribute.fulfill(2)));
return;
}
// YAML tag requires name context and type context.
if (!event.hasNameContext() || !(event.hasTypeContext() || attribute.getAttribute(2).equalsIgnoreCase("to_json"))) {
dB.echoError("YAML tag '" + event.raw_tag + "' is missing required context. Tag replacement aborted.");
return;
}
// Set id (name context) and path (type context)
String id = event.getNameContext();
String path = event.getTypeContext();
// Check if there is a yaml file loaded with the specified id
if (!yamls.containsKey(id.toUpperCase())) {
dB.echoError("YAML tag '" + event.raw_tag + "' has specified an invalid ID, or the specified id has already" +
" been closed. Tag replacement aborted. ID given: '" + id + "'.");
return;
}
// Catch up with what has already been processed.
attribute.fulfill(1);
//
// Check attributes
//
// <--[tag]
// @attribute <yaml[<id>].contains[<path>]>
// @returns Element(Boolean)
// @description
// Returns true if the file has the specified path.
// Otherwise, returns false.
// -->
if (attribute.startsWith("contains")) {
event.setReplaced(new Element(getYaml(id).contains(path))
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <yaml[<id>].is_list[<path>]>
// @returns Element(Boolean)
// @description
// Returns true if the specified path results in a list.
// -->
if (attribute.startsWith("is_list")) {
event.setReplaced(new Element(getYaml(id).isList(path))
.getAttribute(attribute.fulfill(1)));
return;
}
// <--[tag]
// @attribute <yaml[<id>].read[<path>]>
// @returns Element
// @description
// Returns the value of the key at the path.
// If the key is a list, returns a dList instead.
// -->
if (attribute.startsWith("read")) {
attribute.fulfill(1);
if (getYaml(id).isList(path)) {
List<String> value = getYaml(id).getStringList(path);
if (value == null) {
// If value is null, the key at the specified path didn't exist.
return;
}
else {
event.setReplaced(new dList(value).getAttribute(attribute));
return;
}
}
else {
String value = getYaml(id).getString(path);
if (value == null) {
// If value is null, the key at the specified path didn't exist.
return;
}
else {
event.setReplaced(new Element(value).getAttribute(attribute));
return;
}
}
}
// <--[tag]
// @attribute <yaml[<id>].list_deep_keys[<path>]>
// @returns dList
// @description
// Returns a dList of all the keys at the path and all subpaths.
// -->
if (attribute.startsWith("list_deep_keys")) {
Set<String> keys;
if (path != null && path.length() > 0) {
YamlConfiguration section = getYaml(id).getConfigurationSection(path);
if (section == null) {
return;
}
keys = section.getKeys(true);
}
else {
keys = getYaml(id).getKeys(true);
}
if (keys == null) {
return;
} else {
ArrayList<String> list = new ArrayList<String>();
list.addAll(keys);
event.setReplaced(new dList(list).getAttribute(attribute.fulfill(1)));
return;
}
}
// <--[tag]
// @attribute <yaml[<id>].list_keys[<path>]>
// @returns dList
// @description
// Returns a dList of all the keys at the path.
// -->
if (attribute.startsWith("list_keys")) {
Set<String> keys;
if (path != null && path.length() > 0) {
YamlConfiguration section = getYaml(id).getConfigurationSection(path);
if (section == null) {
return;
}
keys = section.getKeys(false);
}
else {
keys = getYaml(id).getKeys(false);
}
if (keys == null) {
return;
} else {
ArrayList<String> list = new ArrayList<String>();
list.addAll(keys);
event.setReplaced(new dList(list).getAttribute(attribute.fulfill(1)));
return;
}
}
// <--[tag]
// @attribute <yaml[<id>].to_json>
// @returns dList
// @description
// Converts the YAML container to a JSON array.
// -->
if (attribute.startsWith("to_json")) {
JSONObject jsobj = new JSONObject(getYaml(id).getMap());
event.setReplaced(new Element(jsobj.toString()).getAttribute(attribute.fulfill(1)));
return;
}
}
}