-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommandExecuter.java
197 lines (187 loc) · 9.45 KB
/
CommandExecuter.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
package com.denizenscript.denizencore.scripts.commands;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.tags.TagManager;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandExecuter {
private static final Pattern definition_pattern = Pattern.compile("%(.+?)%");
public CommandExecuter() {
}
public static String parseDefsRaw(ScriptEntry scriptEntry, String arg) {
if (!hasDef(arg)) {
return arg;
}
if (scriptEntry.getResidingQueue() == null) {
return arg;
}
Matcher m;
StringBuffer sb;
m = definition_pattern.matcher(arg);
sb = new StringBuffer();
while (m.find()) {
String def = m.group(1);
boolean dynamic = false;
if (def.startsWith("|")) {
def = def.substring(1, def.length() - 1);
dynamic = true;
}
String definition;
String defval = scriptEntry.getResidingQueue().getDefinition(def);
if (dynamic) {
definition = scriptEntry.getResidingQueue().getDefinition(def);
}
else {
definition = TagManager.escapeOutput(scriptEntry.getResidingQueue().getDefinition(def));
}
if (defval == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Unknown definition %" + m.group(1) + "%.");
Debug.log("(Attempted: " + scriptEntry.toString() + ")");
definition = "null";
}
Debug.echoError(scriptEntry.getResidingQueue(), "Ancient style definitions ('%def%') are deprecated. Please use modern definition syntax: '<[def]>'.");
Debug.echoDebug(scriptEntry, "Filled definition %" + m.group(1) + "% with '" + definition + "'.");
m.appendReplacement(sb, Matcher.quoteReplacement(definition));
}
m.appendTail(sb);
return sb.toString();
}
public static boolean hasDef(String arg) {
return arg.indexOf('%') != -1;
}
public static boolean handleDefs(ScriptEntry scriptEntry, boolean genned) {
if (scriptEntry.internal.hasOldDefs) {
if (!genned) {
scriptEntry.regenerateArgsCur();
genned = true;
}
for (int argId : scriptEntry.internal.processArgs) {
String arg = scriptEntry.args.get(argId);
if (hasDef(arg)) {
String parsed = parseDefsRaw(scriptEntry, arg);
scriptEntry.setArgument(argId, parsed);
Argument aharg = new Argument(parsed);
Argument oldaharg = scriptEntry.aHArgs.get(argId);
aharg.needsFill = oldaharg.needsFill || oldaharg.hasSpecialPrefix;
aharg.hasSpecialPrefix = false;
scriptEntry.aHArgs.set(argId, aharg);
ScriptEntry.InternalArgument argse = scriptEntry.args_cur.get(argId);
argse.value = TagManager.dupChain(TagManager.genChain(parsed, scriptEntry));
argse.prefix = null;
}
}
}
return genned;
}
public static ScriptQueue currentQueue;
public boolean execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
StringBuilder output = new StringBuilder();
output.append(scriptEntry.getCommandName());
if (scriptEntry.getOriginalArguments() == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Original Arguments null for " + scriptEntry.getCommandName());
}
else {
for (String arg : scriptEntry.getOriginalArguments()) {
output.append(" \"").append(arg).append("\"");
}
}
DenizenCore.getImplementation().debugQueueExecute(scriptEntry, scriptEntry.getResidingQueue().debugId, output.toString());
}
AbstractCommand command = scriptEntry.internal.actualCommand;
if (command == null) {
command = DenizenCore.getCommandRegistry().get(scriptEntry.internal.command);
scriptEntry.internal.actualCommand = command;
if (command == null || command.getOptions().REQUIRED_ARGS > scriptEntry.getArguments().size()) {
scriptEntry.broken = true;
}
}
if (scriptEntry.broken) {
Debug.echoDebug(scriptEntry, Debug.DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
if (command == null) {
Debug.echoError(scriptEntry.getResidingQueue(), scriptEntry.getCommandName() + " is an invalid command! Are you sure it loaded?");
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), scriptEntry.toString() + " cannot be executed! Is the number of arguments given correct?");
}
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer);
return false;
}
currentQueue = scriptEntry.getResidingQueue();
String saveName = null;
try {
scriptEntry.generateAHArgs();
boolean genned = false;
if (scriptEntry.internal.actualCommand.shouldPreParse() && scriptEntry.internal.hasInstantTags) {
scriptEntry.regenerateArgsCur();
genned = true;
TagManager.fillArgumentsObjects(scriptEntry.processed_arguments, scriptEntry.args,
scriptEntry.args_cur, scriptEntry.aHArgs, true,
DenizenCore.getImplementation().getTagContextFor(scriptEntry, true), scriptEntry.internal.processArgs);
}
genned = handleDefs(scriptEntry, genned);
for (Argument arg : scriptEntry.internal.preprocArgs) {
if (DenizenCore.getImplementation().handleCustomArgs(scriptEntry, arg, false)) {
// Do nothing
}
else if (arg.matchesOnePrefix("save")) {
saveName = TagManager.tag(arg.getValue(), DenizenCore.getImplementation().getTagContext(scriptEntry));
Debug.echoDebug(scriptEntry, "...remembering this script entry as '" + saveName + "'!");
}
}
if (scriptEntry.internal.actualCommand.shouldPreParse()) {
TagManager.fillArgumentsObjects(scriptEntry.processed_arguments, scriptEntry.args,
genned ? scriptEntry.args_cur : scriptEntry.internal.args_ref, scriptEntry.aHArgs, false,
DenizenCore.getImplementation().getTagContextFor(scriptEntry, false), scriptEntry.internal.processArgs);
// TODO: Fix this weird interpreter efficiency hack (remove string dependence)
ArgumentHelper.specialInterpretTrickStrings = scriptEntry.args;
ArgumentHelper.specialInterpretTrickObjects = scriptEntry.aHArgs;
}
command.parseArgs(scriptEntry);
}
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 (while preparing it)!");
Debug.echoError(scriptEntry.getResidingQueue(), e);
Debug.log("(Attempted: " + scriptEntry.toString() + ")");
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer);
scriptEntry.setFinished(true);
currentQueue = null;
return false;
}
try {
command.execute(scriptEntry);
if (saveName != null) {
scriptEntry.getResidingQueue().holdScriptEntry(saveName, scriptEntry);
}
currentQueue = null;
return true;
}
catch (Exception e) {
Debug.echoError(scriptEntry.getResidingQueue(), "Woah!! An exception has been called with this command (while executing it)!");
Debug.echoError(scriptEntry.getResidingQueue(), e);
Debug.log("(Attempted: " + scriptEntry.toString() + ")");
Debug.echoDebug(scriptEntry, Debug.DebugElement.Footer);
scriptEntry.setFinished(true);
currentQueue = null;
return false;
}
}
}