/
TimedQueue.java
174 lines (134 loc) · 4.49 KB
/
TimedQueue.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
package net.aufdemrand.denizen.scripts.queues.core;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.scripts.queues.ScriptQueue;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import org.bukkit.Bukkit;
public class TimedQueue extends ScriptQueue implements Delayable {
/**
* Gets a TimedQueue instance.
*
* If a queue already exists with the given id, it will return that instance,
* which may be currently running, unless the type of queue is not a TimedQueue.
* If a queue does not exist, a new stopped queue is created instead.
*
* IDs are case insensitive. If having an easy-to-recall ID is not necessary, just
* pass along null as the id, and it will use ScriptQueue's static method _getNextId()
* which will return a random UUID.
*
* The speed node will be automatically read from the configuration,
* and new ScriptQueues may need further information before they
* can start(), including entries, delays, loops, and possibly context.
*
* @param id unique id of the queue
* @return a TimedQueue
*/
public static TimedQueue getQueue(String id) {
// Get id if not specified.
if (id == null) id = String.valueOf(_getNextId());
TimedQueue scriptQueue;
// Does the queue already exist? Get it if it does.
if (_queueExists(id))
scriptQueue = (TimedQueue) ScriptQueue._queues.get(id.toUpperCase());
// If not, create a new one.
else {
scriptQueue = new TimedQueue(id,
Duration.valueOf(Settings.ScriptQueueSpeed()));
}
// Return the queue
return scriptQueue;
}
/////////////////////
// Private instance fields and constructors
/////////////////////
// Keep track of Bukkit's Scheduler taskId
// for the engine, used when it times out.
private int task_id;
// The speed of the engine, the # of ticks
// between each revolution. Use setSpeed()
// to change this.
private long ticks;
// ScriptQueues can be paused mid-rotation.
// The next entry will be held up until
// un-paused.
protected boolean paused = false;
// The delay in ticks can put off the
// start of a queue
protected long delay_ticks = 0;
@Override
public void delayFor(Duration duration) {
delay_ticks = System.currentTimeMillis() + duration.getMillis();
}
@Override
public boolean isDelayed() {
return (delay_ticks > System.currentTimeMillis());
}
public TimedQueue(String id, Duration timing) {
super(id);
ticks = timing.getTicks();
}
/////////////////////
// Public instance setters and getters
/////////////////////
/**
* Gets the speed of the queue. This is the
* time in between each revolution. '
*
* @return a Duration of the speed.
*/
public Duration getSpeed() {
return new Duration(ticks);
}
/**
* Pauses the queue. Paused queues will check
* to be re-resumed every 'rotation', defined
* by the speed of the queue.
*
* @param paused
*/
@Override
public TimedQueue setPaused(boolean paused) {
this.paused = paused;
return this;
}
/**
* Checks if the queue is currently paused.
*
* @return true if paused.
*/
@Override
public boolean isPaused() {
return paused;
}
/**
* Sets the speed of a queue. Uses bukkit's 'ticks', which is
* 20 ticks per second.
*
* @param ticks the number of ticks between each rotation.
*/
public TimedQueue setSpeed(long ticks) {
this.ticks = ticks;
return this;
}
@Override
protected void onStart() {
// Do the first revolution now...
revolve();
// ...and schedule the rest for later.
task_id = Bukkit.getScheduler().scheduleSyncRepeatingTask(DenizenAPI.getCurrentInstance(),
new Runnable() {
@Override public void run() { revolve(); }
}, ticks == 0 ? 1 : ticks, ticks == 0 ? 1 : ticks);
}
@Override
protected void onStop() {
Bukkit.getScheduler().cancelTask(task_id);
}
@Override
protected boolean shouldRevolve() {
// Check if this Queue isn't paused
if (paused) return false;
// If it's delayed, schedule it for later
return !isDelayed();
}
}