-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
ForeachCommand.java
248 lines (226 loc) · 11.2 KB
/
ForeachCommand.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
package com.denizenscript.denizencore.scripts.commands.queue;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.BracedCommand;
import java.util.List;
public class ForeachCommand extends BracedCommand {
// <--[command]
// @Name Foreach
// @Syntax foreach [stop/next/<object>|...] (as:<name>) [<commands>]
// @Required 1
// @Short Loops through a ListTag, running a set of commands for each item.
// @Group queue
// @Guide https://guide.denizenscript.com/guides/basics/loops.html
//
// @Description
// Loops through a ListTag of any type. For each item in the ListTag, the specified commands will be ran for
// that list entry. To call the value of the entry while in the loop, you can use <[value]>.
//
// Optionally, specify "as:<name>" to change the definition name to something other than "value".
//
// To end a foreach loop, do - foreach stop
//
// To jump immediately to the next entry in the loop, do - foreach next
//
// @Tags
// <[value]> to get the current item in the loop
// <[loop_index]> to get the current loop iteration number
//
// @Usage
// Use to run commands 'for each entry' in a list of objects/elements.
// - foreach <[some_entity]>|<[some_npc]>|<[player]>:
// - announce "There's something at <[value].location>!"
//
// @Usage
// Use to iterate through entries in any tag that returns a list
// - foreach <server.list_online_players> as:player:
// - narrate "Thanks for coming to our server! Here's a bonus $50.00!" player:<[player]>
// - give <[player]> money qty:50
//
// -->
private class ForeachData {
public int index;
public ListTag list;
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean handled = false;
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!handled
&& arg.matches("stop")) {
scriptEntry.addObject("stop", new ElementTag(true));
handled = true;
}
else if (!handled
&& arg.matches("next")) {
scriptEntry.addObject("next", new ElementTag(true));
handled = true;
}
else if (!handled
&& arg.matches("\0callback")) {
scriptEntry.addObject("callback", new ElementTag(true));
handled = true;
}
else if (!scriptEntry.hasObject("as_name")
&& arg.matchesPrefix("as")) {
scriptEntry.addObject("as_name", arg.asElement());
}
else if (!handled) {
scriptEntry.addObject("list", arg.object instanceof ListTag ? (ListTag) arg.object : ListTag.valueOf(arg.raw_value, scriptEntry.getContext()));
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
handled = true;
}
else if (arg.matches("{")) {
break;
}
else {
arg.reportUnhandled();
}
}
if (!handled) {
throw new InvalidArgumentsException("Must specify a valid list or 'stop' or 'next'!");
}
scriptEntry.defaultObject("as_name", new ElementTag("value"));
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag stop = scriptEntry.getElement("stop");
ElementTag next = scriptEntry.getElement("next");
ElementTag callback = scriptEntry.getElement("callback");
ListTag list = (ListTag) scriptEntry.getObject("list");
ElementTag as_name = scriptEntry.getElement("as_name");
if (stop != null && stop.asBoolean()) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.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("foreach") && 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("foreach") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
scriptEntry.getResidingQueue().removeEntry(0);
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot stop foreach: not in one!");
}
return;
}
else if (next != null && next.asBoolean()) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.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("foreach") && 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("foreach") && args.size() > 0 && args.get(0).equalsIgnoreCase("\0CALLBACK")) {
break;
}
scriptEntry.getResidingQueue().removeEntry(0);
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Cannot stop foreach: not in one!");
}
return;
}
else if (callback != null && callback.asBoolean()) {
if (scriptEntry.getOwner() != null && (scriptEntry.getOwner().getCommandName().equalsIgnoreCase("foreach") ||
scriptEntry.getOwner().getBracedSet() == null || scriptEntry.getOwner().getBracedSet().isEmpty() ||
scriptEntry.getBracedSet().get(0).value.get(scriptEntry.getBracedSet().get(0).value.size() - 1) != scriptEntry)) {
ForeachData data = (ForeachData) scriptEntry.getOwner().getData();
data.index++;
if (data.index <= data.list.size()) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, Debug.DebugElement.Header, "Foreach loop " + data.index);
}
scriptEntry.getResidingQueue().addDefinition("loop_index", String.valueOf(data.index));
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), String.valueOf(data.list.get(data.index - 1)));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = new ScriptEntry("FOREACH", new String[]{"\0CALLBACK", "as:" + as_name.asString()},
(scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
callbackEntry.setOwner(scriptEntry.getOwner());
bracedCommands.add(callbackEntry);
for (int i = 0; i < bracedCommands.size(); i++) {
bracedCommands.get(i).setInstant(true);
}
scriptEntry.getResidingQueue().injectEntries(bracedCommands, 0);
}
else {
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, Debug.DebugElement.Header, "Foreach loop complete");
}
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Foreach CALLBACK invalid: not a real callback!");
}
}
else {
List<BracedData> bdlist = (List<BracedData>) scriptEntry.getObject("braces");
if (bdlist == null || bdlist.isEmpty()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Empty braces (internal)!");
return;
}
List<ScriptEntry> bracedCommandsList = bdlist.get(0).value;
if (bracedCommandsList == null || bracedCommandsList.isEmpty()) {
Debug.echoError(scriptEntry.getResidingQueue(), "Empty braces!");
return;
}
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), list.debug() + as_name.debug());
}
int target = list.size();
if (target <= 0) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.echoDebug(scriptEntry, "Empty list, not looping...");
}
return;
}
ForeachData datum = new ForeachData();
datum.list = list;
datum.index = 1;
scriptEntry.setData(datum);
ScriptEntry callbackEntry = new ScriptEntry("FOREACH", new String[]{"\0CALLBACK", "as:" + as_name.asString()},
(scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), list.get(0));
scriptEntry.getResidingQueue().addDefinition("loop_index", "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);
}
scriptEntry.setInstant(true);
scriptEntry.getResidingQueue().injectEntries(bracedCommandsList, 0);
}
}
}