/
QueueTag.java
353 lines (308 loc) · 11.5 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.denizenscript.denizencore.objects.core;
import com.denizenscript.denizencore.flags.AbstractFlagTracker;
import com.denizenscript.denizencore.flags.FlaggableObject;
import com.denizenscript.denizencore.flags.MapTagBasedFlagTracker;
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.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.Deprecations;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import java.util.Collection;
public class QueueTag implements ObjectTag, Adjustable, FlaggableObject {
// <--[ObjectType]
// @name QueueTag
// @prefix q
// @base ElementTag
// @implements FlaggableObject
// @format
// The identity format for queues is simply the queue ID.
//
// @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.
//
// This object type is flaggable.
// Flags on this object type will be reinterpreted as definitions.
// Flags on queues should just not be used. Use definitions directly.
//
// -->
@Deprecated
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();
}
@Override
public String debuggable() {
return "q@" + queue.debugId;
}
@Override
public boolean isTruthy() {
return !queue.isStopped;
}
public class QueueFakeFlagTracker extends MapTagBasedFlagTracker {
@Override
public MapTag getRootMap(String key) {
return (MapTag) getQueue().getDefinitionObject(key);
}
@Override
public void setRootMap(String key, MapTag map) {
getQueue().addDefinition(key, map);
}
@Override
public Collection<String> listAllFlags() {
return getQueue().getAllDefinitions().keys();
}
}
@Override
public AbstractFlagTracker getFlagTracker() {
return new QueueFakeFlagTracker();
}
@Override
public void reapplyTracker(AbstractFlagTracker tracker) {
// Nothing to do.
}
public static void registerTags() {
AbstractFlagTracker.registerFlagHandlers(tagProcessor);
// <--[tag]
// @attribute <QueueTag.id>
// @returns ElementTag
// @description
// Returns the id of the queue.
// -->
tagProcessor.registerTag(ElementTag.class, "id", (attribute, object) -> {
return new ElementTag(object.getQueue().id);
});
// <--[tag]
// @attribute <QueueTag.size>
// @returns ElementTag(Number)
// @description
// Returns the number of script entries in the queue.
// -->
tagProcessor.registerTag(ElementTag.class, "size", (attribute, object) -> {
return new ElementTag(object.getQueue().script_entries.size());
});
// <--[tag]
// @attribute <QueueTag.started_time>
// @returns TimeTag
// @description
// Returns the time this queue started as a duration.
// -->
tagProcessor.registerTag(TimeTag.class, "started_time", (attribute, object) -> {
return new TimeTag(object.getQueue().startTimeMilli);
});
tagProcessor.registerTag(DurationTag.class, "start_time", (attribute, object) -> {
Deprecations.timeTagRewrite.warn(attribute.context);
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.
// -->
tagProcessor.registerTag(DurationTag.class, "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'.
// -->
tagProcessor.registerTag(ElementTag.class, "state", (attribute, object) -> {
String state;
if ((object.getQueue() instanceof TimedQueue) && ((TimedQueue) 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.
// -->
tagProcessor.registerTag(ScriptTag.class, "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.
// -->
tagProcessor.registerTag(ListTag.class, "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.
// -->
tagProcessor.registerTag(ListTag.class, "definitions", (attribute, object) -> {
return object.getQueue().getAllDefinitions().keys();
});
// <--[tag]
// @attribute <QueueTag.definition_map>
// @returns MapTag
// @description
// Returns a map of all definitions on the queue.
// -->
tagProcessor.registerTag(MapTag.class, "definition_map", (attribute, object) -> {
return object.getQueue().getAllDefinitions().duplicate();
});
// <--[tag]
// @attribute <QueueTag.definition[<definition>]>
// @returns ObjectTag
// @description
// Returns the value of the specified definition.
// Returns null if the queue lacks the definition.
// -->
tagProcessor.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.
// -->
tagProcessor.registerTag(ListTag.class, "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'.
// This is largely considered historical - most queues now default to instant.
// -->
tagProcessor.registerTag(DurationTag.class, "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<>();
@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);
}
}