Commits
blablubbabc authored and md_5 committed a9d0c37ce41
1 1 | package org.bukkit.scheduler; |
2 2 | |
3 3 | import org.bukkit.Bukkit; |
4 4 | import org.bukkit.plugin.Plugin; |
5 5 | |
6 6 | /** |
7 7 | * This class is provided as an easy way to handle scheduling tasks. |
8 8 | */ |
9 9 | public abstract class BukkitRunnable implements Runnable { |
10 - | private int taskId = -1; |
10 + | private BukkitTask task; |
11 + | |
12 + | /** |
13 + | * Returns true if this task has been cancelled. |
14 + | * |
15 + | * @return true if the task has been cancelled |
16 + | * @throws IllegalStateException if task was not scheduled yet |
17 + | */ |
18 + | public synchronized boolean isCancelled() throws IllegalStateException { |
19 + | checkScheduled(); |
20 + | return task.isCancelled(); |
21 + | } |
11 22 | |
12 23 | /** |
13 24 | * Attempts to cancel this task. |
14 25 | * |
15 26 | * @throws IllegalStateException if task was not scheduled yet |
16 27 | */ |
17 28 | public synchronized void cancel() throws IllegalStateException { |
18 29 | Bukkit.getScheduler().cancelTask(getTaskId()); |
19 30 | } |
20 31 | |
21 32 | /** |
22 33 | * Schedules this in the Bukkit scheduler to run on next tick. |
23 34 | * |
24 35 | * @param plugin the reference to the plugin scheduling task |
25 36 | * @return a BukkitTask that contains the id number |
26 37 | * @throws IllegalArgumentException if plugin is null |
27 38 | * @throws IllegalStateException if this was already scheduled |
28 39 | * @see BukkitScheduler#runTask(Plugin, Runnable) |
29 40 | */ |
30 41 | public synchronized BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException { |
31 - | checkState(); |
32 - | return setupId(Bukkit.getScheduler().runTask(plugin, (Runnable) this)); |
42 + | checkNotYetScheduled(); |
43 + | return setupTask(Bukkit.getScheduler().runTask(plugin, (Runnable) this)); |
33 44 | } |
34 45 | |
35 46 | /** |
36 47 | * <b>Asynchronous tasks should never access any API in Bukkit. Great care |
37 48 | * should be taken to assure the thread-safety of asynchronous tasks.</b> |
38 49 | * <p> |
39 50 | * Schedules this in the Bukkit scheduler to run asynchronously. |
40 51 | * |
41 52 | * @param plugin the reference to the plugin scheduling task |
42 53 | * @return a BukkitTask that contains the id number |
43 54 | * @throws IllegalArgumentException if plugin is null |
44 55 | * @throws IllegalStateException if this was already scheduled |
45 56 | * @see BukkitScheduler#runTaskAsynchronously(Plugin, Runnable) |
46 57 | */ |
47 58 | public synchronized BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException { |
48 - | checkState(); |
49 - | return setupId(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this)); |
59 + | checkNotYetScheduled(); |
60 + | return setupTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this)); |
50 61 | } |
51 62 | |
52 63 | /** |
53 64 | * Schedules this to run after the specified number of server ticks. |
54 65 | * |
55 66 | * @param plugin the reference to the plugin scheduling task |
56 67 | * @param delay the ticks to wait before running the task |
57 68 | * @return a BukkitTask that contains the id number |
58 69 | * @throws IllegalArgumentException if plugin is null |
59 70 | * @throws IllegalStateException if this was already scheduled |
60 71 | * @see BukkitScheduler#runTaskLater(Plugin, Runnable, long) |
61 72 | */ |
62 73 | public synchronized BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException { |
63 - | checkState(); |
64 - | return setupId(Bukkit.getScheduler().runTaskLater(plugin, (Runnable) this, delay)); |
74 + | checkNotYetScheduled(); |
75 + | return setupTask(Bukkit.getScheduler().runTaskLater(plugin, (Runnable) this, delay)); |
65 76 | } |
66 77 | |
67 78 | /** |
68 79 | * <b>Asynchronous tasks should never access any API in Bukkit. Great care |
69 80 | * should be taken to assure the thread-safety of asynchronous tasks.</b> |
70 81 | * <p> |
71 82 | * Schedules this to run asynchronously after the specified number of |
72 83 | * server ticks. |
73 84 | * |
74 85 | * @param plugin the reference to the plugin scheduling task |
75 86 | * @param delay the ticks to wait before running the task |
76 87 | * @return a BukkitTask that contains the id number |
77 88 | * @throws IllegalArgumentException if plugin is null |
78 89 | * @throws IllegalStateException if this was already scheduled |
79 90 | * @see BukkitScheduler#runTaskLaterAsynchronously(Plugin, Runnable, long) |
80 91 | */ |
81 92 | public synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException { |
82 - | checkState(); |
83 - | return setupId(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, (Runnable) this, delay)); |
93 + | checkNotYetScheduled(); |
94 + | return setupTask(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, (Runnable) this, delay)); |
84 95 | } |
85 96 | |
86 97 | /** |
87 98 | * Schedules this to repeatedly run until cancelled, starting after the |
88 99 | * specified number of server ticks. |
89 100 | * |
90 101 | * @param plugin the reference to the plugin scheduling task |
91 102 | * @param delay the ticks to wait before running the task |
92 103 | * @param period the ticks to wait between runs |
93 104 | * @return a BukkitTask that contains the id number |
94 105 | * @throws IllegalArgumentException if plugin is null |
95 106 | * @throws IllegalStateException if this was already scheduled |
96 107 | * @see BukkitScheduler#runTaskTimer(Plugin, Runnable, long, long) |
97 108 | */ |
98 109 | public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException { |
99 - | checkState(); |
100 - | return setupId(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period)); |
110 + | checkNotYetScheduled(); |
111 + | return setupTask(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period)); |
101 112 | } |
102 113 | |
103 114 | /** |
104 115 | * <b>Asynchronous tasks should never access any API in Bukkit. Great care |
105 116 | * should be taken to assure the thread-safety of asynchronous tasks.</b> |
106 117 | * <p> |
107 118 | * Schedules this to repeatedly run asynchronously until cancelled, |
108 119 | * starting after the specified number of server ticks. |
109 120 | * |
110 121 | * @param plugin the reference to the plugin scheduling task |
111 122 | * @param delay the ticks to wait before running the task for the first |
112 123 | * time |
113 124 | * @param period the ticks to wait between runs |
114 125 | * @return a BukkitTask that contains the id number |
115 126 | * @throws IllegalArgumentException if plugin is null |
116 127 | * @throws IllegalStateException if this was already scheduled |
117 128 | * @see BukkitScheduler#runTaskTimerAsynchronously(Plugin, Runnable, long, |
118 129 | * long) |
119 130 | */ |
120 131 | public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException { |
121 - | checkState(); |
122 - | return setupId(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, (Runnable) this, delay, period)); |
132 + | checkNotYetScheduled(); |
133 + | return setupTask(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, (Runnable) this, delay, period)); |
123 134 | } |
124 135 | |
125 136 | /** |
126 137 | * Gets the task id for this runnable. |
127 138 | * |
128 139 | * @return the task id that this runnable was scheduled as |
129 140 | * @throws IllegalStateException if task was not scheduled yet |
130 141 | */ |
131 142 | public synchronized int getTaskId() throws IllegalStateException { |
132 - | final int id = taskId; |
133 - | if (id == -1) { |
143 + | checkScheduled(); |
144 + | return task.getTaskId(); |
145 + | } |
146 + | |
147 + | private void checkScheduled() { |
148 + | if (task == null) { |
134 149 | throw new IllegalStateException("Not scheduled yet"); |
135 150 | } |
136 - | return id; |
137 151 | } |
138 152 | |
139 - | private void checkState() { |
140 - | if (taskId != -1) { |
141 - | throw new IllegalStateException("Already scheduled as " + taskId); |
153 + | private void checkNotYetScheduled() { |
154 + | if (task != null) { |
155 + | throw new IllegalStateException("Already scheduled as " + task.getTaskId()); |
142 156 | } |
143 157 | } |
144 158 | |
145 - | private BukkitTask setupId(final BukkitTask task) { |
146 - | this.taskId = task.getTaskId(); |
159 + | private BukkitTask setupTask(final BukkitTask task) { |
160 + | this.task = task; |
147 161 | return task; |
148 162 | } |
149 163 | } |