Initial commit: ET Placeholders plugin
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package dev.bailey.etplaceholders;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
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 org.bukkit.Bukkit;
|
||||
|
||||
public class DataManager {
|
||||
|
||||
private final ETPlaceholdersPlugin plugin;
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
private final Map<String, Map<Integer, String>> leaderboardCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<String, Map<Integer, String>> leaderboardTierCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<String, Map<String, String>> playerTierCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final Map<String, Integer> playerRankCache = new ConcurrentHashMap<>();
|
||||
|
||||
private final String[] trackedModes = {"overall", "vanilla", "mace", "axe", "sword", "smp", "diamondsmp", "uhc", "pot", "nethop", "cart"};
|
||||
|
||||
public DataManager(ETPlaceholdersPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void startTasks() {
|
||||
int minutes = plugin.getConfig().getInt("cache-refresh-minutes", 15);
|
||||
long ticks = minutes * 60 * 20L;
|
||||
|
||||
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::refreshLeaderboards, 0L, ticks);
|
||||
}
|
||||
|
||||
private String getApiUrl() {
|
||||
String url = plugin.getConfig().getString("api-url", "https://api.eaglertiers.com/api");
|
||||
if (url.endsWith("/")) {
|
||||
url = url.substring(0, url.length() - 1);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
private HttpURLConnection createConnection(String endpoint) throws Exception {
|
||||
String urlStr = getApiUrl() + endpoint;
|
||||
String key = plugin.getConfig().getString("api-key", "");
|
||||
if (key != null && !key.trim().isEmpty()) {
|
||||
if (urlStr.contains("?")) {
|
||||
urlStr += "&key=" + key;
|
||||
} else {
|
||||
urlStr += "?key=" + key;
|
||||
}
|
||||
}
|
||||
|
||||
URL url = new URL(urlStr);
|
||||
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
|
||||
conn.setRequestMethod("GET");
|
||||
conn.setRequestProperty("Accept", "application/json");
|
||||
conn.setConnectTimeout(5000);
|
||||
conn.setReadTimeout(5000);
|
||||
|
||||
if (key != null && !key.trim().isEmpty()) {
|
||||
conn.setRequestProperty("X-API-Key", key);
|
||||
}
|
||||
|
||||
return conn;
|
||||
}
|
||||
|
||||
public void refreshLeaderboards() {
|
||||
for (String mode : trackedModes) {
|
||||
try {
|
||||
String endpoint = mode.equalsIgnoreCase("overall") ? "/players" : "/rankings/" + mode;
|
||||
HttpURLConnection conn = createConnection(endpoint);
|
||||
|
||||
if (conn.getResponseCode() == 200) {
|
||||
try (InputStreamReader reader = new InputStreamReader(conn.getInputStream())) {
|
||||
JsonObject json = gson.fromJson(reader, JsonObject.class);
|
||||
if (json != null && json.has("data")) {
|
||||
JsonArray data = json.getAsJsonArray("data");
|
||||
Map<Integer, String> modeTopNames = new ConcurrentHashMap<>();
|
||||
Map<Integer, String> modeTopTiers = new ConcurrentHashMap<>();
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
JsonObject pObj = data.get(i).getAsJsonObject();
|
||||
String name = pObj.has("username") && !pObj.get("username").isJsonNull() ? pObj.get("username").getAsString() : "Unknown";
|
||||
int pos = pObj.has("position") && !pObj.get("position").isJsonNull() ? pObj.get("position").getAsInt() : (i + 1);
|
||||
|
||||
modeTopNames.put(pos, name);
|
||||
|
||||
String tier = plugin.getConfig().getString("not-found-text", "Unranked");
|
||||
if (pObj.has("tiers") && !pObj.get("tiers").isJsonNull()) {
|
||||
JsonObject tiersObj = pObj.getAsJsonObject("tiers");
|
||||
if (tiersObj.has(mode) && !tiersObj.get(mode).isJsonNull()) {
|
||||
tier = tiersObj.get(mode).getAsString();
|
||||
}
|
||||
}
|
||||
modeTopTiers.put(pos, tier);
|
||||
}
|
||||
leaderboardCache.put(mode.toLowerCase(), modeTopNames);
|
||||
leaderboardTierCache.put(mode.toLowerCase(), modeTopTiers);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("Failed to fetch leaderboard for " + mode + ": " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
playerTierCache.put(playerName.toLowerCase(), pTiers);
|
||||
|
||||
if (json.has("position") && !json.get("position").isJsonNull()) {
|
||||
playerRankCache.put(playerName.toLowerCase(), json.get("position").getAsInt());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public String getPlayerTier(String playerName, String mode) {
|
||||
String pLow = playerName.toLowerCase();
|
||||
if (!playerTierCache.containsKey(pLow)) {
|
||||
fetchPlayerAsync(playerName);
|
||||
return plugin.getConfig().getString("not-found-text", "Unranked");
|
||||
}
|
||||
Map<String, String> tiers = playerTierCache.get(pLow);
|
||||
return tiers.getOrDefault(mode.toLowerCase(), plugin.getConfig().getString("not-found-text", "Unranked"));
|
||||
}
|
||||
|
||||
public String getPlayerRank(String playerName) {
|
||||
String pLow = playerName.toLowerCase();
|
||||
if (!playerRankCache.containsKey(pLow)) {
|
||||
fetchPlayerAsync(playerName);
|
||||
return plugin.getConfig().getString("not-found-text", "Unranked");
|
||||
}
|
||||
return String.valueOf(playerRankCache.get(pLow));
|
||||
}
|
||||
|
||||
public String getTopPlayerName(String mode, int position) {
|
||||
Map<Integer, String> modeCache = leaderboardCache.get(mode.toLowerCase());
|
||||
if (modeCache == null || !modeCache.containsKey(position)) {
|
||||
return plugin.getConfig().getString("not-found-text", "None");
|
||||
}
|
||||
return modeCache.get(position);
|
||||
}
|
||||
|
||||
public String getTopPlayerTier(String mode, int position) {
|
||||
Map<Integer, String> modeCache = leaderboardTierCache.get(mode.toLowerCase());
|
||||
if (modeCache == null || !modeCache.containsKey(position)) {
|
||||
return plugin.getConfig().getString("not-found-text", "Unranked");
|
||||
}
|
||||
return modeCache.get(position);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package dev.bailey.etplaceholders;
|
||||
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class ETExpansion extends PlaceholderExpansion {
|
||||
|
||||
private final ETPlaceholdersPlugin plugin;
|
||||
|
||||
public ETExpansion(ETPlaceholdersPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getIdentifier() {
|
||||
return "et";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getAuthor() {
|
||||
return "Bailey";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onPlaceholderRequest(Player player, String identifier) {
|
||||
if (identifier.equalsIgnoreCase("rank")) {
|
||||
if (player == null) return "";
|
||||
return plugin.getDataManager().getPlayerRank(player.getName());
|
||||
}
|
||||
|
||||
if (identifier.toLowerCase().startsWith("tier_")) {
|
||||
if (player == null) return "";
|
||||
String mode = identifier.substring(5);
|
||||
return plugin.getDataManager().getPlayerTier(player.getName(), mode);
|
||||
}
|
||||
|
||||
if (identifier.toLowerCase().startsWith("top_name_")) {
|
||||
String[] parts = identifier.split("_");
|
||||
if (parts.length == 4) {
|
||||
String mode = parts[2];
|
||||
try {
|
||||
int pos = Integer.parseInt(parts[3]);
|
||||
return plugin.getDataManager().getTopPlayerName(mode, pos);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (identifier.toLowerCase().startsWith("top_tier_")) {
|
||||
String[] parts = identifier.split("_");
|
||||
if (parts.length == 4) {
|
||||
String mode = parts[2];
|
||||
try {
|
||||
int pos = Integer.parseInt(parts[3]);
|
||||
return plugin.getDataManager().getTopPlayerTier(mode, pos);
|
||||
} catch (NumberFormatException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package dev.bailey.etplaceholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class ETPlaceholdersPlugin extends JavaPlugin {
|
||||
|
||||
private DataManager dataManager;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
|
||||
dataManager = new DataManager(this);
|
||||
dataManager.startTasks();
|
||||
|
||||
if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
|
||||
new ETExpansion(this).register();
|
||||
getLogger().info("ETPlaceholders enabled and hooked into PlaceholderAPI.");
|
||||
} else {
|
||||
getLogger().warning("PlaceholderAPI not found! Placeholders will not work.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Bukkit.getScheduler().cancelTasks(this);
|
||||
}
|
||||
|
||||
public DataManager getDataManager() {
|
||||
return dataManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
# ET Placeholders Configuration
|
||||
# Available Gamemodes: overall, vanilla, mace, axe, sword, smp, diamondsmp, uhc, pot, nethop, cart
|
||||
|
||||
# The base URL of the EaglerTiers API
|
||||
api-url: "https://api.eaglertiers.com/api"
|
||||
|
||||
# Your API key. While public read requests don't require an API key, it is highly recommended to provide one to avoid rate limits.
|
||||
api-key: ""
|
||||
|
||||
# How often (in minutes) to refresh cached player and leaderboard data.
|
||||
# Do not set this too low or you may hit the API rate limit (120 req/min).
|
||||
cache-refresh-minutes: 15
|
||||
|
||||
# Value to display when a tier or player is not found
|
||||
not-found-text: "Unranked"
|
||||
@@ -0,0 +1,7 @@
|
||||
name: ETPlaceholders
|
||||
version: 1.0.0
|
||||
main: dev.bailey.etplaceholders.ETPlaceholdersPlugin
|
||||
api-version: "1.13"
|
||||
softdepend: [PlaceholderAPI]
|
||||
description: PlaceholderAPI expansion for EaglerTiers
|
||||
author: Bailey
|
||||
Reference in New Issue
Block a user