From 301d4bc25e1c450fa55a39d0ccf6e19d981ca50a Mon Sep 17 00:00:00 2001 From: Emmanuel Lampe Date: Wed, 14 Feb 2024 23:25:16 +0100 Subject: [PATCH] fix: use async scheduler instead of BukkitScheduler for folia --- .../simplyadmin/SimplyAdminPlugin.java | 5 ----- .../cron/CronScheduleExecutor.java | 22 +++++-------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/src/main/java/net/simplyvanilla/simplyadmin/SimplyAdminPlugin.java b/src/main/java/net/simplyvanilla/simplyadmin/SimplyAdminPlugin.java index f97d759..026cef9 100644 --- a/src/main/java/net/simplyvanilla/simplyadmin/SimplyAdminPlugin.java +++ b/src/main/java/net/simplyvanilla/simplyadmin/SimplyAdminPlugin.java @@ -40,11 +40,6 @@ public void onEnable() { manager.registerEvents(new NewPlayerListener(this), this); } - @Override - public void onDisable() { - this.cronScheduleExecutor.cancel(); - } - public Component getMessage(String key, TagResolver... tagResolvers) { String string = this.getConfig().getString("messages." + key); if (string == null) { diff --git a/src/main/java/net/simplyvanilla/simplyadmin/cron/CronScheduleExecutor.java b/src/main/java/net/simplyvanilla/simplyadmin/cron/CronScheduleExecutor.java index 31a56d7..32dbc57 100644 --- a/src/main/java/net/simplyvanilla/simplyadmin/cron/CronScheduleExecutor.java +++ b/src/main/java/net/simplyvanilla/simplyadmin/cron/CronScheduleExecutor.java @@ -1,10 +1,9 @@ package net.simplyvanilla.simplyadmin.cron; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Map; -import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import net.simplyvanilla.simplyadmin.SimplyAdminPlugin; import org.bukkit.configuration.ConfigurationSection; @@ -14,8 +13,7 @@ record CronEntry(int interval, List commands) { private final SimplyAdminPlugin plugin; - private final Map currentCommandIndex = new HashMap<>(); - private final Set taskIds = new HashSet<>(); + private final Map currentCommandIndex = new ConcurrentHashMap<>(); public CronScheduleExecutor(SimplyAdminPlugin plugin, ConfigurationSection section) { this.plugin = plugin; @@ -37,8 +35,8 @@ private void parseEntries(ConfigurationSection section) { } private void schedule(CronEntry entry) { - int taskId = - this.plugin.getServer().getScheduler().scheduleSyncRepeatingTask(this.plugin, () -> { + this.plugin.getServer().getAsyncScheduler() + .runAtFixedRate(this.plugin, scheduledTask -> { int currentIndex = this.currentCommandIndex.getOrDefault(entry, 0); if (currentIndex >= entry.commands.size()) { currentIndex = 0; @@ -48,14 +46,6 @@ private void schedule(CronEntry entry) { this.plugin.getServer() .dispatchCommand(this.plugin.getServer().getConsoleSender(), command); this.currentCommandIndex.put(entry, currentIndex + 1); - }, 0L, entry.interval * 20L); - - this.taskIds.add(taskId); - } - - public void cancel() { - for (int taskId : this.taskIds) { - this.plugin.getServer().getScheduler().cancelTask(taskId); - } + }, 0, entry.interval, TimeUnit.SECONDS); } }