-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
RunCommand.java
242 lines (234 loc) · 11.6 KB
/
RunCommand.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
package com.denizenscript.denizencore.scripts.commands.queue;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.*;
import com.denizenscript.denizencore.scripts.ScriptRegistry;
import com.denizenscript.denizencore.scripts.containers.ScriptContainer;
import com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer;
import com.denizenscript.denizencore.utilities.ScriptUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.Map;
import java.util.function.Consumer;
public class RunCommand extends AbstractCommand implements Holdable {
public RunCommand() {
setName("run");
setSyntax("run [<script>/locally] (path:<name>) (def:<element>|.../defmap:<map>/def.<name>:<value>) (id:<name>) (speed:<value>/instantly) (delay:<value>)");
setRequiredArguments(1, -1);
isProcedural = true;
}
// <--[command]
// @Name Run
// @Syntax run [<script>/locally] (path:<name>) (def:<element>|.../defmap:<map>/def.<name>:<value>) (id:<name>) (speed:<value>/instantly) (delay:<value>)
// @Required 1
// @Maximum -1
// @Short Runs a script in a new queue.
// @Guide https://guide.denizenscript.com/guides/basics/run-options.html
// @Group queue
//
// @Description
// Runs a script in a new queue.
//
// You can specify either a script object to run, or "locally" to use a path within the same script.
//
// Optionally, use the "path:" argument to choose a specific sub-path within a script (works well with the "locally" argument).
//
// Optionally, use the "def:" argument to specify definition values to pass to the script,
// the definitions will be named via the "definitions:" script key on the script being ran,
// or numerically in order if that isn't specified (starting with <[1]>).
//
// Alternately, use "defmap:<map>" to specify definitions to pass as a MapTag, where the keys will be definition names and the values will of course be definition values.
//
// Alternately, use "def.<name>:<value>" to define one or more named definitions individually.
//
// Optionally, use the "speed:" argument to specify the queue command-speed to run the target script at,
// or use the "instantly" argument to use an instant speed (no command delay applied).
// If neither argument is specified, the default queue speed applies (normally instant, refer to the config file).
// Generally, prefer to set the "speed:" script key on the script to be ran, rather than using this argument.
//
// Optionally, use the "delay:" argument to specify a delay time before the script starts running.
//
// Optionally, specify the "id:" argument to choose a custom queue ID to be used.
// If none is specified, a randomly generated one will be used. Generally, don't use this argument.
//
// The run command is ~waitable. Refer to <@link language ~waitable>.
//
// @Tags
// <entry[saveName].created_queue> returns the queue that was started by the run command.
//
// @Usage
// Use to run a task script named 'MyTask'.
// - run MyTask
//
// @Usage
// Use to run a task script named 'MyTask' that isn't normally instant, instantly.
// - run MyTask instantly
//
// @Usage
// Use to run a local subscript named 'alt_path'.
// - run locally path:alt_path
//
// @Usage
// Use to run 'MyTask' and pass 3 definitions to it.
// - run MyTask def:A|Second_Def|Taco
//
// @Usage
// Use to run 'MyTask' and pass 3 named definitions to it.
// - run MyTask def.count:5 def.type:Taco def.smell:Tasty
//
// @Usage
// Use to run 'MyTask' and pass a list as a single definition.
// - run MyTask def:<list_single[<list[a|big|list|here]>]>
// # MyTask can then get the list back by doing:
// - define mylist <[1]>
//
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (ScriptContainer script : ScriptRegistry.scriptContainers.values()) {
if (script instanceof TaskScriptContainer) {
addOne.accept(script.getName());
}
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
MapTag defMap = new MapTag();
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("i", "id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (arg.matchesPrefix("d", "def", "define", "c", "context")) {
scriptEntry.addObject("definitions", arg.asType(ListTag.class));
}
else if (arg.matchesPrefix("defmap")
&& arg.matchesArgumentType(MapTag.class)) {
defMap.map.putAll(arg.asType(MapTag.class).map);
}
else if (arg.matches("instant", "instantly")) {
scriptEntry.addObject("instant", new ElementTag(true));
}
else if (arg.matchesPrefix("delay")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("delay", arg.asType(DurationTag.class));
}
else if (arg.matches("local", "locally")) {
scriptEntry.addObject("local", new ElementTag("true"));
scriptEntry.addObject("script", scriptEntry.getScript());
}
else if (!scriptEntry.hasObject("speed")
&& arg.matchesPrefix("speed")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("speed", arg.asType(DurationTag.class));
}
else if (arg.hasPrefix()
&& arg.getPrefix().getRawValue().startsWith("def.")) {
defMap.putObject(arg.getPrefix().getRawValue().substring("def.".length()), arg.object);
}
else if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(ScriptTag.class)
&& !arg.matchesPrefix("p", "path")) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!arg.hasPrefix() && arg.getRawValue().startsWith("def.") && arg.getRawValue().contains(":")) {
int colon = arg.getRawValue().indexOf(':');
defMap.putObject(arg.getRawValue().substring("def.".length(), colon), new ElementTag(arg.getRawValue().substring(colon + 1)));
}
else if (!scriptEntry.hasObject("path")) {
String path = arg.asElement().asString();
if (!scriptEntry.hasObject("script")) {
int dotIndex = path.indexOf('.');
if (dotIndex > 0) {
ScriptTag script = new ScriptTag(path.substring(0, dotIndex));
if (script.isValid()) {
scriptEntry.addObject("script", script);
path = path.substring(dotIndex + 1);
}
}
}
scriptEntry.addObject("path", new ElementTag(path));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("script") && (!scriptEntry.hasObject("local") || scriptEntry.getScript() == null)) {
throw new InvalidArgumentsException("Must define a SCRIPT to be run.");
}
if (!scriptEntry.hasObject("path") && scriptEntry.hasObject("local")) {
throw new InvalidArgumentsException("Must specify a PATH.");
}
if (!defMap.map.isEmpty()) {
scriptEntry.addObject("def_map", defMap);
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag pathElement = scriptEntry.getElement("path");
ScriptTag script = scriptEntry.getObjectTag("script");
ElementTag local = scriptEntry.getElement("local");
ElementTag instant = scriptEntry.getElement("instant");
ElementTag id = scriptEntry.getElement("id");
DurationTag speed = scriptEntry.getObjectTag("speed");
DurationTag delay = scriptEntry.getObjectTag("delay");
MapTag defMap = scriptEntry.getObjectTag("def_map");
if (local != null && local.asBoolean()) {
script = scriptEntry.getScript();
}
String path = pathElement != null ? pathElement.asString() : null;
if (script == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed (invalid script name)!");
return;
}
if (path != null && (!script.getContainer().contains(path) || !script.getContainer().getContents().isList(path))) {
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed (invalid path)!");
return;
}
if (instant != null && instant.asBoolean()) {
speed = new DurationTag(0);
}
ListTag definitions = scriptEntry.getObjectTag("definitions");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(script.debug())
+ (pathElement != null ? pathElement.debug() : "")
+ (local != null ? local.debug() : "")
+ (instant != null ? instant.debug() : "")
+ (speed != null ? speed.debug() : "")
+ (delay != null ? delay.debug() : "")
+ (id != null ? id.debug() : "")
+ (defMap != null ? defMap.debug() : "")
+ (definitions != null ? definitions.debug() : ""));
}
Consumer<ScriptQueue> configure = (queue) -> {
// Set any delay
if (delay != null) {
queue.delayUntil(DenizenCore.serverTimeMillis + delay.getMillis());
}
// Setup a callback if the queue is being waited on
if (scriptEntry.shouldWaitFor()) {
queue.callBack(() -> scriptEntry.setFinished(true));
}
if (defMap != null) {
for (Map.Entry<StringHolder, ObjectTag> val : defMap.map.entrySet()) {
queue.addDefinition(val.getKey().str, val.getValue());
}
}
// Save the queue for script referencing
scriptEntry.addObject("created_queue", new QueueTag(queue));
// Preserve procedural status
queue.procedural = scriptEntry.getResidingQueue().procedural;
};
String idString = id != null ? "FORCE:" + id.asString() : null;
ScriptQueue result = ScriptUtilities.createAndStartQueue(script.getContainer(), path, scriptEntry.entryData, null, configure, speed, idString, definitions, scriptEntry);
if (result == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed!");
return;
}
}
}