/
IfCommand.java
505 lines (428 loc) · 23.2 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package net.aufdemrand.denizen.scripts.commands.core;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.containers.core.TaskScriptContainer;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
/**
* Core dScript IF command.
*
* @author Jeremy Schroeder
*/
public class IfCommand extends AbstractCommand {
enum Operator { EQUALS, MATCHES, ORMORE, ORLESS, MORE, LESS, CONTAINS, ISEMPTY }
enum Bridge { OR, AND, FIRST }
enum Logic { REGULAR, NEGATIVE }
private class Comparable {
Logic logic = Logic.REGULAR;
Bridge bridge = Bridge.OR;
Object comparable = null;
Operator operator = Operator.EQUALS;
Object comparedto = (boolean) true;
Boolean outcome = null;
@Override
public String toString() {
return (logic != Logic.REGULAR ? "Logic='" + logic.toString() + "', " : "")
+ "Comparable='" + (comparable == null ? "null'" : comparable.getClass().getSimpleName()
+ "(" + ChatColor.AQUA + comparable + ChatColor.WHITE + ")'")
+ ", Operator='" + operator.toString()
+ "', ComparedTo='" + (comparedto == null ? "null'" : comparedto.getClass().getSimpleName()
+ "(" + ChatColor.AQUA + comparedto + ChatColor.WHITE + ")' ")
+ ChatColor.YELLOW + "--> OUTCOME='" + outcome + "'";
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Initialize necessary fields
List<Comparable> comparables = new ArrayList<Comparable>();
String outcomeCommand = null;
ArrayList<String> outcomeArgs = new ArrayList<String>();
String elseCommand = null;
ArrayList<String> elseArgs = new ArrayList<String>();
comparables.add(new Comparable());
int index = 0;
// Iterate through the arguments, build comparables
for (String arg : scriptEntry.getArguments()) {
if (outcomeCommand == null) {
// Set logic (Optional, default is REGULAR)
if (arg.startsWith("!")) {
comparables.get(index).logic = Logic.NEGATIVE;
arg = arg.substring(1);
}
// Replace symbol-operators/bridges with ENUM value for matching
arg = arg.replace("==", "EQUALS").replace(">=", "ORMORE").replace("<=", "ORLESS")
.replace("<", "LESS").replace(">", "MORE")
.replace("||", "OR").replace("&&", "AND");
// Set bridge
if (aH.matchesArg("OR", arg) || aH.matchesArg("AND", arg)) {
index++;
// new Comparable to add to the list
comparables.add(new Comparable());
comparables.get(index).bridge = Bridge.valueOf(arg.toUpperCase());
}
// Set operator (Optional, default is EQUALS)
else if (aH.matchesArg("EQUALS", arg) || aH.matchesArg("MATCHES", arg) || aH.matchesArg("ISEMPTY", arg)
|| aH.matchesArg("ORMORE", arg) || aH.matchesArg("MORE", arg) || aH.matchesArg("LESS", arg)
|| aH.matchesArg("ORLESS", arg) || aH.matchesArg("CONTAINS", arg))
comparables.get(index).operator = Operator.valueOf(arg.toUpperCase());
// Set outcomeCommand
else if (denizen.getCommandRegistry().get(arg) != null)
outcomeCommand = arg;
// Set comparable
else if (comparables.get(index).comparable == null) {
// If using MATCHES operator, keep as string.
comparables.get(index).comparable = findObjectType(arg);
}
// Set compared-to
else {
comparables.get(index).comparedto = matchObjectType(comparables.get(index), arg);
}
// Set outcome command.
} else if (elseCommand == null) {
if (aH.matchesArg("ELSE", arg)) elseCommand = "";
else {
outcomeArgs.add(arg);
}
// Set ELSE command
} else {
// Specify ELSE command
if (elseCommand.equals("")) elseCommand = arg;
// Add elseArgs arguments
else {
elseArgs.add(arg);
}
}
}
// Stash objects required to execute() into the ScriptEntry
scriptEntry.addObject("comparables", comparables);
scriptEntry.addObject("outcome-command", outcomeCommand);
scriptEntry.addObject("outcome-command-args", outcomeArgs.toArray());
scriptEntry.addObject("else-command", elseCommand);
scriptEntry.addObject("else-command-args", elseArgs.toArray());
}
@SuppressWarnings({ "unchecked", "incomplete-switch" })
@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.outcome = false;
//
// Comparable is a STRING
//
if (com.comparable instanceof String) {
String comparable = (String) com.comparable;
if (!(com.comparedto instanceof String) && com.operator != Operator.ISEMPTY) {
} else {
switch(com.operator) {
case ISEMPTY:
// For checking if a FLAG is empty.
if (comparable.equalsIgnoreCase("EMPTY")) com.outcome = true;
break;
case EQUALS:
// For checking straight up if comparable is equal to (ignoring case) comparedto
if (comparable.equalsIgnoreCase((String) com.comparedto)) com.outcome = true;
break;
case CONTAINS:
// For checking if the comparable contains comparedto
if (comparable.toUpperCase().contains(((String) com.comparedto).toUpperCase())) com.outcome = true;
break;
case MATCHES:
// This is a fun one! Can check if the STRING compareable matches a specific argument type, as specified by comparedto
if (((String) com.comparedto).equalsIgnoreCase("location")) {
if (aH.matchesLocation("location:" + comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("pose")) {
com.outcome = true; // TODO: if (aH.matchesPose(comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("double")) {
if (aH.matchesDouble(comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("integer")) {
if (aH.matchesInteger(comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("even integer")) {
if (aH.matchesInteger(comparable) && (aH.getIntegerFrom(comparable) % 2) == 0)
com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("odd integer")) {
if (aH.matchesInteger(comparable) && (aH.getIntegerFrom(comparable) % 2) == 1)
com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("duration")) {
if (aH.matchesDuration("duration:" + comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("boolean")) {
if (comparable.equalsIgnoreCase("true")
|| comparable.equalsIgnoreCase("false")) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("entitytype")) {
if (aH.matchesEntityType("entity:" + comparable)) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("livingentity")) {
if (aH.getLivingEntityFrom(comparable) != null) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("npc")) {
if (CitizensAPI.getNPCRegistry().getNPC(aH.getLivingEntityFrom(comparable)) != null) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("player")) {
if (aH.getPlayerFrom(comparable) != null) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("offlineplayer")) {
if (aH.getOfflinePlayerFrom(comparable) != null) com.outcome = true;
}
else if (((String) com.comparedto).equalsIgnoreCase("item")) {
if (aH.matchesItem("item:" + comparable)) com.outcome = true;
}
break;
// ORMORE/ORLESS/etc. deal with the LENGTH of the the comparable/comparedto strings
case ORMORE:
if (((String) com.comparable).length() >= ((String) com.comparedto).length()) com.outcome = true;
break;
case ORLESS:
if (((String) com.comparable).length() <= ((String) com.comparedto).length()) com.outcome = true;
break;
case MORE:
if (((String) com.comparable).length() > ((String) com.comparedto).length()) com.outcome = true;
break;
case LESS:
if (((String) com.comparable).length() < ((String) com.comparedto).length()) com.outcome = true;
break;
}
}
} else if (com.comparable instanceof List) {
switch(com.operator) {
case CONTAINS:
for (String string : ((List<String>) com.comparable)) {
if (com.comparedto instanceof Integer) {
if (aH.getIntegerFrom(string) == (Integer) com.comparedto) com.outcome = true;
} else if (com.comparedto instanceof Double) {
if (aH.getDoubleFrom(string) == (Double) com.comparedto) com.outcome = true;
} else if (com.comparedto instanceof Boolean) {
if (Boolean.valueOf(string).booleanValue() == ((Boolean) com.comparedto).booleanValue()) com.outcome = true;
} else if (com.comparedto instanceof String) {
if (string.equalsIgnoreCase((String) com.comparedto)) com.outcome = true;
}
}
break;
case ORMORE:
if (((List<String>) com.comparable).size() >= (Integer.parseInt(String.valueOf(com.comparedto)))) com.outcome = true;
break;
case ORLESS:
if (((List<String>) com.comparable).size() <= (Integer.parseInt(String.valueOf(com.comparedto)))) com.outcome = true;
break;
case MORE:
if (((List<String>) com.comparable).size() > (Integer.parseInt(String.valueOf(com.comparedto)))) com.outcome = true;
break;
case LESS:
if (((List<String>) com.comparable).size() < (Integer.parseInt(String.valueOf(com.comparedto)))) com.outcome = true;
break;
}
//
// COMPARABLE IS DOUBLE
//
} else if (com.comparable instanceof Double) {
// Check to make sure comparedto is Double
if (!(com.comparedto instanceof Double)) {
// Not comparing with a Double, outcome = false
} else {
switch(com.operator) {
case EQUALS:
if (((Double) com.comparable).compareTo((Double) com.comparedto) == 0) com.outcome = true;
break;
case ORMORE:
if (((Double) com.comparable).compareTo((Double) com.comparedto) >= 0) com.outcome = true;
break;
case ORLESS:
if (((Double) com.comparable).compareTo((Double) com.comparedto) <= 0) com.outcome = true;
break;
case MORE:
if (((Double) com.comparable).compareTo((Double) com.comparedto) > 0) com.outcome = true;
break;
case LESS:
if (((Double) com.comparable).compareTo((Double) com.comparedto) < 0) com.outcome = true;
break;
}
}
//
// COMPARABLE IS INTEGER
//
} else if (com.comparable instanceof Integer) {
// Check to make sure comparedto is Integer
if (!(com.comparedto instanceof Integer)) {
// Not comparing with an Integer, outcome = false;
} else {
// Comparing integers.. let's do the logic
switch(com.operator) {
case EQUALS:
if (((Integer) com.comparable).compareTo((Integer) com.comparedto) == 0) com.outcome = true;
break;
case ORMORE:
if (((Integer) com.comparable).compareTo((Integer) com.comparedto) >= 0) com.outcome = true;
break;
case ORLESS:
if (((Integer) com.comparable).compareTo((Integer) com.comparedto) <= 0) com.outcome = true;
break;
case MORE:
if (((Integer) com.comparable).compareTo((Integer) com.comparedto) > 0) com.outcome = true;
break;
case LESS:
if (((Integer) com.comparable).compareTo((Integer) com.comparedto) < 0) com.outcome = true;
break;
}
}
//
// COMPARABLE IS BOOLEAN
//
} else if (com.comparable instanceof Boolean) {
// Check to make sure comparedto is Boolean
if (!(com.comparedto instanceof Boolean)) {
// Not comparing with a Boolean, outcome = false;
} else {
// Comparing booleans.. let's do the logic
if ((Boolean) com.comparable.equals((Boolean) com.comparedto))
com.outcome = true;
else
com.outcome = false;
}
}
if (com.logic == Logic.NEGATIVE) com.outcome = !com.outcome;
// Show outcome of Comparable
dB.echoDebug(ChatColor.YELLOW + "Comparable " + counter + ": " + ChatColor.WHITE + com.toString());
counter++;
}
// Compare outcomes
int ormet = 0;
for (Comparable compareable : comparables) {
if (compareable.bridge == Bridge.OR)
if (compareable.outcome) ormet++;
}
int andcount = 0;
int andmet = 0;
for (Comparable compareable : comparables) {
if (compareable.bridge == Bridge.AND) {
if (compareable.outcome) andmet++;
andcount++;
}
}
// Determine outcome -- do, or else?
if (ormet > 0 && andcount == andmet) doCommand(scriptEntry);
else doElse(scriptEntry);
}
// Finds the object type of the String arg
private Object findObjectType(String arg) {
// If a Integer
if (aH.matchesInteger(arg))
return aH.getIntegerFrom(arg);
// If a Double
else if (aH.matchesDouble(arg))
return aH.getDoubleFrom(arg);
// If a Boolean
else if (arg.equalsIgnoreCase("true")) return true;
else if (arg.equalsIgnoreCase("false")) return false;
// If a List<Object>
else if (arg.contains("|")) {
return aH.getListFrom(arg);
}
// If none of the above, must be a String! :D
// 'arg' is already a String, so return it.
else return arg;
}
// Attempts to match a String arg with a Comparable's comparable
private Object matchObjectType(Comparable match, String arg) {
// If MATCHES, change comparable to String
if (match.operator == Operator.MATCHES)
match.comparable = String.valueOf(match.comparable);
Object comparable = match.comparable;
// Comparable is String, return String
if (comparable instanceof String)
return arg;
// Comparable is Double, return Double
else if (comparable instanceof Double) {
if (aH.matchesDouble(arg) || aH.matchesInteger(arg))
return aH.getDoubleFrom(arg);
else {
dB.echoDebug(ChatColor.YELLOW + "WARNING! " + ChatColor.WHITE + "Cannot compare DOUBLE("
+ match.comparable + ") with '" + arg + "'. Outcome for this Comparable will be false.");
return Double.NaN;
}
}
else if (comparable instanceof Integer) {
// If comparedTo is Double, convert comparable to double as well
if (aH.matchesDouble(arg)) {
match.comparable = Double.valueOf(String.valueOf(match.comparable));
return aH.getDoubleFrom(arg);
} else if (aH.matchesInteger(arg)) {
return aH.getIntegerFrom(arg);
} else {
dB.echoDebug(ChatColor.YELLOW + "WARNING! " + ChatColor.WHITE + "Cannot compare INTEGER("
+ match.comparable + ") with '" + arg + "'. Outcome for this Comparable will be false.");
return Double.NaN;
}
}
else if (comparable instanceof Boolean) {
return aH.getBooleanFrom(arg);
}
else if (comparable instanceof List) {
return arg;
}
return arg;
}
// Runs the outcome-command if the evaluation of the statement == true
private void doCommand(ScriptEntry scriptEntry) {
String outcomeCommand = ((String) scriptEntry.getObject("outcome-command")).toUpperCase();
String[] outcomeArgs = Arrays.copyOf((Object[]) scriptEntry.getObject("outcome-command-args"),
((Object[]) scriptEntry.getObject("outcome-command-args")).length, String[].class);
try { ScriptEntry entry = new ScriptEntry(outcomeCommand, outcomeArgs,
scriptEntry.getScript().getContainer())
.setPlayer(scriptEntry.getPlayer())
.setNPC(scriptEntry.getNPC()).setInstant(true)
.addObject("reqId", scriptEntry.getObject("reqId"));
scriptEntry.getResidingQueue().injectEntry(entry, 0);
} 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.");
}
}
// If ELSE command is specified, runs it if the evaluation of the statement == false
private void doElse(ScriptEntry scriptEntry) {
String elseCommand = null;
if (scriptEntry.getObject("else-command") != null)
elseCommand = ((String) scriptEntry.getObject("else-command")).toUpperCase();
String[] elseArgs = null;
if (scriptEntry.getObject("else-command-args") != null)
elseArgs = Arrays.copyOf((Object[]) scriptEntry.getObject("else-command-args"),
((Object[]) scriptEntry.getObject("else-command-args")).length, String[].class);
if (elseCommand == null) return;
try { ScriptEntry entry = new ScriptEntry(elseCommand, elseArgs,
scriptEntry.getScript().getContainer())
.setPlayer(scriptEntry.getPlayer())
.setNPC(scriptEntry.getNPC()).setInstant(true)
.addObject("reqId", scriptEntry.getObject("reqId"));
scriptEntry.getResidingQueue().injectEntry(entry, 0);
} catch (ScriptEntryCreationException e) {
dB.echoError("There has been a problem running the ELSE Command. Check syntax.");
if (dB.showStackTraces) {
dB.echoDebug("STACKTRACE follows:");
e.printStackTrace();
}
else dB.echoDebug("Use '/denizen debug -s' for the nitty-gritty.");
}
}
}