Javadoc and some API changes
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package fr.pandacube.lib.paper.scheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||
|
||||
public class AutoUpdatedObject {
|
||||
private static final Plugin plugin = PandaLibPaper.getPlugin();
|
||||
|
||||
private Runnable updater;
|
||||
|
||||
private final List<BukkitTask> tasks = new ArrayList<>();
|
||||
|
||||
|
||||
protected AutoUpdatedObject() { }
|
||||
public AutoUpdatedObject(Runnable updater) {
|
||||
this.updater = Objects.requireNonNull(updater);
|
||||
}
|
||||
|
||||
public synchronized void updateSync() {
|
||||
tasks.add(Bukkit.getScheduler().runTask(plugin, this::update));
|
||||
}
|
||||
|
||||
public synchronized void updateAsync() {
|
||||
tasks.add(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update));
|
||||
}
|
||||
|
||||
public synchronized void updateLater(long delay)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskLater(plugin, this::update, delay));
|
||||
}
|
||||
|
||||
public synchronized void updateLaterAsync(long delay)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::update, delay));
|
||||
}
|
||||
|
||||
public synchronized void updateTimer(long delay, long period)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskTimer(plugin, this::update, delay, period));
|
||||
}
|
||||
|
||||
public synchronized void updateTimerAsync(long delay, long period)
|
||||
throws IllegalArgumentException, IllegalStateException {
|
||||
tasks.add(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::update, delay, period));
|
||||
}
|
||||
|
||||
public synchronized void cancel() {
|
||||
tasks.forEach(BukkitTask::cancel);
|
||||
tasks.clear();
|
||||
}
|
||||
|
||||
public void update() {
|
||||
Objects.requireNonNull(updater, "Please use new AutoUpdatedObject(Runnable) or override the run method.");
|
||||
updater.run();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,104 @@
|
||||
package fr.pandacube.lib.paper.scheduler;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scheduler.BukkitTask;
|
||||
|
||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||
|
||||
/**
|
||||
* An extension of {@link BukkitRunnable} that already integrates a reference to the Bukkit plugin.
|
||||
*/
|
||||
public class PandalibRunnable extends BukkitRunnable {
|
||||
private static final Plugin plugin = PandaLibPaper.getPlugin();
|
||||
|
||||
private Runnable updater;
|
||||
|
||||
|
||||
/**
|
||||
* Instanciate a {@link PandalibRunnable}, whose {@link #run()} method will be called by the server scheduler.
|
||||
* When using this default constructor, the {@link #run()} method must be override to provides code to run.
|
||||
*/
|
||||
protected PandalibRunnable() { }
|
||||
|
||||
/**
|
||||
* Instanciate a {@link PandalibRunnable}, with an {@code updater} that will be called by the server scheduler.
|
||||
* @param updater the updater to run when this task is executed.
|
||||
*/
|
||||
public PandalibRunnable(Runnable updater) {
|
||||
this.updater = Objects.requireNonNull(updater);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Same as {@link #runTask(Plugin)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTask() {
|
||||
return runTask(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #runTaskAsynchronously(Plugin)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTaskAsynchronously() {
|
||||
return runTaskAsynchronously(plugin);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #runTaskLater(Plugin, long)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @param delay the ticks to wait before running the task.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTaskLater(long delay) {
|
||||
return runTaskLater(plugin, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #runTaskLaterAsynchronously(Plugin, long)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @param delay the ticks to wait before running the task.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTaskLaterAsynchronously(long delay) {
|
||||
return runTaskLaterAsynchronously(plugin, delay);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #runTaskTimer(Plugin, long, long)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @param delay the ticks to wait before running the task.
|
||||
* @param period the ticks to wait between runs.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTaskTimer(long delay, long period) {
|
||||
return runTaskTimer(plugin, delay, period);
|
||||
}
|
||||
|
||||
/**
|
||||
* Same as {@link #runTaskTimerAsynchronously(Plugin, long, long)}, but the plugin instance is already provided by
|
||||
* {@link PandaLibPaper#getPlugin()}.
|
||||
* @param delay the ticks to wait before running the task.
|
||||
* @param period the ticks to wait between runs.
|
||||
* @return a {@link BukkitTask} that contains the id number.
|
||||
*/
|
||||
public synchronized BukkitTask runTaskTimerAsynchronously(long delay, long period) {
|
||||
return runTaskTimerAsynchronously(plugin, delay, period);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Objects.requireNonNull(updater, "Please use new PandalibRunnable(Runnable) or override the run method.")
|
||||
.run();
|
||||
}
|
||||
|
||||
}
|
@@ -1,21 +1,45 @@
|
||||
package fr.pandacube.lib.paper.scheduler;
|
||||
|
||||
import java.util.concurrent.Callable;
|
||||
import java.util.concurrent.CancellationException;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import fr.pandacube.lib.util.Log;
|
||||
import fr.pandacube.lib.paper.PandaLibPaper;
|
||||
|
||||
/**
|
||||
* Provides methods to easily manage synchronous and asynchronous operations with the server thread.
|
||||
*/
|
||||
public class SchedulerUtil {
|
||||
|
||||
|
||||
/**
|
||||
* Ensure that the provided runnable is run on the server thread.
|
||||
* If the current thread is the server thread, then the task is run right now, then this method returns.
|
||||
* If the current thread is another thread, it passes over the runnable to the Bukkit scheduler, then returns
|
||||
* imediately.
|
||||
* @param task the task to run on the main thread.
|
||||
*/
|
||||
public static void runOnServerThread(Runnable task) {
|
||||
if (Bukkit.isPrimaryThread())
|
||||
task.run();
|
||||
|
||||
Bukkit.getScheduler().runTask(PandaLibPaper.getPlugin(), task);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runs the provided task on the main thread, and wait for this task to end to return its value.
|
||||
* If the current thread is the server thread, then the task is run right now, then this method returns with the
|
||||
* return value of the provided task. Otherwise, it will wait for the task to run on the main thread, to be able to
|
||||
* return the value from the task.
|
||||
* @param task the task to run on the main thread and to get the value from.
|
||||
* @return the value returned by the task.
|
||||
* @param <T> the type fo the return value of the task
|
||||
* @throws CancellationException – if the task was cancelled
|
||||
* @throws ExecutionException – if the task threw an exception
|
||||
* @throws InterruptedException – if the current thread was interrupted while waiting
|
||||
*/
|
||||
public static <T> T runOnServerThreadAndWait(Callable<T> task) throws Exception {
|
||||
if (Bukkit.isPrimaryThread())
|
||||
return task.call();
|
||||
@@ -29,7 +53,16 @@ public class SchedulerUtil {
|
||||
}
|
||||
}).get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Runs the provided task on the main thread, and wait for this task to end to return.
|
||||
* If the current thread is the server thread, then the task is run right now, then this method returns.
|
||||
* Otherwise, it will wait for the task to finish on the main thread.
|
||||
* @param task the task to run on the main thread.
|
||||
* @throws CancellationException – if the task was cancelled
|
||||
* @throws ExecutionException – if the task threw an exception
|
||||
* @throws InterruptedException – if the current thread was interrupted while waiting
|
||||
*/
|
||||
public static void runOnServerThreadAndWait(Runnable task) throws Exception {
|
||||
runOnServerThreadAndWait((Callable<Void>)() -> {
|
||||
task.run();
|
||||
|
Reference in New Issue
Block a user