Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use async scheduler instead of BukkitScheduler for folia #24

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = 'net.simplyvanilla'
version = '0.2.0'
version = '0.2.1'

repositories {
mavenCentral()
Expand Down
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);
}
}