Skip to content

Commit

Permalink
fix: use async scheduler instead of BukkitScheduler for folia
Browse files Browse the repository at this point in the history
  • Loading branch information
rexlManu committed Feb 14, 2024
1 parent 8c8afa9 commit 301d4bc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,8 +13,7 @@ record CronEntry(int interval, List<String> commands) {

private final SimplyAdminPlugin plugin;

private final Map<CronEntry, Integer> currentCommandIndex = new HashMap<>();
private final Set<Integer> taskIds = new HashSet<>();
private final Map<CronEntry, Integer> currentCommandIndex = new ConcurrentHashMap<>();

public CronScheduleExecutor(SimplyAdminPlugin plugin, ConfigurationSection section) {
this.plugin = plugin;
Expand All @@ -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;
Expand All @@ -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);
}
}

0 comments on commit 301d4bc

Please sign in to comment.