Fix Folia compatibility by migrating to native Java Executors for async tasks

This commit is contained in:
2026-09-20 17:20:33 -05:00
parent bfb4225146
commit 528eb01d8e
8 changed files with 40 additions and 32 deletions
@@ -7,8 +7,10 @@ import com.google.gson.JsonObject;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
public class DataManager { public class DataManager {
@@ -26,15 +28,23 @@ public class DataManager {
private final String[] trackedModes = {"overall", "vanilla", "mace", "axe", "sword", "smp", "diamondsmp", "uhc", "pot", "nethop", "cart"}; private final String[] trackedModes = {"overall", "vanilla", "mace", "axe", "sword", "smp", "diamondsmp", "uhc", "pot", "nethop", "cart"};
private ScheduledExecutorService executor;
public DataManager(ETPlaceholdersPlugin plugin) { public DataManager(ETPlaceholdersPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
public void startTasks() { public void startTasks() {
int minutes = plugin.getConfig().getInt("cache-refresh-minutes", 15); int minutes = plugin.getConfig().getInt("cache-refresh-minutes", 15);
long ticks = minutes * 60 * 20L;
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::refreshLeaderboards, 0L, ticks); executor = Executors.newScheduledThreadPool(2);
executor.scheduleAtFixedRate(this::refreshLeaderboards, 0, minutes, TimeUnit.MINUTES);
}
public void stopTasks() {
if (executor != null && !executor.isShutdown()) {
executor.shutdownNow();
}
} }
private String getApiUrl() { private String getApiUrl() {
@@ -112,7 +122,8 @@ public class DataManager {
} }
public void fetchPlayerAsync(String playerName) { public void fetchPlayerAsync(String playerName) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { if (executor != null && !executor.isShutdown()) {
executor.submit(() -> {
try { try {
HttpURLConnection conn = createConnection("/players/" + playerName); HttpURLConnection conn = createConnection("/players/" + playerName);
if (conn.getResponseCode() == 200) { if (conn.getResponseCode() == 200) {
@@ -142,6 +153,7 @@ public class DataManager {
} }
}); });
} }
}
public String getPlayerTier(String playerName, String mode) { public String getPlayerTier(String playerName, String mode) {
String pLow = playerName.toLowerCase(); String pLow = playerName.toLowerCase();
@@ -24,7 +24,9 @@ public class ETPlaceholdersPlugin extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
Bukkit.getScheduler().cancelTasks(this); if (dataManager != null) {
dataManager.stopTasks();
}
} }
public DataManager getDataManager() { public DataManager getDataManager() {
Binary file not shown.
-3
View File
@@ -1,3 +0,0 @@
artifactId=ETPlaceholders
groupId=dev.bailey
version=1.0.0
@@ -1,3 +0,0 @@
dev\bailey\etplaceholders\DataManager.class
dev\bailey\etplaceholders\ETExpansion.class
dev\bailey\etplaceholders\ETPlaceholdersPlugin.class