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.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,35 +122,37 @@ public class DataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void fetchPlayerAsync(String playerName) {
|
public void fetchPlayerAsync(String playerName) {
|
||||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
if (executor != null && !executor.isShutdown()) {
|
||||||
try {
|
executor.submit(() -> {
|
||||||
HttpURLConnection conn = createConnection("/players/" + playerName);
|
try {
|
||||||
if (conn.getResponseCode() == 200) {
|
HttpURLConnection conn = createConnection("/players/" + playerName);
|
||||||
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
|
if (conn.getResponseCode() == 200) {
|
||||||
JsonObject json = gson.fromJson(reader, JsonObject.class);
|
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
|
||||||
if (json != null) {
|
JsonObject json = gson.fromJson(reader, JsonObject.class);
|
||||||
Map<String, String> pTiers = new ConcurrentHashMap<>();
|
if (json != null) {
|
||||||
|
Map<String, String> pTiers = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
if (json.has("tiers") && !json.get("tiers").isJsonNull()) {
|
if (json.has("tiers") && !json.get("tiers").isJsonNull()) {
|
||||||
JsonObject tiersObj = json.getAsJsonObject("tiers");
|
JsonObject tiersObj = json.getAsJsonObject("tiers");
|
||||||
for (Map.Entry<String, JsonElement> entry : tiersObj.entrySet()) {
|
for (Map.Entry<String, JsonElement> entry : tiersObj.entrySet()) {
|
||||||
if (!entry.getValue().isJsonNull()) {
|
if (!entry.getValue().isJsonNull()) {
|
||||||
pTiers.put(entry.getKey().toLowerCase(), entry.getValue().getAsString());
|
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()) {
|
if (json.has("position") && !json.get("position").isJsonNull()) {
|
||||||
playerRankCache.put(playerName.toLowerCase(), json.get("position").getAsInt());
|
playerRankCache.put(playerName.toLowerCase(), json.get("position").getAsInt());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPlayerTier(String playerName, String mode) {
|
public String getPlayerTier(String playerName, String mode) {
|
||||||
|
|||||||
@@ -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.
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