-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommandExecutor.java
108 lines (102 loc) · 5.14 KB
/
CommandExecutor.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
package com.denizenscript.denizencore.scripts.commands;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagManager;
public class CommandExecutor {
public static ScriptQueue currentQueue;
public static void debugSingleExecution(ScriptEntry scriptEntry) {
StringBuilder output = new StringBuilder();
output.append("<G>(line ").append(scriptEntry.internal.lineNumber).append(")<W> ");
output.append(scriptEntry.getCommandName());
if (scriptEntry.getOriginalArguments() == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Original Arguments null for " + scriptEntry.getCommandName());
}
else {
for (String arg : scriptEntry.getOriginalArguments()) {
if (arg.contains(" ")) {
output.append(" \"").append(arg).append("\"");
}
else {
output.append(" ").append(arg);
}
}
}
DenizenCore.getImplementation().debugQueueExecute(scriptEntry, scriptEntry.getResidingQueue().debugId, output.toString());
}
// <--[language]
// @name The Save Argument
// @group Script Command System
// @description
// The "save:<name>" argument is a special meta-argument that is available for all commands, but is only useful for some.
// It is written like:
// - run MyScript save:mysave
//
// When the save argument is used, the results of the command will be saved on the queue, for later usage by the "entry" tag.
//
// The useful entry keys available for any command are listed in the "Tags" documentation section for any command.
// For example, the "run" command lists "<entry[saveName].created_queue>".
// The "saveName" part should be replaced with whatever name you gave to the "save" argument,
// and the "created_queue" part changes between commands.
// Some commands have multiple save entry keys, some have just one, most don't have any.
// -->
public boolean execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
debugSingleExecution(scriptEntry);
}
AbstractCommand command = scriptEntry.internal.actualCommand;
currentQueue = scriptEntry.getResidingQueue();
String saveName = null;
try {
scriptEntry.generateAHArgs();
TagContext context = scriptEntry.getContext();
for (Argument arg : scriptEntry.internal.preprocArgs) {
if (DenizenCore.getImplementation().handleCustomArgs(scriptEntry, arg, false)) {
// Do nothing
}
else if (arg.matchesPrefix("save")) {
saveName = TagManager.tag(arg.getValue(), context);
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, "...remembering this script entry as '" + saveName + "'!");
}
}
}
if (scriptEntry.internal.actualCommand.shouldPreParse()) {
TagManager.fillArgumentsObjects(scriptEntry.processed_arguments, scriptEntry.internal.args_ref, scriptEntry.aHArgs, context, scriptEntry.internal.processArgs);
}
command.parseArgs(scriptEntry);
command.execute(scriptEntry);
if (saveName != null) {
scriptEntry.getResidingQueue().holdScriptEntry(saveName, scriptEntry);
}
currentQueue = null;
return true;
}
catch (InvalidArgumentsException e) {
// Give usage hint if InvalidArgumentsException was called.
Debug.echoError(scriptEntry.getResidingQueue(), "Woah! Invalid arguments were specified!");
if (e.getMessage() != null && e.getMessage().length() > 0) {
Debug.log("+> MESSAGE follows: " + "'" + e.getMessage() + "'");
}
Debug.log("Usage: " + command.getUsageHint());
Debug.log("(Attempted: " + scriptEntry.toString() + ")");
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer);
scriptEntry.setFinished(true);
currentQueue = null;
return false;
}
catch (Exception e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Woah! An exception has been called with this command!");
Debug.echoError(scriptEntry.getResidingQueue(), e);
Debug.log("(Attempted: " + scriptEntry.toString() + ")");
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer);
scriptEntry.setFinished(true);
currentQueue = null;
return false;
}
}
}