Fix Folia compatibility by migrating to native Java Executors for async tasks
This commit is contained in:
@@ -7,8 +7,10 @@ import com.google.gson.JsonObject;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
public class DataManager {
|
||||
@@ -25,6 +27,8 @@ public class DataManager {
|
||||
private final Map<String, Integer> playerRankCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final String[] trackedModes = {"overall", "vanilla", "mace", "axe", "sword", "smp", "diamondsmp", "uhc", "pot", "nethop", "cart"};
|
||||
|
||||
private ScheduledExecutorService executor;
|
||||
|
||||
public DataManager(ETPlaceholdersPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
@@ -32,9 +36,15 @@ public class DataManager {
|
||||
|
||||
public void startTasks() {
|
||||
int minutes = plugin.getConfig().getInt("cache-refresh-minutes", 15);
|
||||
long ticks = minutes * 60 * 20L;
|
||||
|
||||
executor = Executors.newScheduledThreadPool(2);
|
||||
executor.scheduleAtFixedRate(this::refreshLeaderboards, 0, minutes, TimeUnit.MINUTES);
|
||||
}
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::refreshLeaderboards, 0L, ticks);
|
||||
public void stopTasks() {
|
||||
if (executor != null && !executor.isShutdown()) {
|
||||
executor.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private String getApiUrl() {
|
||||
@@ -112,35 +122,37 @@ public class DataManager {
|
||||
}
|
||||
|
||||
public void fetchPlayerAsync(String playerName) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
HttpURLConnection conn = createConnection("/players/" + playerName);
|
||||
if (conn.getResponseCode() == 200) {
|
||||
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
|
||||
JsonObject json = gson.fromJson(reader, JsonObject.class);
|
||||
if (json != null) {
|
||||
Map<String, String> pTiers = new ConcurrentHashMap<>();
|
||||
|
||||
if (json.has("tiers") && !json.get("tiers").isJsonNull()) {
|
||||
JsonObject tiersObj = json.getAsJsonObject("tiers");
|
||||
for (Map.Entry<String, JsonElement> entry : tiersObj.entrySet()) {
|
||||
if (!entry.getValue().isJsonNull()) {
|
||||
pTiers.put(entry.getKey().toLowerCase(), entry.getValue().getAsString());
|
||||
if (executor != null && !executor.isShutdown()) {
|
||||
executor.submit(() -> {
|
||||
try {
|
||||
HttpURLConnection conn = createConnection("/players/" + playerName);
|
||||
if (conn.getResponseCode() == 200) {
|
||||
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
|
||||
JsonObject json = gson.fromJson(reader, JsonObject.class);
|
||||
if (json != null) {
|
||||
Map<String, String> pTiers = new ConcurrentHashMap<>();
|
||||
|
||||
if (json.has("tiers") && !json.get("tiers").isJsonNull()) {
|
||||
JsonObject tiersObj = json.getAsJsonObject("tiers");
|
||||
for (Map.Entry<String, JsonElement> entry : tiersObj.entrySet()) {
|
||||
if (!entry.getValue().isJsonNull()) {
|
||||
pTiers.put(entry.getKey().toLowerCase(), entry.getValue().getAsString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
playerTierCache.put(playerName.toLowerCase(), pTiers);
|
||||
|
||||
playerTierCache.put(playerName.toLowerCase(), pTiers);
|
||||
|
||||
if (json.has("position") && !json.get("position").isJsonNull()) {
|
||||
playerRankCache.put(playerName.toLowerCase(), json.get("position").getAsInt());
|
||||
if (json.has("position") && !json.get("position").isJsonNull()) {
|
||||
playerRankCache.put(playerName.toLowerCase(), json.get("position").getAsInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public String getPlayerTier(String playerName, String mode) {
|
||||
|
||||
@@ -24,7 +24,9 @@ public class ETPlaceholdersPlugin extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
if (dataManager != null) {
|
||||
dataManager.stopTasks();
|
||||
}
|
||||
}
|
||||
|
||||
public DataManager getDataManager() {
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user