/
CommandExecutor.java
148 lines (141 loc) · 7.22 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
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
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.CoreUtilities;
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> ");
if (scriptEntry.internal.waitfor) {
output.append("~");
}
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 (CoreUtilities.contains(arg, ' ')) {
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.
// -->
// <--[language]
// @name The Global If Argument
// @group Script Command System
// @description
// The "if:<boolean>" argument is a special meta-argument that is available for all commands, but is more useful for some than others.
// It is written like:
// <code>
// - stop if:<player.has_flag[forbidden]>
// # Equivalent to
// - if <player.has_flag[forbidden]>:
// - stop
// </code>
//
// When the if argument is used, the command will only run if the value of the argument is 'true'.
//
// The most useful place to have this is a 'stop' command, to quickly stop a script if a condition is true (a player has a flag, lacks a permission, is outside a region, or whatever else).
//
// If you need more complex matching, especially using '&&', '||', '==', etc. you should probably just do an 'if' command rather than using the argument.
// Though if you really want to, you can use tags here like <@link tag elementtag.is.to> or <@link tag elementtag.and> or <@link tag elementtag.or>.
// -->
public boolean execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
debugSingleExecution(scriptEntry);
}
TagManager.recentTagError = false;
AbstractCommand command = scriptEntry.internal.actualCommand;
currentQueue = scriptEntry.getResidingQueue();
if (currentQueue.procedural && !command.isProcedural) {
Debug.echoError("Command " + command.name + " is not accepted within a procedure. Procedures may not produce a change in the world, they may only process logic.");
return false;
}
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("if")) {
boolean shouldRun = CoreUtilities.equalsIgnoreCase(TagManager.tag(arg.getValue(), context), "true");
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, shouldRun ? "'if:' arg passed, command will run." : "'if:' arg returned false, command won't run.");
}
if (!shouldRun) {
currentQueue = null;
return true;
}
}
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.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;
}
}
}