/
RepeatCommand.java
86 lines (69 loc) · 3.37 KB
/
RepeatCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.Element;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dObject;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.BracedCommand;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.utilities.debugging.dB;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
public class RepeatCommand extends BracedCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("qty")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
scriptEntry.addObject("qty", arg.asElement());
// Don't report unhandled argument since getBracedCommands will handle
// the remainder of the commands.
}
if (!scriptEntry.hasObject("qty"))
throw new InvalidArgumentsException("Must specify a quantity!");
scriptEntry.addObject("braces", getBracedCommands(scriptEntry, 1));
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Get objects
Element qty = scriptEntry.getElement("qty");
ArrayList<ScriptEntry> bracedCommands = ((LinkedHashMap<String, ArrayList<ScriptEntry>>) scriptEntry.getObject("braces")).get("REPEAT");
if (bracedCommands == null || bracedCommands.isEmpty()) {
dB.echoError("Empty braces!");
return;
}
// Report to dB
dB.report(scriptEntry, getName(), qty.debug());
String queueId = UUID.randomUUID().toString();
for (int incr = 0; incr < qty.asInt(); incr++) {
if (scriptEntry.getResidingQueue().getWasCleared())
return;
ArrayList<ScriptEntry> newEntries = new ArrayList<ScriptEntry>();
for (ScriptEntry entry: bracedCommands) {
try {
ScriptEntry toAdd = entry.clone();
toAdd.getObjects().clear();
newEntries.add(toAdd);
}
catch (Throwable e) {
dB.echoError(e);
}
}
ScriptQueue queue = new InstantQueue(queueId);
for (Map.Entry<String, dObject> entry : scriptEntry.getResidingQueue().getAllContext().entrySet()) {
queue.addContext(entry.getKey(), entry.getValue());
}
queue.addDefinition("parent_queue", scriptEntry.getResidingQueue().id);
scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(incr + 1));
queue.addDefinition("value", String.valueOf(incr + 1));
queue.getAllDefinitions().putAll(scriptEntry.getResidingQueue().getAllDefinitions());
queue.addEntries(newEntries);
queue.start();
}
}
}