Added async task scheduling
This commit is contained in:
parent
af286d685d
commit
12e2c2963d
4 changed files with 24 additions and 0 deletions
|
@ -169,6 +169,14 @@ public final class Reflections {
|
|||
.setStrict(FoliaUtil.isFolia())
|
||||
.toMethodReflection();
|
||||
|
||||
public static final ReflectionLazyLoader<Method> FOLIA_RUN_NOW_ASYNC =
|
||||
new ReflectionBuilder(ASYNC_SCHEDULER_CLASS)
|
||||
.withMethodName("runNow")
|
||||
.withParameterTypes(Plugin.class, Consumer.class)
|
||||
.withExpectResult(SCHEDULED_TASK_CLASS)
|
||||
.setStrict(FoliaUtil.isFolia())
|
||||
.toMethodReflection();
|
||||
|
||||
public static final ReflectionLazyLoader<Method> FOLIA_RUN_DELAYED_ASYNC =
|
||||
new ReflectionBuilder(ASYNC_SCHEDULER_CLASS)
|
||||
.withMethodName("runDelayed")
|
||||
|
|
|
@ -43,6 +43,16 @@ public class FoliaScheduler extends TaskScheduler {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runAsyncGlobal(Runnable runnable) {
|
||||
try {
|
||||
Object scheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null);
|
||||
Reflections.FOLIA_RUN_NOW_ASYNC.get().invoke(scheduler, plugin, (Consumer<Object>) o -> runnable.run());
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runLaterAsync(Runnable runnable, long delay) {
|
||||
try {
|
||||
|
|
|
@ -24,6 +24,11 @@ public class SpigotScheduler extends TaskScheduler {
|
|||
Bukkit.getScheduler().runTask(plugin, runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runAsyncGlobal(Runnable runnable) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runLaterAsync(Runnable runnable, long delay) {
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, delay);
|
||||
|
|
|
@ -13,6 +13,7 @@ public abstract class TaskScheduler {
|
|||
public abstract void schedulePlayerChat(Player player, String message);
|
||||
public abstract void schedulePlayerCommand(Player player, String command);
|
||||
public abstract void runSyncGlobal(Runnable runnable);
|
||||
public abstract void runAsyncGlobal(Runnable runnable);
|
||||
public abstract void runLaterAsync(Runnable runnable, long delay);
|
||||
public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long interval);
|
||||
public abstract void cancelAll();
|
||||
|
|
Loading…
Reference in a new issue