/
WhileCommand.java
231 lines (205 loc) · 10.4 KB
/
WhileCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.exceptions.ScriptEntryCreationException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.BracedCommand;
import net.aufdemrand.denizencore.tags.TagManager;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.utilities.debugging.dB.DebugElement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class WhileCommand extends BracedCommand {
private class WhileData {
public int index;
public String value;
public long LastChecked;
int instaTicks;
}
@Override
public void onEnable() {
setBraced();
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
List<aH.Argument> original_args = aH.interpret(scriptEntry.getOriginalArguments());
List<aH.Argument> parsed_args = aH.interpret(scriptEntry.getArguments());
for (int i = 0; i < parsed_args.size(); i++) {
aH.Argument arg = parsed_args.get(i);
aH.Argument original = original_args.get(i);
if (!scriptEntry.hasObject("stop")
&& arg.matches("stop")) {
scriptEntry.addObject("stop", Element.TRUE);
break;
}
else if (!scriptEntry.hasObject("next")
&& arg.matches("next")) {
scriptEntry.addObject("next", Element.TRUE);
break;
}
else if (!scriptEntry.hasObject("callback")
&& arg.matches("\0CALLBACK")) {
scriptEntry.addObject("callback", Element.TRUE);
break;
}
else if (!scriptEntry.hasObject("value")) {
scriptEntry.addObject("value", new Element(original.raw_value));
break;
}
else {
arg.reportUnhandled();
break;
}
}
if (!scriptEntry.hasObject("value") && !scriptEntry.hasObject("stop") && !scriptEntry.hasObject("next") && !scriptEntry.hasObject("callback"))
throw new InvalidArgumentsException("Must specify a comparison value or 'stop' or 'next'!");
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element stop = scriptEntry.getElement("stop");
Element next = scriptEntry.getElement("next");
Element callback = scriptEntry.getElement("callback");
if (stop != null && stop.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), stop.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
scriptEntry.getResidingQueue().removeEntry(0);
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
}
return;
}
else if (next != null && next.asBoolean()) {
// Report to dB
dB.report(scriptEntry, getName(), next.debug());
boolean hasnext = false;
for (int i = 0; i < scriptEntry.getResidingQueue().getQueueSize(); i++) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(i);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
hasnext = true;
break;
}
}
if (hasnext) {
while (scriptEntry.getResidingQueue().getQueueSize() > 0) {
ScriptEntry entry = scriptEntry.getResidingQueue().getEntry(0);
List<String> args = entry.getOriginalArguments();
if (entry.getCommandName().equalsIgnoreCase("while") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Cannot stop while: not in one!");
}
return;
}
else if (callback != null && callback.asBoolean()) {
if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("while") ||
scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().size() == 0 ||
scriptEntry.getBracedSet().get("WHILE").get(scriptEntry.getBracedSet().get("WHILE").size() - 1) != scriptEntry)) {
WhileData data = (WhileData)scriptEntry.getOwner().getData();
data.index++;
if (System.currentTimeMillis() - data.LastChecked < 50) {
data.instaTicks++;
int max = Settings.whileMaxLoops();
if (data.instaTicks > max && max != 0)
return;
}
else {
data.instaTicks = 0;
}
data.LastChecked = System.currentTimeMillis();
if (TagManager.tag(data.value, new BukkitTagContext(scriptEntry, false)).equalsIgnoreCase("true")) {
dB.echoDebug(scriptEntry, DebugElement.Header, "While loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("loop_index", String.valueOf(data.index));
ArrayList<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get("WHILE");
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" },
(scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer(): null));
callbackEntry.copyFrom(scriptEntry);
}
catch (ScriptEntryCreationException e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
callbackEntry.setOwner(scriptEntry.getOwner());
bracedCommands.add(callbackEntry);
for (int i = 0; i < bracedCommands.size(); i++) {
bracedCommands.get(i).setInstant(true);
bracedCommands.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
}
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "While CALLBACK invalid: not a real callback!");
}
}
else {
// Get objects
Element value = scriptEntry.getElement("value");
ArrayList<ScriptEntry> bracedCommandsList =
((LinkedHashMap<String, ArrayList<ScriptEntry>>) scriptEntry.getObject("braces")).get("WHILE");
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
dB.echoError(scriptEntry.getResidingQueue(), "Empty braces!");
return;
}
// Report to dB
dB.report(scriptEntry, getName(), value.debug());
if (!TagManager.tag(value.asString(), new BukkitTagContext(scriptEntry, false)).equalsIgnoreCase("true")) {
return;
}
WhileData datum = new WhileData();
datum.index = 1;
datum.value = value.asString();
datum.LastChecked = System.currentTimeMillis();
datum.instaTicks = 1;
scriptEntry.setData(datum);
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("WHILE", new String[] { "\0CALLBACK" },
(scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer(): null));
callbackEntry.copyFrom(scriptEntry);
}
catch (ScriptEntryCreationException e) {
dB.echoError(scriptEntry.getResidingQueue(), e);
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("loop_index", "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);
bracedCommandsList.get(i).addObject("reqId", scriptEntry.getObject("reqId"));
}
scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
}
}
}