cancel tasks on plugin disable
This commit is contained in:
parent
40c31b44aa
commit
e3d5ebab8b
5 changed files with 36 additions and 0 deletions
|
@ -212,6 +212,7 @@ public class ZNpcsPlus extends JavaPlugin implements ZApi {
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
if (!enabled) return;
|
if (!enabled) return;
|
||||||
|
scheduler.cancelAll();
|
||||||
npcRegistry.save();
|
npcRegistry.save();
|
||||||
ZApiProvider.unregister();
|
ZApiProvider.unregister();
|
||||||
Bukkit.getOnlinePlayers().forEach(userManager::remove);
|
Bukkit.getOnlinePlayers().forEach(userManager::remove);
|
||||||
|
|
|
@ -117,6 +117,18 @@ public final class Reflections {
|
||||||
.setStrict(FoliaUtil.isFolia())
|
.setStrict(FoliaUtil.isFolia())
|
||||||
.toMethodReflection();
|
.toMethodReflection();
|
||||||
|
|
||||||
|
public static final ReflectionLazyLoader<Method> FOLIA_CANCEL_ASYNC_TASKS =
|
||||||
|
new ReflectionBuilder(ASYNC_SCHEDULER_CLASS)
|
||||||
|
.withMethodName("cancelTasks")
|
||||||
|
.setStrict(FoliaUtil.isFolia())
|
||||||
|
.toMethodReflection();
|
||||||
|
|
||||||
|
public static final ReflectionLazyLoader<Method> FOLIA_CANCEL_GLOBAL_TASKS =
|
||||||
|
new ReflectionBuilder(GLOBAL_REGION_SCHEDULER_CLASS)
|
||||||
|
.withMethodName("cancelTasks")
|
||||||
|
.setStrict(FoliaUtil.isFolia())
|
||||||
|
.toMethodReflection();
|
||||||
|
|
||||||
public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC =
|
public static final ReflectionLazyLoader<Method> FOLIA_TELEPORT_ASYNC =
|
||||||
new ReflectionBuilder(Entity.class)
|
new ReflectionBuilder(Entity.class)
|
||||||
.withMethodName("teleportAsync")
|
.withMethodName("teleportAsync")
|
||||||
|
|
|
@ -41,5 +41,21 @@ public class FoliaScheduler extends TaskScheduler {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelAll() {
|
||||||
|
try {
|
||||||
|
Object asyncScheduler = Reflections.FOLIA_GET_ASYNC_SCHEDULER.get().invoke(null);
|
||||||
|
Reflections.FOLIA_CANCEL_ASYNC_TASKS.get().invoke(asyncScheduler, plugin);
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Object globalScheduler = Reflections.FOLIA_GET_GLOBAL_REGION_SCHEDULER.get().invoke(null);
|
||||||
|
Reflections.FOLIA_CANCEL_GLOBAL_TASKS.get().invoke(globalScheduler, plugin);
|
||||||
|
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,4 +22,9 @@ public class SpigotScheduler extends TaskScheduler {
|
||||||
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) {
|
public void runDelayedTimerAsync(Runnable runnable, long delay, long ticks) {
|
||||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks);
|
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, delay, ticks);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancelAll() {
|
||||||
|
Bukkit.getScheduler().cancelTasks(plugin);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,6 @@ public abstract class TaskScheduler {
|
||||||
public abstract void runLaterAsync(Runnable runnable, long ticks);
|
public abstract void runLaterAsync(Runnable runnable, long ticks);
|
||||||
|
|
||||||
public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long ticks);
|
public abstract void runDelayedTimerAsync(Runnable runnable, long delay, long ticks);
|
||||||
|
|
||||||
|
public abstract void cancelAll();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue