mirror of
https://github.com/PaperMC/Paper.git
synced 2025-08-14 11:45:52 -07:00
@@ -7,7 +7,18 @@ import org.bukkit.plugin.Plugin;
|
|||||||
* This class is provided as an easy way to handle scheduling tasks.
|
* This class is provided as an easy way to handle scheduling tasks.
|
||||||
*/
|
*/
|
||||||
public abstract class BukkitRunnable implements Runnable {
|
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.
|
* Attempts to cancel this task.
|
||||||
@@ -28,8 +39,8 @@ public abstract class BukkitRunnable implements Runnable {
|
|||||||
* @see BukkitScheduler#runTask(Plugin, Runnable)
|
* @see BukkitScheduler#runTask(Plugin, Runnable)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTask(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTask(plugin, (Runnable) this));
|
return setupTask(Bukkit.getScheduler().runTask(plugin, (Runnable) this));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,8 +56,8 @@ public abstract class BukkitRunnable implements Runnable {
|
|||||||
* @see BukkitScheduler#runTaskAsynchronously(Plugin, Runnable)
|
* @see BukkitScheduler#runTaskAsynchronously(Plugin, Runnable)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTaskAsynchronously(Plugin plugin) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this));
|
return setupTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, (Runnable) this));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -60,8 +71,8 @@ public abstract class BukkitRunnable implements Runnable {
|
|||||||
* @see BukkitScheduler#runTaskLater(Plugin, Runnable, long)
|
* @see BukkitScheduler#runTaskLater(Plugin, Runnable, long)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTaskLater(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTaskLater(plugin, (Runnable) this, delay));
|
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)
|
* @see BukkitScheduler#runTaskLaterAsynchronously(Plugin, Runnable, long)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTaskLaterAsynchronously(Plugin plugin, long delay) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, (Runnable) this, delay));
|
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)
|
* @see BukkitScheduler#runTaskTimer(Plugin, Runnable, long, long)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTaskTimer(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period));
|
return setupTask(Bukkit.getScheduler().runTaskTimer(plugin, (Runnable) this, delay, period));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -118,8 +129,8 @@ public abstract class BukkitRunnable implements Runnable {
|
|||||||
* long)
|
* long)
|
||||||
*/
|
*/
|
||||||
public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
|
public synchronized BukkitTask runTaskTimerAsynchronously(Plugin plugin, long delay, long period) throws IllegalArgumentException, IllegalStateException {
|
||||||
checkState();
|
checkNotYetScheduled();
|
||||||
return setupId(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, (Runnable) this, delay, period));
|
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
|
* @throws IllegalStateException if task was not scheduled yet
|
||||||
*/
|
*/
|
||||||
public synchronized int getTaskId() throws IllegalStateException {
|
public synchronized int getTaskId() throws IllegalStateException {
|
||||||
final int id = taskId;
|
checkScheduled();
|
||||||
if (id == -1) {
|
return task.getTaskId();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkScheduled() {
|
||||||
|
if (task == null) {
|
||||||
throw new IllegalStateException("Not scheduled yet");
|
throw new IllegalStateException("Not scheduled yet");
|
||||||
}
|
}
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkState() {
|
private void checkNotYetScheduled() {
|
||||||
if (taskId != -1) {
|
if (task != null) {
|
||||||
throw new IllegalStateException("Already scheduled as " + taskId);
|
throw new IllegalStateException("Already scheduled as " + task.getTaskId());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private BukkitTask setupId(final BukkitTask task) {
|
private BukkitTask setupTask(final BukkitTask task) {
|
||||||
this.taskId = task.getTaskId();
|
this.task = task;
|
||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -28,6 +28,13 @@ public interface BukkitTask {
|
|||||||
*/
|
*/
|
||||||
public boolean isSync();
|
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.
|
* Will attempt to cancel this task.
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user