/
RunCommand.java
155 lines (125 loc) · 6.38 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.scripts.queues.core.InstantQueue;
import net.aufdemrand.denizen.scripts.queues.core.TimedQueue;
import net.aufdemrand.denizen.utilities.debugging.dB;
import java.util.List;
/**
* Runs a task script in a new ScriptQueue.
* This replaces the now-deprecated runtask command with queue argument.
*
* @author Jeremy Schroeder
*
*/
public class RunCommand extends AbstractCommand {
public String getHelp() {
return "Runs a script in a new ScriptQueue. By using a new and separate" +
"queue, scripts can be delayed, run instantly, and even used to" +
"create loops. If wanting to run a series of commands in the same" +
"queue, use the 'inject' command. \n" +
" \n" +
"Use to start an 'event' that is independent of the current script. \n" +
"- run giant_door_open_script \n" +
"Use the 'as' argument to attach a player or npc. \n" +
"- run start_walking as:n@4 \n" +
"Name the queue with an 'id' argument. \n" +
"- run goal_tracker id:<p.name>'s_goal_tracker \n" +
"Put a script's execution off by specifying a delay. \n" +
"- run gate_closer delay:10s \n" +
"Use run with a delay and 'loop' argument to create a script on a " +
"timer. You can even specify the amount of loops. \n" +
"- run server_announcement loop 1h \n" +
"You can also attach some definitions to the task script being run. \n" +
"- run get_color_of def:i@epic_leather_armor";
}
public String getUsage() {
return "run [script] (path:...) (as:p@player|n@npc) (def:...|...) (id:id_name) (delay:duration) (loop) (q:#)";
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("script")
&& arg.matchesArgumentType(dScript.class))
scriptEntry.addObject("script", arg.asType(dScript.class));
else if (arg.matchesPrefix("i, id"))
scriptEntry.addObject("id", arg.asElement());
else if (arg.matchesPrefix("p, path"))
scriptEntry.addObject("path", arg.asElement());
else if (arg.matches("instant, instantly"))
scriptEntry.addObject("instant", new Element(true));
else if (arg.matchesPrefix("delay")
&& arg.matchesArgumentType(Duration.class))
scriptEntry.addObject("delay", arg.asType(Duration.class));
else if (arg.matches("loop"))
scriptEntry.addObject("loop", new Element(true));
else if (arg.matchesPrefix("q, quantity")
&& arg.matchesPrimitive(aH.PrimitiveType.Integer))
scriptEntry.addObject("quantity", arg.asElement());
else if (arg.matchesPrefix("a, as")
&& arg.matchesArgumentType(dPlayer.class))
scriptEntry.setPlayer((dPlayer) arg.asType(dPlayer.class));
else if (arg.matchesPrefix("a, as")
&& arg.matchesArgumentType(dNPC.class))
scriptEntry.setNPC((dNPC) arg.asType(dNPC.class));
else if (arg.matchesPrefix("d, def, define, c, context"))
scriptEntry.addObject("definitions", arg.asType(dList.class));
}
if (!scriptEntry.hasObject("script"))
throw new InvalidArgumentsException("Must define a SCRIPT to be run.");
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dB.report(getName(), scriptEntry.getdObject("script").debug()
+ (scriptEntry.hasObject("instant") ? scriptEntry.getdObject("instant").debug() : ""));
// definitions
// loop
// quantity
// delay
// instant
// Get the script
dScript script = (dScript) scriptEntry.getObject("script");
// Get the entries
List<ScriptEntry> entries;
// If a path is specified
if (scriptEntry.hasObject("path"))
entries = script.getContainer().getEntries(
scriptEntry.getPlayer(),
scriptEntry.getNPC(),
scriptEntry.getElement("path").asString());
// Else, assume standard path
else entries = script.getContainer().getBaseEntries(
scriptEntry.getPlayer(),
scriptEntry.getNPC());
// Get the 'id' if specified
String id = (scriptEntry.hasObject("id") ?
(scriptEntry.getElement("id")).asString() : ScriptQueue._getNextId());
// Build the queue
ScriptQueue queue;
if (scriptEntry.hasObject("instant"))
queue = InstantQueue.getQueue(id).addEntries(entries);
else queue = TimedQueue.getQueue(id).addEntries(entries);
// Set any delay
if (scriptEntry.hasObject("delay"))
queue.delayUntil(System.currentTimeMillis() + ((Duration) scriptEntry.getObject("delay")).getMillis());
// Set any definitions
if (scriptEntry.hasObject("definitions")) {
int x = 1;
dList definitions = (dList) scriptEntry.getObject("definitions");
String[] definition_names = null;
try { definition_names = script.getContainer().getString("definitions").split("\\|"); }
catch (Exception e) { }
for (String definition : definitions) {
queue.addContext(definition_names != null && definition_names.length >= x ?
definition_names[x - 1].trim() : String.valueOf(x), definition);
x++;
}
}
// OK, GO!
queue.start();
}
}