/
ScriptEntry.java
631 lines (541 loc) · 20.8 KB
/
ScriptEntry.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
package com.denizenscript.denizencore.scripts;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.BracedCommand;
import com.denizenscript.denizencore.scripts.commands.CommandRegistry;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debuggable;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagManager;
import java.util.*;
/**
* ScriptEntry contain information about a single entry from a dScript. It is used
* by the CommandExecutor, among other parts of Denizen.
*/
public class ScriptEntry implements Cloneable, Debuggable {
public static class ScriptEntryInternal {
public String command = null;
public AbstractCommand actualCommand = null;
public List<String> pre_tagged_args = null;
public List<BracedCommand.BracedData> bracedSet = null;
public List<InternalArgument> args_ref = null;
public ScriptTag script = null;
public List<Object> insideList = null;
public boolean instant = false;
public boolean waitfor = false;
public boolean hasTags = false;
public int[] processArgs = null;
public List<Argument> preprocArgs = null;
public Object specialProcessedData = null;
public String originalLine = null;
public int lineNumber;
public boolean brokenArgs = false;
public HashMap<String, Integer> argPrefixMap = null;
}
public static class InternalArgument {
public InternalArgument prefix = null;
public List<TagManager.ParseableTagPiece> value = null;
public Argument aHArg = null;
public InternalArgument duplicate() {
InternalArgument newArg = new InternalArgument();
newArg.prefix = prefix == null ? null : prefix.duplicate();
newArg.value = new ArrayList<>(value);
newArg.aHArg = aHArg == null ? null : aHArg.clone();
return newArg;
}
}
public List<Argument> getProcessedArgs() {
for (Argument arg : aHArgs) {
arg.scriptEntry = this;
if (arg.object instanceof ElementTag && arg.prefix == null) {
arg.fillStr(arg.object.toString());
}
else {
arg.value = arg.object.toString();
arg.lower_value = CoreUtilities.toLowerCase(arg.value);
arg.raw_value = arg.generateRaw();
}
}
return aHArgs;
}
public List<Argument> aHArgs;
public List<String> args;
public List<ObjectTag> processed_arguments = null;
public ScriptEntryData entryData;
private ScriptQueue queue = null;
public ScriptEntryInternal internal;
public TagContext context;
public List<BracedCommand.BracedData> getBracedSet() {
return internal.bracedSet;
}
public TagContext getContext() {
return context;
}
public void updateContext() {
context = DenizenCore.getImplementation().getTagContext(this);
}
public void setBracedSet(List<BracedCommand.BracedData> set) {
internal.bracedSet = set;
}
private Map<String, Object> objects = new HashMap<>(8);
public final static Argument NULL_ARGUMENT = new Argument("null_trick", "null_trick");
public final static InternalArgument NULL_INTERNAL_ARGUMENT = new InternalArgument();
static {
NULL_INTERNAL_ARGUMENT.aHArg = NULL_ARGUMENT;
NULL_INTERNAL_ARGUMENT.value = new ArrayList<>();
}
public void generateAHArgs() {
for (int i : internal.processArgs) {
Argument aHArg = internal.args_ref.get(i).aHArg.clone();
aHArg.scriptEntry = this;
aHArgs.set(i, aHArg);
}
}
@Override
public ScriptEntry clone() {
try {
ScriptEntry se = (ScriptEntry) super.clone();
se.objects = new HashMap<>(8);
se.processed_arguments = processed_arguments == null ? null : new ArrayList<>(processed_arguments);
se.args = new ArrayList<>(args);
se.entryData = entryData.clone();
se.entryData.scriptEntry = se;
se.aHArgs = new ArrayList<>(aHArgs);
se.updateContext();
return se;
}
catch (CloneNotSupportedException ex) {
throw new RuntimeException(ex);
}
}
public List<Object> getInsideList() {
return internal.insideList;
}
/**
* Get a hot, fresh, script entry, ready for execution! Just supply a valid command,
* some arguments, and bonus points for a script container (can be null)!
*
* @param command the name of the command this entry will be handed to
* @param arguments an array of the arguments
* @param script optional ScriptContainer reference
*/
public ScriptEntry(String command, String[] arguments, ScriptContainer script) {
this(command, arguments, script, null);
}
public void crunchInto(InternalArgument argVal, String arg, TagContext refContext) {
argVal.value = TagManager.dupChain(TagManager.genChain(arg, refContext));
boolean isTag = false;
int indStart = arg.indexOf('<');
if (indStart >= 0) {
int indEnd = arg.lastIndexOf('>');
if (indEnd > indStart) {
isTag = true;
internal.hasTags = true;
}
}
argVal.aHArg = new Argument(argVal.prefix == null ? null : argVal.prefix.aHArg.raw_value, arg);
argVal.aHArg.needsFill = isTag;
argVal.aHArg.hasSpecialPrefix = argVal.prefix != null;
}
public ScriptEntry(String command, String[] arguments, ScriptContainer script, List<Object> insides) {
if (command == null) {
throw new RuntimeException("Command name cannot be null!");
}
internal = new ScriptEntryInternal();
entryData = DenizenCore.getImplementation().getEmptyScriptEntryData();
internal.command = command.toUpperCase();
internal.insideList = insides;
internal.argPrefixMap = new HashMap<>();
if (script != null) {
internal.script = script.getAsScriptArg();
}
updateContext();
if (command.length() > 0) {
if (command.charAt(0) == '^') {
internal.instant = true;
internal.command = command.substring(1);
}
else if (command.charAt(0) == '~') {
internal.command = command.substring(1);
if (DenizenCore.getCommandRegistry().get(internal.command) instanceof Holdable) {
internal.waitfor = true;
}
else {
Debug.echoError("The command '" + internal.command + "' cannot be waited for!");
}
}
internal.actualCommand = DenizenCore.getCommandRegistry().get(internal.command);
if (internal.actualCommand != null && internal.actualCommand.forceHold) {
internal.waitfor = true;
}
}
else {
internal.actualCommand = null;
}
boolean hasBraces = false;
if (arguments != null) {
args = new ArrayList<>(arguments.length);
internal.preprocArgs = new ArrayList<>(arguments.length);
int nested_depth = 0;
for (String arg : arguments) {
if (arg.lastIndexOf('%') > arg.indexOf('%')) {
Deprecations.ancientDefs.warn(this);
}
if (arg.lastIndexOf('>') > arg.indexOf('<') && arg.contains("<^")) {
Deprecations.instantTags.warn(this);
}
if (arg.equals("{")) {
if (!hasBraces) {
if (getScript() != null) { // ex command allowed to bypass
Deprecations.oldBraceSyntax.warn(this);
}
hasBraces = true;
}
nested_depth++;
args.add(arg);
continue;
}
if (arg.equals("}")) {
nested_depth--;
args.add(arg);
continue;
}
if (nested_depth > 0) {
args.add(arg);
continue;
}
String parg = arg;
String after = null;
if (parg.endsWith("{")) {
after = "{";
parg = parg.substring(0, parg.length() - 1);
Debug.echoError("Command '" + command + "' in script '" + (script == null ? "(None)" : script.getName()) + "' has typo: brace written without space... like 'arg{' when it should be 'arg {'.");
}
Argument argObj = new Argument(arg);
if (argObj.hasPrefix()) {
if (argObj.matchesPrefix("save") || DenizenCore.getImplementation().needsHandleArgPrefix(argObj.prefix)) {
internal.preprocArgs.add(argObj);
}
else {
args.add(arg);
}
}
else {
args.add(parg);
}
if (after != null) {
args.add(after);
if (after.equals("{")) {
nested_depth++;
args.add(arg);
continue;
}
}
}
internal.pre_tagged_args = new ArrayList<>(args);
nested_depth = 0;
TagContext refContext = DenizenCore.getImplementation().getTagContext(this);
internal.args_ref = new ArrayList<>(args.size());
List<Integer> tempProcessArgs = new ArrayList<>(args.size());
aHArgs = new ArrayList<>(args.size());
for (int i = 0; i < args.size(); i++) {
String arg = args.get(i);
if (arg.equals("{")) {
InternalArgument brace = new InternalArgument();
brace.aHArg = new Argument("", "{");
internal.args_ref.add(brace);
nested_depth++;
continue;
}
if (arg.equals("}")) {
InternalArgument brace = new InternalArgument();
brace.aHArg = new Argument("", "}");
internal.args_ref.add(brace);
nested_depth--;
continue;
}
internal.args_ref.add(NULL_INTERNAL_ARGUMENT);
if (nested_depth > 0) {
continue;
}
InternalArgument argVal = new InternalArgument();
internal.args_ref.set(i, argVal);
int colon = TagManager.findColonNotTagNorSpace(arg);
if (colon > 0) {
argVal.prefix = new InternalArgument();
crunchInto(argVal.prefix, arg.substring(0, colon), refContext);
arg = arg.substring(colon + 1);
if (!argVal.prefix.aHArg.needsFill) {
internal.argPrefixMap.put(argVal.prefix.aHArg.lower_value, i);
}
}
crunchInto(argVal, arg, refContext);
if (argVal.aHArg.needsFill || argVal.aHArg.hasSpecialPrefix) {
tempProcessArgs.add(aHArgs.size());
}
aHArgs.add(argVal.aHArg);
}
internal.processArgs = new int[tempProcessArgs.size()];
for (int i = 0; i < tempProcessArgs.size(); i++) {
internal.processArgs[i] = tempProcessArgs.get(i);
}
objectify();
}
else {
args = new ArrayList<>();
internal.preprocArgs = new ArrayList<>();
internal.pre_tagged_args = new ArrayList<>();
internal.processArgs = new int[0];
internal.args_ref = new ArrayList<>();
processed_arguments = new ArrayList<>();
aHArgs = new ArrayList<>();
}
if (internal.actualCommand != null) {
int argCount = getArguments().size();
if (argCount < internal.actualCommand.minimumArguments || (!hasBraces && argCount > internal.actualCommand.maximumArguments)) {
internal.brokenArgs = true;
internal.actualCommand = CommandRegistry.debugInvalidCommand;
}
if (internal.actualCommand instanceof BracedCommand) {
BracedCommand.getBracedCommands(this);
}
}
else {
internal.actualCommand = CommandRegistry.debugInvalidCommand;
}
}
/**
* Adds a context object to the script entry. Just provide a key and an object.
* Technically any type of object can be stored, however providing ObjectTags
* is preferred.
*
* @param key the name of the object
* @param object the object, preferably a ObjectTag
*/
public ScriptEntry addObject(String key, Object object) {
if (object == null) {
return this;
}
if (object instanceof ObjectTag) {
((ObjectTag) object).setPrefix(key);
}
objects.put(key, object);
return this;
}
/**
* If the scriptEntry lacks the object corresponding to the
* key, set it to the first non-null argument
*
* @param key The key of the object to check
* @return The scriptEntry
*/
public ScriptEntry defaultObject(String key, Object... objects) throws InvalidArgumentsException {
if (!this.objects.containsKey(key)) {
for (Object obj : objects) {
if (obj != null) {
this.addObject(key, obj);
break;
}
}
}
// Check if the object has been filled. If not, throw new Invalid Arguments Exception.
// TODO: Should this be here? Most checks are done separately.
if (!hasObject(key)) {
throw new InvalidArgumentsException("Missing '" + key + "' argument!");
}
else {
return this;
}
}
public List<String> getArguments() {
return args;
}
////////////
// INSTANCE METHODS
//////////
/**
* Gets the original, pre-tagged arguments, as constructed. This is simply a copy of
* the original arguments, immune from any changes that may be made (such as tag filling)
* by the CommandExecutor.
*
* @return unmodified arguments from entry creation
*/
public List<String> getOriginalArguments() {
return internal.pre_tagged_args;
}
public String getCommandName() {
return internal.command;
}
public AbstractCommand getCommand() {
return internal.actualCommand;
}
public ScriptEntry setArguments(List<String> arguments) {
args = arguments;
return this;
}
public void objectify() {
processed_arguments = new ArrayList<>(args.size());
for (String arg : args) {
processed_arguments.add(new ElementTag(arg));
}
}
private ScriptEntry owner = null;
public void setOwner(ScriptEntry owner) {
this.owner = owner;
}
public ScriptEntry getOwner() {
return owner;
}
private Object data;
public Object getData() {
return data;
}
public void setData(Object result) {
this.data = result;
}
public void copyFrom(ScriptEntry entry) {
entryData = entry.entryData.clone();
setSendingQueue(entry.getResidingQueue());
updateContext();
}
//////////////////
// SCRIPTENTRY CONTEXT
//////////////
public Map<String, Object> getObjects() {
return objects;
}
public Object getObject(String key) {
try {
return objects.get(key);
}
catch (Exception ex) {
if (Debug.verbose) {
Debug.echoError(ex);
}
return null;
}
}
public <T extends ObjectTag> T getObjectTag(String key) {
try {
// If an ENUM, return as an Element
Object gotten = objects.get(key);
if (gotten == null) {
return null;
}
if (gotten instanceof Enum) {
return (T) new ElementTag(((Enum) gotten).name());
}
// Otherwise, just return the stored ObjectTag
return (T) gotten;
// If not a ObjectTag, return null
}
catch (Exception ex) {
if (Debug.verbose) {
Debug.echoError(ex);
}
return null;
}
}
public ElementTag getElement(String key) {
try {
Object gotten = objects.get(key);
if (gotten == null) {
return null;
}
return (ElementTag) gotten;
}
catch (Exception ex) {
if (Debug.verbose) {
Debug.echoError(ex);
}
return null;
}
}
public boolean hasObject(String key) {
return objects.containsKey(key);
}
/////////////
// CORE LINKED OBJECTS
///////
public ScriptTag getScript() {
return internal.script;
}
public ScriptEntry setScript(String scriptName) {
internal.script = ScriptTag.valueOf(scriptName, CoreUtilities.basicContext);
return this;
}
public ScriptQueue getResidingQueue() {
return queue;
}
public void setSendingQueue(ScriptQueue scriptQueue) {
queue = scriptQueue;
}
//////////////
// TimedQueue FEATURES
/////////
public boolean forceInstant = false;
public boolean isInstant() {
return internal.instant || forceInstant;
}
public ScriptEntry setInstant(boolean instant) {
forceInstant = instant;
return this;
}
public boolean isFinished = false;
public boolean shouldWaitFor() {
return internal.waitfor && !isFinished;
}
public ScriptEntry setFinished(boolean finished) {
isFinished = finished;
return this;
}
/////////////
// DEBUGGABLE
/////////
public boolean fallbackDebug = true;
public Boolean shouldDebugBool = null;
public boolean dbCallShouldDebug() {
return DenizenCore.getImplementation().shouldDebug(this);
}
@Override
public boolean shouldDebug() {
if (shouldDebugBool != null) {
return shouldDebugBool;
}
if (internal.script == null || internal.script.getContainer() == null) {
shouldDebugBool = fallbackDebug;
return shouldDebugBool;
}
shouldDebugBool = internal.script.getContainer().shouldDebug();
return shouldDebugBool;
}
private static String stringifyArg(String arg) {
arg = arg.replace("\"", "<&dq>");
if (CoreUtilities.contains(arg, ' ')) {
return '"' + arg + '"';
}
else {
return arg;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (String str : getOriginalArguments()) {
sb.append(" ").append(stringifyArg(str));
}
for (Argument arg : internal.preprocArgs) {
sb.append(" ").append(stringifyArg(arg.toString()));
}
return internal.command + sb.toString();
}
}