/
RunCommand.java
261 lines (233 loc) · 11.2 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package com.denizenscript.denizencore.scripts.commands.queue;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
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.scripts.queues.core.InstantQueue;
import com.denizenscript.denizencore.scripts.queues.core.TimedQueue;
import java.util.List;
public class RunCommand extends AbstractCommand implements Holdable {
// <--[command]
// @Name Run
// @Syntax run (locally) [<script>] (path:<name>) (def:<element>|...) (id:<name>) (instantly) (speed:<value>) (delay:<value>)
// @Required 1
// @Short Runs a script in a new ScriptQueue.
// @Video /denizen/vids/Run%20And%20Inject
// @Group queue
//
// @Description
// Runs a new script queue, either in the local script or in a different task script.
//
// You can set the queue speed using the speed argument
// this makes the queue run each script command with a delay.
// Specifying the "instantly" argument will run the queue instantly
// (speed at 0 ticks; queue running in total of 1 tick, just like an event script)
// If no speed or "instantly" argument are applied,
// it assumes the default script speed that are configured.
//
// Specifying definitions as argument
// allows the transfer of definitions to the new queue.
// Definitions are not carried over if not specified.
// (See <@link command define>)
//
// Specifying a player argument will run the queue with a player attached
// to that queue. The same can be done to attach an npc.
// Player and npc are not carried over to the new queue if not specified.
//
// @Tags
// <entry[saveName].created_queue> returns the queue that was started by the run command.
//
// @Usage
// Use to run a new queue instant
// - run MyNewTask instantly
//
// @Usage
// Use to run a new queue instant
// - run MyNewTask instantly def:4|20|true
//
// @Usage
// Use to run a new queue with an attached player and npc with a definition
// - run MyNewTask def:friends player:p@bob npc:<player.selected_npc>
//
// @Usage
// Use to run a new queue instant with the same attached player
// - run MyNewTask instantly player:<player>
//
// @Usage
// Use to run a new queue from a local script
// - run locally MyNewTask
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : ArgumentHelper.interpretArguments(scriptEntry.aHArgs)) {
if (arg.matchesPrefix("i", "id")) {
scriptEntry.addObject("id", arg.asElement());
}
else if (arg.matchesPrefix("d", "def", "define", "c", "context")) {
scriptEntry.addObject("definitions", arg.asElement());
}
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("script")
&& arg.matchesArgumentType(ScriptTag.class)
&& !arg.matchesPrefix("p", "path")) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (!scriptEntry.hasObject("speed") && arg.matchesPrefix("speed")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("speed", arg.asType(DurationTag.class));
}
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.");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(),
(scriptEntry.hasObject("script") ? scriptEntry.getdObject("script").debug() : scriptEntry.getScript().debug())
+ (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : "")
+ (scriptEntry.hasObject("path") ? scriptEntry.getElement("path").debug() : "")
+ (scriptEntry.hasObject("local") ? scriptEntry.getElement("local").debug() : "")
+ (scriptEntry.hasObject("delay") ? scriptEntry.getdObject("delay").debug() : "")
+ (scriptEntry.hasObject("id") ? scriptEntry.getdObject("id").debug() : "")
+ (scriptEntry.hasObject("definitions") ? scriptEntry.getdObject("definitions").debug() : "")
+ (scriptEntry.hasObject("speed") ? scriptEntry.getdObject("speed").debug() : ""));
}
// Get the script
ScriptTag script = scriptEntry.getdObject("script");
// Get the entries
List<ScriptEntry> entries;
// If it's local
if (scriptEntry.hasObject("local")) {
entries = scriptEntry.getScript().getContainer().getEntries(scriptEntry.entryData.clone(),
scriptEntry.getElement("path").asString());
script = scriptEntry.getScript();
}
// If it has a path
else if (scriptEntry.hasObject("path") && scriptEntry.getObject("path") != null) {
entries = script.getContainer().getEntries(scriptEntry.entryData.clone(),
scriptEntry.getElement("path").asString());
}
// Else, assume standard path
else {
entries = script.getContainer().getBaseEntries(scriptEntry.entryData.clone());
}
if (entries == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Script run failed (invalid path or script name)!");
return;
}
// Get the 'id' if specified
String id = (scriptEntry.hasObject("id") ?
"FORCE:" + (scriptEntry.getElement("id")).asString() : script.getContainer().getName());
// Build the queue
ScriptQueue queue;
if (scriptEntry.hasObject("instant")) {
queue = new InstantQueue(id).addEntries(entries);
}
else {
if (scriptEntry.hasObject("speed")) {
DurationTag speed = scriptEntry.getdObject("speed");
queue = ((TimedQueue) new TimedQueue(id).addEntries(entries)).setSpeed(speed.getTicks());
}
else {
// Check speed of the script if a TimedQueue -- if identified, use the speed from the script.
if (script != null && script.getContainer().contains("SPEED")) {
long ticks = DurationTag.valueOf(script.getContainer().getString("SPEED", "0")).getTicks();
if (ticks > 0) {
queue = ((TimedQueue) new TimedQueue(id).addEntries(entries)).setSpeed(ticks);
}
else {
queue = new InstantQueue(id).addEntries(entries);
}
}
else {
queue = new TimedQueue(id).addEntries(entries);
}
}
}
// Set any delay
if (scriptEntry.hasObject("delay")) {
queue.delayUntil(DenizenCore.serverTimeMillis + ((DurationTag) scriptEntry.getObject("delay")).getMillis());
}
// Set any definitions
if (scriptEntry.hasObject("definitions")) {
int x = 1;
ElementTag raw_defintions = scriptEntry.getElement("definitions");
ListTag definitions = ListTag.valueOf(raw_defintions.asString());
String[] definition_names = null;
try {
if (script != null && script.getContainer() != null) {
String str = script.getContainer().getString("definitions");
if (str != null) {
definition_names = str.split("\\|");
}
}
}
catch (Exception e) {
// TODO: less lazy handling
}
for (String definition : definitions) {
String name = definition_names != null && definition_names.length >= x ?
definition_names[x - 1].trim() : String.valueOf(x);
queue.addDefinition(name, definition);
Debug.echoDebug(scriptEntry, "Adding definition '" + name + "' as " + definition);
x++;
}
queue.addDefinition("raw_context", raw_defintions.asString());
}
// Setup a callback if the queue is being waited on
if (scriptEntry.shouldWaitFor()) {
// Record the ScriptEntry
final ScriptEntry se = scriptEntry;
queue.callBack(new Runnable() {
@Override
public void run() {
se.setFinished(true);
}
});
}
// Save the queue for script referencing
scriptEntry.addObject("created_queue", queue);
// OK, GO!
queue.start();
}
}