Wrap Folia scheduler in the same check as Bukkit scheduler to ensure tasks running at disable don't get sent to the scheduler

This commit is contained in:
Vankka 2023-06-19 18:40:36 +03:00
parent 6d744ccec4
commit c8590cfe2f
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
3 changed files with 27 additions and 16 deletions

View File

@ -19,6 +19,8 @@
package com.discordsrv.bukkit.scheduler;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.bukkit.DiscordSRVBukkitBootstrap;
import com.discordsrv.common.DiscordSRV;
import com.discordsrv.common.scheduler.ServerScheduler;
import com.discordsrv.common.scheduler.StandardScheduler;
import org.bukkit.Server;
@ -35,6 +37,16 @@ public abstract class AbstractBukkitScheduler extends StandardScheduler implemen
this.discordSRV = discordSRV;
}
protected void checkDisable(Runnable task, BiConsumer<Server, Plugin> runNormal) {
// Can't run tasks when disabling, so we'll push those to the bootstrap to run after disable
if (!discordSRV.plugin().isEnabled() && discordSRV.status() == DiscordSRV.Status.SHUTTING_DOWN) {
((DiscordSRVBukkitBootstrap) discordSRV.bootstrap()).mainThreadTasksForDisable().add(task);
return;
}
runWithArgs(runNormal);
}
@Override
public void runWithArgs(BiConsumer<Server, Plugin> runNormal) {
runNormal.accept(discordSRV.server(), discordSRV.plugin());

View File

@ -19,12 +19,6 @@
package com.discordsrv.bukkit.scheduler;
import com.discordsrv.bukkit.BukkitDiscordSRV;
import com.discordsrv.bukkit.DiscordSRVBukkitBootstrap;
import com.discordsrv.common.DiscordSRV;
import org.bukkit.Server;
import org.bukkit.plugin.Plugin;
import java.util.function.BiConsumer;
public class BukkitScheduler extends AbstractBukkitScheduler {
@ -32,16 +26,6 @@ public class BukkitScheduler extends AbstractBukkitScheduler {
super(discordSRV);
}
protected void checkDisable(Runnable task, BiConsumer<Server, Plugin> runNormal) {
// Can't run tasks when disabling, so we'll push those to the bootstrap to run after disable
if (!discordSRV.plugin().isEnabled() && discordSRV.status() == DiscordSRV.Status.SHUTTING_DOWN) {
((DiscordSRVBukkitBootstrap) discordSRV.bootstrap()).mainThreadTasksForDisable().add(task);
return;
}
runWithArgs(runNormal);
}
@Override
public void runOnMainThread(Runnable task) {
checkDisable(task, (server, plugin) -> server.getScheduler().runTask(plugin, task));

View File

@ -25,4 +25,19 @@ public class FoliaScheduler extends AbstractBukkitScheduler implements IFoliaSch
public FoliaScheduler(BukkitDiscordSRV discordSRV) {
super(discordSRV);
}
@Override
public void runOnMainThread(Runnable task) {
checkDisable(task, (server, plugin) -> IFoliaScheduler.super.runOnMainThread(task));
}
@Override
public void runOnMainThreadLaterInTicks(Runnable task, int ticks) {
checkDisable(task, (server, plugin) -> IFoliaScheduler.super.runOnMainThreadLaterInTicks(task, ticks));
}
@Override
public void runOnMainThreadAtFixedRateInTicks(Runnable task, int initialTicks, int rateTicks) {
checkDisable(task, (server, plugin) -> IFoliaScheduler.super.runOnMainThreadAtFixedRateInTicks(task, initialTicks, rateTicks));
}
}