/
IfCommand.java
311 lines (246 loc) · 12.6 KB
/
IfCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import java.util.*;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.scripts.ScriptBuilder;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.ChatColor;
/**
* Core dScript If command.
*
* @author Jeremy Schroeder, David Cernat
*/
public class IfCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Comparables check the logic
List<Comparable> comparables = new ArrayList<Comparable>();
// Insert new comparable into the list
comparables.add(new Comparable());
// Indicate that comparables are building
boolean buildingComparables = true;
// What to do depending on the logic of the comparables
// is stored in two tree maps
TreeMap<Integer, ArrayList<String>> thenOutcome = new TreeMap<Integer, ArrayList<String>>();
TreeMap<Integer, ArrayList<String>> elseOutcome = new TreeMap<Integer, ArrayList<String>>();
// Keep tracking of whether we're inside the Else part of the statement or not
boolean insideElse = false;
// Keep track of this to avoid Denizen overlooking comparedTo when an operator is used
// with a value that matches the name of a command. (Good find dimensionZ!)
boolean usedOperator = false;
// Track whether we are adding a new command or not
boolean newCommand = false;
// Track whether we are inside nested brackets whose contents
// should be added as arguments to our current If, not as commands
int bracketsEntered = 0;
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (buildingComparables) {
// Set logic to NEGATIVE
if (arg.startsWith("!")) {
comparables.get(comparables.size() - 1).setNegativeLogic();
if (arg.getValue().length() == 1) continue;
if (arg.startsWith("!=")) arg.replaceValue("==");
else arg.replaceValue(arg.getValue().substring(1));
}
// Replace symbol-operators/bridges with ENUM value for matching
arg.replaceValue(arg.getValue().replace("==", "EQUALS").replace(">=", "OR_MORE").replace("<=", "OR_LESS")
.replace("<", "LESS").replace(">", "MORE").replace("||", "OR").replace("&&", "AND"));
// Set bridge
if (arg.matchesEnum(Comparable.Bridge.values())) {
// new Comparable to add to the list
comparables.add(new Comparable());
comparables.get(comparables.size() - 1).bridge =
Comparable.Bridge.valueOf(arg.getValue().toUpperCase());
}
// Set operator (Optional, default is EQUALS)
else if (arg.matchesEnum(Comparable.Operator.values())) {
comparables.get(comparables.size() - 1).operator =
Comparable.Operator.valueOf(arg.getValue().toUpperCase());
usedOperator = true;
}
// Set comparable
else if (comparables.get(comparables.size() - 1).comparable == null) {
// If using MATCHES operator, keep as string.
comparables.get(comparables.size() - 1).setComparable(arg.getValue());
}
else if (!usedOperator && arg.matches("{")) {
buildingComparables = false;
}
// Check if filling comparables are done by checking the command registry for valid commands.
// If using an operator though, skip on to compared-to!
else if (!usedOperator && denizen.getCommandRegistry()
.get(arg.getValue().replace("^", "")) != null) {
buildingComparables = false;
}
// Set compared-to
else {
comparables.get(comparables.size() - 1).setComparedto(arg.getValue());
usedOperator = false;
}
}
if (!buildingComparables) {
// Read "-" as meaning we are moving to a new command, unless we
// are inside nested brackets (i.e. bracketsEntered of at least 2)
// in which case we just treat what follows as arguments
if (arg.matches("-") && bracketsEntered < 2) {
newCommand = true;
}
else if (!insideElse) {
// Move to else commands if we read an "else" and we're not
// currently going through nested arguments
if (arg.matches("else") && bracketsEntered == 0) {
insideElse = true;
}
// If we find a bracket, and we're already inside
// nested brackets, add the bracket to the current
// command's arguments
else if (arg.matches("{")) {
bracketsEntered++;
if (bracketsEntered > 1) {
thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
}
}
else if (arg.matches("}")) {
bracketsEntered--;
if (bracketsEntered > 0) {
thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
}
}
// Add new outcome command if the last argument was a non-nested "-"
// or if there are no outcome commands yet
else if (newCommand == true || thenOutcome.size() == 0) {
thenOutcome.put(thenOutcome.size(), new ArrayList<String>());
thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
newCommand = false;
}
// Add new outcome argument
else {
thenOutcome.get(thenOutcome.lastKey()).add(arg.raw_value);
}
}
else if (insideElse) {
// If we find a bracket, and we're already inside
// nested brackets, add the bracket to the current
// command's arguments
if (arg.matches("{")) {
bracketsEntered++;
if (bracketsEntered > 1) {
elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
}
}
else if (arg.matches("}")) {
bracketsEntered--;
if (bracketsEntered > 0) {
elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
}
}
// Add new else command if the last argument was a non-nested "-"
// or if it was "else" and we have no else commands yet
else if (newCommand == true || elseOutcome.size() == 0) {
newCommand = false;
elseOutcome.put(elseOutcome.size(), new ArrayList<String>());
elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
// Important!
//
// If we find an "if", act like we entered a set of
// brackets, so we treat the if's commands as arguments
// and don't add them to our current else commands
if (arg.matches("if")) {
bracketsEntered++;
}
}
// Add new else argument
else {
elseOutcome.get(elseOutcome.lastKey()).add(arg.raw_value);
}
}
}
}
// Stash objects required to execute() into the ScriptEntry
scriptEntry.addObject("comparables", comparables)
.addObject("then-outcome", thenOutcome)
.addObject("else-outcome", elseOutcome);
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Grab comparables from the ScriptEntry
List<Comparable> comparables = (List<Comparable>) scriptEntry.getObject("comparables");
int counter = 1;
// Evaluate comparables
for (Comparable com : comparables) {
com.determineOutcome();
// Show outcome of Comparable
dB.echoDebug(ChatColor.YELLOW + "Comparable " + counter + ": " + ChatColor.WHITE + com.toString());
counter++;
}
// Compare outcomes
int ormet = 0;
for (Comparable comparable : comparables) {
if (comparable.bridge == Comparable.Bridge.OR)
if (comparable.outcome) ormet++;
}
int andcount = 0;
int andmet = 0;
for (Comparable comparable : comparables) {
if (comparable.bridge == Comparable.Bridge.AND) {
if (comparable.outcome) andmet++;
andcount++;
}
}
boolean do_then;
if (comparables.size() > 1) {
if (ormet > 0)
do_then = true;
else if (andcount == andmet && comparables.get(0).outcome == true)
do_then = true;
else do_then = false;
} else
do_then = comparables.get(0).outcome;
// Determine outcome -- then, or else?
if (do_then) {
// dB.log("then: " + scriptEntry.getObject("then-outcome").toString());
doCommand(scriptEntry, "then-outcome");
}
else {
// dB.log("else: " + scriptEntry.getObject("else-outcome").toString());
doCommand(scriptEntry, "else-outcome");
}
}
private void doCommand(ScriptEntry scriptEntry, String mapName) {
TreeMap<Integer, ArrayList<String>> commandMap =
(TreeMap<Integer, ArrayList<String>>) scriptEntry.getObject(mapName);
if (commandMap == null || commandMap.size() == 0) return;
List<ScriptEntry> entries = new ArrayList<ScriptEntry>();
for (Map.Entry<Integer, ArrayList<String>> pairs : commandMap.entrySet()) {
ArrayList<String> commandArray = pairs.getValue();
String command = commandArray.get(0);
commandArray.remove(0);
String[] arguments = commandArray.toArray(new String[commandArray.size()]);
try {
ScriptEntry entry = new ScriptEntry(command, arguments,
scriptEntry.getScript().getContainer())
.setPlayer(scriptEntry.getPlayer())
.setNPC(scriptEntry.getNPC()).setInstant(true)
.addObject("reqId", scriptEntry.getObject("reqId"));
entries.add(entry);
} catch (ScriptEntryCreationException e) {
dB.echoError("There has been a problem running the Command. Check syntax.");
if (dB.showStackTraces) {
dB.echoDebug("STACKTRACE follows:");
e.printStackTrace();
}
else dB.echoDebug("Use '/denizen debug -s' for the nitty-gritty.");
}
}
// Put tracked objects into new script entries.
for (String tracked_object : scriptEntry.tracked_objects) {
ScriptBuilder.addObjectToEntries(entries, tracked_object, scriptEntry.getObject(tracked_object));
dB.log(tracked_object);
}
scriptEntry.getResidingQueue().injectEntries(entries, 0);
}
}