From f57c5596c176496a8fb7bfd73ee7fe168edaf449 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Sun, 30 May 2021 21:37:43 +0800 Subject: [PATCH] Handle async task correctly --- .../java/org/maxgamer/quickshop/QuickShop.java | 14 +++++++++++--- .../quickshop/watcher/CalendarWatcher.java | 8 ++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 0d665beb8..2a4be1ddb 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -38,6 +38,7 @@ import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredListener; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.maxgamer.quickshop.api.QuickShopAPI; @@ -248,6 +249,8 @@ public class QuickShop extends JavaPlugin { @Getter private CalendarWatcher calendarWatcher; + private final List timerTaskList = new ArrayList<>(3); + @NotNull public static QuickShop getInstance() { return instance; @@ -544,6 +547,11 @@ public class QuickShop extends JavaPlugin { if (this.integrationHelper != null) { this.integrationHelper.callIntegrationsUnload(IntegrateStage.onUnloadBegin); } + for (BukkitTask bukkitTask : timerTaskList) { + if (!bukkitTask.isCancelled()) { + bukkitTask.cancel(); + } + } if (calendarWatcher != null) { calendarWatcher.stop(); } @@ -773,7 +781,7 @@ public class QuickShop extends JavaPlugin { displayDupeRemoverWatcher = new DisplayDupeRemoverWatcher(); } if (display && DisplayItem.getNowUsing() != DisplayType.VIRTUALITEM) { - displayDupeRemoverWatcher.runTaskTimerAsynchronously(this, 0, 1); + timerTaskList.add(displayDupeRemoverWatcher.runTaskTimerAsynchronously(this, 0, 1)); } if (display && DisplayItem.getNowUsing() == DisplayType.VIRTUALITEM) { AsyncPacketSender.start(this); @@ -835,12 +843,12 @@ public class QuickShop extends JavaPlugin { shopContainerWatcher.runTaskTimer(this, 0, 5); // Nobody use it if (logWatcher != null) { - logWatcher.runTaskTimerAsynchronously(this, 10, 10); + timerTaskList.add(logWatcher.runTaskTimerAsynchronously(this, 10, 10)); getLogger().info("Log actions is enabled, actions will log in the qs.log file!"); } if (getConfig().getBoolean("shop.ongoing-fee.enable")) { getLogger().info("Ongoing fee feature is enabled."); - ongoingFeeWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.ongoing-fee.ticks")); + timerTaskList.add(ongoingFeeWatcher.runTaskTimerAsynchronously(this, 0, getConfig().getInt("shop.ongoing-fee.ticks"))); } diff --git a/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java b/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java index 52023494e..52f56bcbb 100644 --- a/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java +++ b/src/main/java/org/maxgamer/quickshop/watcher/CalendarWatcher.java @@ -23,6 +23,7 @@ import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.scheduler.BukkitTask; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.event.CalendarEvent; import org.maxgamer.quickshop.util.Util; @@ -38,6 +39,7 @@ public class CalendarWatcher extends BukkitRunnable { @Getter private final YamlConfiguration configuration; private final QuickShop plugin; + private BukkitTask task; public CalendarWatcher(QuickShop plugin) { this.plugin = plugin; @@ -53,13 +55,15 @@ public class CalendarWatcher extends BukkitRunnable { } public void start() { - this.runTaskTimerAsynchronously(plugin, 20, 20); + task = this.runTaskTimerAsynchronously(plugin, 20, 20); } public void stop() { save(); try { - this.cancel(); + if (task != null && !task.isCancelled()) { + task.cancel(); + } } catch (IllegalStateException ignored) { } }