-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
CommandExecuter.java
211 lines (203 loc) · 10.6 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package net.aufdemrand.denizencore.scripts.commands;
import net.aufdemrand.denizencore.DenizenCore;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizencore.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CommandExecuter {
private static final Pattern definition_pattern = Pattern.compile("%(.+?)%");
public static List<String> parseDefs(List<String> args, ScriptEntry scriptEntry) {
Matcher m;
StringBuffer sb;
List<String> newArgs = new ArrayList<String>(args.size());
for (String arg : args) {
if (arg.indexOf('%') != -1) {
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) {
dB.echoError("Unknown definition %" + m.group(1) + "%.");
definition = "null";
}
dB.echoDebug(scriptEntry, "Filled definition %" + m.group(1) + "% with '" + definition + "'.");
m.appendReplacement(sb, Matcher.quoteReplacement(definition));
}
m.appendTail(sb);
arg = sb.toString();
}
newArgs.add(arg);
}
return newArgs;
}
public CommandExecuter() {
}
/*
* Executes a command defined in scriptEntry
*/
public boolean execute(ScriptEntry scriptEntry) {
if (DenizenCore.getImplementation().shouldDebug(scriptEntry)) {
StringBuilder output = new StringBuilder();
output.append(scriptEntry.getCommandName());
if (scriptEntry.getOriginalArguments() == null) {
dB.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().id, output.toString());
DenizenCore.getImplementation().debugCommandHeader(scriptEntry);
}
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) {
dB.echoDebug(scriptEntry, DebugElement.Header, "Executing command: " + scriptEntry.getCommandName());
if (command == null) {
dB.echoError(scriptEntry.getResidingQueue(), scriptEntry.getCommandName() + " is an invalid dCommand! Are you sure it loaded?");
}
else {
dB.echoError(scriptEntry.getResidingQueue(), scriptEntry.toString() + " cannot be executed! Is the number of arguments given correct?");
}
dB.echoDebug(scriptEntry, DebugElement.Footer);
return false;
}
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);
}
if (scriptEntry.internal.hasOldDefs) {
if (!genned) {
scriptEntry.regenerateArgsCur();
genned = true;
}
for (int argId : scriptEntry.internal.processArgs) {
String arg = scriptEntry.args.get(argId);
if (arg.indexOf('%') != -1) {
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) {
dB.echoError(scriptEntry.getResidingQueue(), "Unknown definition %" + m.group(1) + "%.");
dB.log("(Attempted: " + scriptEntry.toString() + ")");
definition = "null";
}
dB.echoDebug(scriptEntry, "Filled definition %" + m.group(1) + "% with '" + definition + "'.");
m.appendReplacement(sb, Matcher.quoteReplacement(definition));
}
m.appendTail(sb);
scriptEntry.setArgument(argId, sb.toString());
aH.Argument aharg = new aH.Argument(sb.toString());
aH.Argument oldaharg = scriptEntry.aHArgs.get(argId);
aharg.needsFill = oldaharg.needsFill;
aharg.hasSpecialPrefix = oldaharg.hasSpecialPrefix;
scriptEntry.aHArgs.set(argId, aharg);
scriptEntry.args_cur.get(argId).value = TagManager.genChain(sb.toString(), scriptEntry);
}
}
}
for (aH.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));
dB.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)
aH.specialInterpretTrickStrings = scriptEntry.args;
aH.specialInterpretTrickObjects = scriptEntry.aHArgs;
}
command.parseArgs(scriptEntry);
}
catch (InvalidArgumentsException e) {
// Give usage hint if InvalidArgumentsException was called.
dB.echoError(scriptEntry.getResidingQueue(), "Woah! Invalid arguments were specified!");
if (e.getMessage() != null && e.getMessage().length() > 0) {
dB.log("+> MESSAGE follows: " + "'" + e.getMessage() + "'");
}
dB.log("Usage: " + command.getUsageHint());
dB.log("(Attempted: " + scriptEntry.toString() + ")");
dB.echoDebug(scriptEntry, DebugElement.Footer);
scriptEntry.setFinished(true);
return false;
}
catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), "Woah! An exception has been called with this command (while preparing it)!");
dB.echoError(scriptEntry.getResidingQueue(), e);
dB.log("(Attempted: " + scriptEntry.toString() + ")");
dB.echoDebug(scriptEntry, DebugElement.Footer);
scriptEntry.setFinished(true);
return false;
}
try {
command.execute(scriptEntry);
if (saveName != null) {
scriptEntry.getResidingQueue().holdScriptEntry(saveName, scriptEntry);
}
return true;
}
catch (Exception e) {
dB.echoError(scriptEntry.getResidingQueue(), "Woah!! An exception has been called with this command (while executing it)!");
dB.echoError(scriptEntry.getResidingQueue(), e);
dB.log("(Attempted: " + scriptEntry.toString() + ")");
dB.echoDebug(scriptEntry, DebugElement.Footer);
scriptEntry.setFinished(true);
return false;
}
}
}