-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
QueueCommand.java
167 lines (143 loc) · 6.4 KB
/
QueueCommand.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
package com.denizenscript.denizencore.scripts.commands.queue;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.QueueTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.queues.core.Delayable;
public class QueueCommand extends AbstractCommand {
public QueueCommand() {
setName("queue");
setSyntax("queue (<queue>) [clear/stop/pause/resume/delay:<duration>]");
setRequiredArguments(1, 2);
}
// <--[command]
// @Name Queue
// @Syntax queue (<queue>) [clear/stop/pause/resume/delay:<duration>]
// @Required 1
// @Maximum 2
// @Short Modifies the current state of a script queue.
// @Group queue
//
// @Description
// Allows queues to be modified during their run. This can also be used to modify other queues currently running.
//
// Clearing a queue will remove any commands still queued within it, and thus end the queue.
// When trying to clear the current queue, use <@link command stop> instead.
//
// Using the "stop" argument will force the queue to immediately stop running.
// When trying to stop the current queue, use <@link command stop> instead.
//
// Using the "delay:<duration>" argument will cause the queue to wait for a specified duration.
// When trying to delay the current queue, use <@link command wait> instead.
//
// Using the "pause" argument will freeze the queue but keep it listed, waiting for a "resume" instruction.
// It is of course not possible to resume the current queue (as if you're running a 'queue' command, the queue can't be paused).
//
// Generally, the queue is considered a non-ideal way of doing things - that is, there's usually a better/cleaner way to achieve similar results.
// It's most useful within the "/ex" command for quick problem solving
// (eg if a script in testing gets caught in an infinite loop, you can do "/ex queue ID_HERE stop" to fix that).
//
// @Tags
// <queue>
// <QueueTag.id>
// <QueueTag.size>
// <queue.list>
// <queue.stats>
// <queue.exists[queue_id]>
// <ScriptTag.list_queues>
//
// @Usage
// Use to force-stop a given queue.
// - queue <server.flag[OtherQueue]> clear
//
// @Usage
// Use to delay the current queue (use <@link command wait> instead!)
// - queue delay:5t
//
// @Usage
// Use to pause the given queue.
// - queue <server.flag[OtherQueue]> pause
//
// @Usage
// Use to resume the given queue.
// - queue <server.flag[OtherQueue]> resume
// -->
private enum Action {CLEAR, DELAY, PAUSE, RESUME, STOP}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.values())) {
scriptEntry.addObject("action", Action.valueOf(arg.getValue().toUpperCase()));
if (scriptEntry.getObject("action") == Action.DELAY
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("delay", arg.asType(DurationTag.class));
}
}
// No prefix required to specify the queue
else if ((arg.matchesArgumentType(QueueTag.class)
|| arg.matchesPrefix("queue"))
&& !scriptEntry.hasObject("queue")) {
scriptEntry.addObject("queue", arg.asType(QueueTag.class));
}
// ...but we also need to error out this command if the queue was not found.
else {
throw new InvalidArgumentsException("The specified queue could not be found: " + arg.raw_value);
}
}
// If no queues have been added, assume 'residing queue'
scriptEntry.defaultObject("queue", new QueueTag(scriptEntry.getResidingQueue()));
// Check required args
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify an action. Valid: CLEAR, DELAY, PAUSE, RESUME");
}
if (scriptEntry.getObject("action") == Action.DELAY && !scriptEntry.hasObject("delay")) {
throw new InvalidArgumentsException("Must specify a delay.");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
QueueTag queue = (QueueTag) scriptEntry.getObject("queue");
Action action = (Action) scriptEntry.getObject("action");
DurationTag delay = (DurationTag) scriptEntry.getObject("delay");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), queue.debug()
+ ArgumentHelper.debugObj("Action", action.toString())
+ (action == Action.DELAY ? delay.debug() : ""));
}
switch (action) {
case CLEAR:
queue.queue.clear();
return;
case STOP:
queue.queue.clear();
queue.queue.stop();
return;
case PAUSE:
if (queue.queue instanceof Delayable) {
((Delayable) queue.queue).setPaused(true);
}
else {
queue.queue.forceToTimed(new DurationTag(1L)).setPaused(true);
}
return;
case RESUME:
if (queue.queue instanceof Delayable) {
((Delayable) queue.queue).setPaused(false);
}
return;
case DELAY:
if (queue.queue instanceof Delayable) {
((Delayable) queue.queue).delayFor(delay);
}
else {
queue.queue.forceToTimed(delay);
}
return;
}
}
}