-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
ScriptQueue.java
337 lines (262 loc) · 9.95 KB
/
ScriptQueue.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
package net.aufdemrand.denizen.scripts;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class ScriptQueue implements Listener {
protected static int totalQueues = 0;
/**
* Returns the number of queues created in the current instance
* as well as the number of currently active queues.
*
* @return stats
*/
public static String _getStats() {
return "Total number of queues created: '"
+ totalQueues
+ "', currently active queues: '"
+ _queues.size() + "'.";
}
/**
* Gets a random UUID for use in creating a 'nameless' queue.
*
* @return String value of a random UUID
*/
public static String _getNextId() {
return UUID.randomUUID().toString();
}
// Contains all currently active queues, keyed by a String id.
public static Map<String, ScriptQueue> _queues = new ConcurrentHashMap<String, ScriptQueue>(8, 0.9f, 1);
/**
* Returns a collection of all active queues.
*
* @return a collection of ScriptQueues
*/
public static Collection<ScriptQueue> _getQueues() {
return _queues.values();
}
/**
* <p>Gets a ScriptQueue instance. If a queue already exists with the
* given id, it will return that instance as opposed to creating a
* new one. IDs are case insensitive. If having an easy-to-recall
* ID is not necessary, use the static method _getNextId() which
* will return a random UUID.</p>
*
* <p>New ScriptQueues will need further information before they
* can start(), including </p>
* @param id unique id of the queue
* @return
*/
public static ScriptQueue _getQueue(String id) {
// Get id if not specified.
if (id == null) id = String.valueOf(_getNextId());
ScriptQueue scriptQueue;
// Does the queue already exist?
if (_queueExists(id))
scriptQueue = _queues.get(id.toUpperCase());
// If not, create a new one.
else {
scriptQueue = new ScriptQueue(id,
Duration.valueOf(Settings.ScriptQueueSpeed()).getTicksAsInt());
}
// Return the queue
return scriptQueue;
}
public static ScriptQueue _getInstantQueue(String id) {
// Get id if not specified.
if (id == null) id = String.valueOf(_getNextId());
ScriptQueue scriptQueue;
// Does the queue already exist?
if (_queueExists(id))
scriptQueue = _queues.get(id.toUpperCase());
// If not, create a new one.
else {
scriptQueue = new ScriptQueue(id, 0);
}
return scriptQueue;
}
public static boolean _queueExists(String id) {
return _queues.containsKey(id.toUpperCase());
}
// Name of the queue -- this identifies the ScriptQueue when using _getQueue
// or a dScript Queue:xxx argument, whereas xxx = id
public String id;
// Keep track of Bukit's Scheduler taskId for the engine, for when it times out.
protected int taskId;
//The speed of the engine, the # of ticks between each revolution.
protected int ticks;
// List of ScriptEntries in the queue
List<ScriptEntry> scriptEntries = new ArrayList<ScriptEntry>();
// If this number is larger than getCurrentTimeMillis, the queues will delay execution
protected long delayTime = 0;
// The delay in ticks
protected long delayTicks = 0;
// ScriptQueues can have a bit of context, keyed by a String Id. All that can be accessed by either getContext()
// or a dScript replaceable tag <context.id>
private ConcurrentHashMap<String, String> context = new ConcurrentHashMap<String, String>(8, 0.9f, 1);
public String getContext(String key) {
return context.get(key.toLowerCase());
}
public boolean hasContext(String key) {
return context.containsKey(key.toLowerCase());
}
public void addContext(String key, String value) {
context.put(key.toLowerCase(), value);
}
public ConcurrentHashMap<String, String> getAllContext() {
return context;
}
protected ScriptEntry lastEntryExecuted = null;
public void setLastEntryExecuted(ScriptEntry entry) {
lastEntryExecuted = entry;
}
public Duration getSpeed() {
return Duration.valueOf(ticks + "t");
}
public ScriptEntry getLastEntryExecuted() {
return lastEntryExecuted;
}
public void clear() {
scriptEntries.clear();
}
protected boolean paused = false;
protected ScriptQueue(String id, int ticks) {
this.id = id.toUpperCase();
_queues.put(id.toUpperCase(), this);
totalQueues++;
this.ticks = ticks;
}
public void setPaused(boolean paused) {
this.paused = paused;
}
public boolean isPaused() {
return paused;
}
public void setSpeed(int ticks) {
this.ticks = ticks;
}
public void delayFor(long delayTicks) {
this.delayTicks = delayTicks;
}
public void delayUntil(long delayTime) {
this.delayTime = delayTime;
}
boolean is_stopping = false;
public void stop() {
if (!is_stopping) {
is_stopping = true;
List<ScriptEntry> entries = lastEntryExecuted.getScript()
.getContainer().getEntries(lastEntryExecuted.getPlayer(), lastEntryExecuted.getNPC(), "on queue completes");
if (!entries.isEmpty()) {
scriptEntries.addAll(entries);
dB.log("Finishing up queue " + id + "...");
}
else {
_queues.remove(id);
dB.echoDebug("Completing queue " + id + "...");
Bukkit.getServer().getScheduler().cancelTask(taskId);
}
} else {
_queues.remove(id);
dB.echoDebug("Completing queue " + id + "...");
Bukkit.getServer().getScheduler().cancelTask(taskId);
}
}
private boolean isStarted = false;
public void start() {
// If already started, no need to restart.
if (isStarted) return;
// Start the queue
dB.echoDebug("Starting queue " + id + ". (Speed=" + ticks + "tpr)");
isStarted = true;
// If not an instant queue, set a bukkit repeating task with the speed
if (ticks > 0)
taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(), new Runnable() {
@Override
public void run() {
// Turn the engine
revolve();
}
}, delayTicks, ticks);
// If the ticks are 0, this is an 'instant queue'
else
{
// If it's delayed, schedule it for later
if (delayTime > System.currentTimeMillis())
{
Bukkit.getScheduler().scheduleSyncDelayedTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override
public void run() {
// revolve
while (isStarted) revolve();
}
}, delayTicks + 1);
}
else while (isStarted) revolve();
}
}
private void revolve() {
// If entries queued up are empty, deconstruct the queue.
if (scriptEntries.isEmpty())
{
stop();
isStarted = false;
return;
}
// Check if this Queue isn't paused
if (paused) return;
// If it's delayed, schedule it for later
if (delayTime > System.currentTimeMillis()) return;
// Criteria met for a successful 'revolution' of this queue...
DenizenAPI.getCurrentInstance().getScriptEngine().revolve(this);
if (scriptEntries.isEmpty())
{
stop();
isStarted = false;
}
}
public ScriptEntry getNext() {
if (!scriptEntries.isEmpty()) {
ScriptEntry entry = scriptEntries.get(0);
scriptEntries.remove(0);
return entry;
}
else return null;
}
public ScriptQueue addEntries(List<ScriptEntry> entries) {
scriptEntries.addAll(entries);
return this;
}
protected boolean hasInjectedItems = false;
public ScriptQueue injectEntries(List<ScriptEntry> entries, int position) {
if (position > scriptEntries.size() || position < 0) position = 1;
if (scriptEntries.size() == 0) position = 0;
scriptEntries.addAll(position, entries);
hasInjectedItems = true;
return this;
}
public boolean removeEntry(int position) {
if (scriptEntries.size() < position) return false;
scriptEntries.remove(position);
return true;
}
public ScriptEntry getEntry(int position) {
if (scriptEntries.size() < position) return null;
return scriptEntries.get(position);
}
public ScriptQueue injectEntry(ScriptEntry entry, int position) {
if (position > scriptEntries.size() || position < 0) position = 1;
if (scriptEntries.size() == 0) position = 0;
scriptEntries.add(position, entry);
hasInjectedItems = true;
return this;
}
public int getQueueSize() {
return scriptEntries.size();
}
}