/
QueueTag.java
310 lines (270 loc) · 9.91 KB
/
QueueTag.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.queues.ScriptQueue;
import com.denizenscript.denizencore.scripts.queues.core.Delayable;
import com.denizenscript.denizencore.scripts.queues.core.TimedQueue;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.ObjectTagProcessor;
import com.denizenscript.denizencore.tags.TagContext;
import com.denizenscript.denizencore.tags.TagRunnable;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.text.StringHolder;
import java.util.Map;
public class QueueTag implements ObjectTag, Adjustable {
// <--[language]
// @name QueueTag Objects
// @group Object System
// @description
// A QueueTag is a single currently running set of script commands.
// This is not to be confused with a script path, which is a single set of script commands that can be run.
// There can be one, multiple, or zero queues running at any time for any given path.
//
// These use the object notation "q@".
// The identity format for queues is simply the queue ID.
//
// -->
public static QueueTag valueOf(String string) {
return valueOf(string, null);
}
@Fetchable("q")
public static QueueTag valueOf(String string, TagContext context) {
if (string == null) {
return null;
}
if (string.startsWith("q@") && string.length() > 2) {
string = string.substring(2);
}
if (ScriptQueue.queueExists(string)) {
return new QueueTag(ScriptQueue.getExistingQueue(string));
}
return null;
}
public static boolean matches(String string) {
// Starts with q@? Assume match.
if (CoreUtilities.toLowerCase(string).startsWith("q@")) {
return true;
}
else {
return false;
}
}
public ScriptQueue queue;
public QueueTag(ScriptQueue queue) {
this.queue = queue;
}
String prefix = "Queue";
@Override
public String getPrefix() {
return prefix;
}
@Override
public QueueTag setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public boolean isUnique() {
return true;
}
@Override
public String getObjectType() {
return "queue";
}
@Override
public String identify() {
return "q@" + queue.id;
}
@Override
public String identifySimple() {
return identify();
}
@Override
public String toString() {
return identify();
}
public static void registerTags() {
// <--[tag]
// @attribute <QueueTag.id>
// @returns ElementTag
// @description
// Returns the id of the queue.
// -->
registerTag("id", (attribute, object) -> {
return new ElementTag(object.getQueue().id);
});
// <--[tag]
// @attribute <QueueTag.size>
// @returns ElementTag
// @description
// Returns the number of script entries in the queue.
// -->
registerTag("size", (attribute, object) -> {
return new ElementTag(object.getQueue().script_entries.size());
});
// <--[tag]
// @attribute <QueueTag.start_time>
// @returns DurationTag
// @description
// Returns the time this queue started as a duration.
// -->
registerTag("start_time", (attribute, object) -> {
return new DurationTag(object.getQueue().startTimeMilli / 50);
});
// <--[tag]
// @attribute <QueueTag.time_ran>
// @returns DurationTag
// @description
// Returns the time this queue has ran for (the length of time between now and when the queue started) as a duration.
// -->
registerTag("time_ran", (attribute, object) -> {
long timeNano = System.nanoTime() - object.getQueue().startTime;
return new DurationTag(timeNano / (1000000 * 1000.0));
});
// <--[tag]
// @attribute <QueueTag.state>
// @returns ElementTag
// @description
// Returns 'stopping', 'running', 'paused', or 'unknown'.
// -->
registerTag("state", (attribute, object) -> {
String state;
if ((object.getQueue() instanceof Delayable) && ((Delayable) object.getQueue()).isPaused()) {
state = "paused";
}
else if (object.getQueue().is_started) {
state = "running";
}
else if (object.getQueue().is_stopping) {
state = "stopping";
}
else {
state = "unknown";
}
return new ElementTag(state);
});
// <--[tag]
// @attribute <QueueTag.script>
// @returns ScriptTag
// @description
// Returns the script that started this queue.
// -->
registerTag("script", (attribute, object) -> {
if (object.getQueue().script == null) {
return null;
}
return object.getQueue().script;
});
// <--[tag]
// @attribute <QueueTag.commands>
// @returns ListTag
// @description
// Returns a list of commands waiting in the queue.
// -->
registerTag("commands", (attribute, object) -> {
ListTag commands = new ListTag();
for (ScriptEntry entry : object.getQueue().script_entries) {
StringBuilder sb = new StringBuilder();
sb.append(entry.getCommandName()).append(" ");
for (String arg : entry.getOriginalArguments()) {
sb.append(arg).append(" ");
}
commands.add(sb.substring(0, sb.length() - 1));
}
return commands;
});
// <--[tag]
// @attribute <QueueTag.definitions>
// @returns ListTag
// @description
// Returns the names of all definitions that were added to the current queue.
// -->
registerTag("definitions", (attribute, object) -> {
return new ListTag(object.getQueue().getAllDefinitions().keySet());
});
// <--[tag]
// @attribute <QueueTag.definition_map>
// @returns MapTag
// @description
// Returns a map of all definitions on the queue.
// -->
registerTag("definition_map", (attribute, object) -> {
MapTag map = new MapTag();
for (Map.Entry<String, ObjectTag> entry : object.getQueue().getAllDefinitions().entrySet()) {
map.map.put(new StringHolder(entry.getKey()), entry.getValue());
}
return map;
});
// <--[tag]
// @attribute <QueueTag.definition[<definition>]>
// @returns ObjectTag
// @description
// Returns the value of the specified definition.
// Returns null if the queue lacks the definition.
// -->
registerTag("definition", (attribute, object) -> {
if (!attribute.hasContext(1)) {
Debug.echoError("The tag QueueTag.definition[...] must have a value.");
return null;
}
return object.getQueue().getDefinitionObject(attribute.getContext(1));
});
// <--[tag]
// @attribute <QueueTag.determination>
// @returns ListTag
// @description
// Returns the values that have been determined via <@link command Determine>
// for this queue, or null if there is none.
// -->
registerTag("determination", (attribute, object) -> {
if (object.getQueue().determinations == null) {
return null;
}
else {
return object.getQueue().determinations;
}
});
// <--[tag]
// @attribute <QueueTag.speed>
// @returns DurationTag
// @description
// Returns the speed of the queue as a Duration. A return of '0' implies it is 'instant'.
// -->
registerTag("speed", (attribute, object) -> {
if (!(object.getQueue() instanceof TimedQueue)) {
if (!attribute.hasAlternative()) {
Debug.echoError("The tag QueueTag.speed is only valid for Timed queues.");
}
return null;
}
return ((TimedQueue) object.getQueue()).getSpeed();
});
}
public static ObjectTagProcessor<QueueTag> tagProcessor = new ObjectTagProcessor<>();
public static void registerTag(String name, TagRunnable.ObjectInterface<QueueTag> runnable, String... variants) {
tagProcessor.registerTag(name, runnable, variants);
}
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
return tagProcessor.getObjectAttribute(this, attribute);
}
public ScriptQueue getQueue() {
ensure();
return queue;
}
public void ensure() {
while (queue.replacementQueue != null) {
queue = queue.replacementQueue;
}
}
@Override
public void applyProperty(Mechanism mechanism) {
Debug.echoError("QueueTags can not hold properties.");
}
@Override
public void adjust(Mechanism mechanism) {
ensure();
CoreUtilities.autoPropertyMechanism(this, mechanism);
}
}