Add BukkitTask#isCancelled

By: blablubbabc <lukas@wirsindwir.de>
This commit is contained in:
Bukkit/Spigot 2017-08-29 14:42:14 +02:00
parent 3fdb71844b
commit 5c2d43ae39
2 changed files with 42 additions and 21 deletions

View file

@ -7,7 +7,18 @@ import org.bukkit.plugin.Plugin;
* This class is provided as an easy way to handle scheduling tasks.
*/
public abstract class BukkitRunnable implements Runnable {
private int taskId = -1;
private BukkitTask task;
/**
* Returns true if this task has been cancelled.
*
* @return true if the task has been cancelled
* @throws IllegalStateException if task was not scheduled yet
*/
public synchronized boolean isCancelled() throws IllegalStateException {
checkScheduled();
return task.isCancelled();
}
/**
* Attempts to cancel this task.
@ -28,8 +39,8 @@ public abstract class BukkitRunnable implements Runnable {
* @see BukkitScheduler#runTask(Plugin, Runnable)
*/
public synchronized BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTask(plugin, (Runnable) this));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTask(plugin, (Runnable) this));
}
/**
@ -45,8 +56,8 @@ public abstract class BukkitRunnable implements Runnable {
* @see BukkitScheduler#runTaskAsynchronously(Plugin, Runnable)
*/
public synchronized BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this));
}
/**
@ -60,8 +71,8 @@ public abstract class BukkitRunnable implements Runnable {
* @see BukkitScheduler#runTaskLater(Plugin, Runnable, long)
*/
public synchronized BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTaskLater(plugin, (Runnable) this, delay));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTaskLater(plugin, (Runnable) this, delay));
}
/**
@ -79,8 +90,8 @@ public abstract class BukkitRunnable implements Runnable {
* @see BukkitScheduler#runTaskLaterAsynchronously(Plugin, Runnable, long)
*/
public synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, (Runnable) this, delay));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, (Runnable) this, delay));
}
/**
@ -96,8 +107,8 @@ public abstract class BukkitRunnable implements Runnable {
* @see BukkitScheduler#runTaskTimer(Plugin, Runnable, long, long)
*/
public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period));
}
/**
@ -118,8 +129,8 @@ public abstract class BukkitRunnable implements Runnable {
* long)
*/
public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
checkState();
return setupId(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, (Runnable) this, delay, period));
checkNotYetScheduled();
return setupTask(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, (Runnable) this, delay, period));
}
/**
@ -129,21 +140,24 @@ public abstract class BukkitRunnable implements Runnable {
* @throws IllegalStateException if task was not scheduled yet
*/
public synchronized int getTaskId() throws IllegalStateException {
final int id = taskId;
if (id == -1) {
checkScheduled();
return task.getTaskId();
}
private void checkScheduled() {
if (task == null) {
throw new IllegalStateException("Not scheduled yet");
}
return id;
}
private void checkState() {
if (taskId != -1) {
throw new IllegalStateException("Already scheduled as " + taskId);
private void checkNotYetScheduled() {
if (task != null) {
throw new IllegalStateException("Already scheduled as " + task.getTaskId());
}
}
private BukkitTask setupId(final BukkitTask task) {
this.taskId = task.getTaskId();
private BukkitTask setupTask(final BukkitTask task) {
this.task = task;
return task;
}
}

View file

@ -28,6 +28,13 @@ public interface BukkitTask {
*/
public boolean isSync();
/**
* Returns true if this task has been cancelled.
*
* @return true if the task has been cancelled
*/
public boolean isCancelled();
/**
* Will attempt to cancel this task.
*/