Handle async task correctly

This commit is contained in:
sandtechnology committed 2021-05-30 21:55:44 +08:00
1 parent d488ca64ab
commit f57c5596c1
2 files changed
+17 -5

No files matched your search

@@ -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<BukkitTask> 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")));
}
@@ -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) {
}
}