From 62b4f91c5f4c0abb2b6d844de8019b94cae19e8d Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 5 Jan 2025 11:10:55 +0100 Subject: [PATCH] update --- .idea/misc.xml | 2 +- README.md | 7 +- pom.xml | 5 +- src/main/java/eu/m724/mstats/MStats.java | 88 ++++++++++++------- .../java/eu/m724/mstats/MStatsPlugin.java | 30 ++++++- 5 files changed, 88 insertions(+), 44 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 7ace097..ca206d5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/README.md b/README.md index 1c0e434..94dff1a 100644 --- a/README.md +++ b/README.md @@ -14,15 +14,16 @@ libraries: - eu.m724:mstats-spigot:0.1.0 ``` -In main class: +In `JavaPlugin` class: ```java import eu.m724.mstats.MStatsPlugin; -public class MyPlugin extends MStatsPlugin { +public class MyPlugin extends MStatsPlugin { // instead of JavaPlugin @Override public void onEnable() { - // It's recommended that this is the last line of onEnable + // ... your onEnable code ... + mStats(1); // Replace 1 of course with your plugin ID } } diff --git a/pom.xml b/pom.xml index 07b2fcd..568f037 100644 --- a/pom.xml +++ b/pom.xml @@ -26,8 +26,7 @@ mStats client for Spigot - 21 - 21 + 11 UTF-8 1.21.1-R0.1-SNAPSHOT @@ -47,7 +46,7 @@ org.spigotmc spigot-api - 1.21.1-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided diff --git a/src/main/java/eu/m724/mstats/MStats.java b/src/main/java/eu/m724/mstats/MStats.java index e70ff1e..edf0191 100644 --- a/src/main/java/eu/m724/mstats/MStats.java +++ b/src/main/java/eu/m724/mstats/MStats.java @@ -3,7 +3,6 @@ package eu.m724.mstats; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.google.gson.JsonParser; import org.bukkit.plugin.Plugin; import org.bukkit.scheduler.BukkitRunnable; @@ -20,10 +19,10 @@ import java.util.stream.Collectors; public class MStats extends BukkitRunnable { static final int PROT_VERSION = 1; - private final Plugin plugin; + private final MStatsPlugin plugin; private final URI server; - private final Map registeredPlugins = new HashMap<>(); + private final Map registeredPlugins = new HashMap<>(); private final Logger logger = Logger.getLogger("mStats"); private String token = ""; @@ -56,8 +55,11 @@ public class MStats extends BukkitRunnable { method = plugin.getClass().getMethod("getMStatsServer"); URI server = (URI) method.invoke(plugin); + method = plugin.getClass().getMethod("getMStatsVersion"); + int version = (int) method.invoke(plugin); + if (id > 0 && server.equals(this.server)) - registeredPlugins.put(plugin, id); + registeredPlugins.put(plugin, new Integer[] { id, version }); } catch (NoSuchMethodException e) { // plugin is not a mstats plugin } catch (InvocationTargetException | IllegalAccessException e) { @@ -66,9 +68,13 @@ public class MStats extends BukkitRunnable { } } - String first = registeredPlugins.keySet().stream().map(Plugin::getName).sorted().findFirst().get(); + int maxVersion = registeredPlugins.values().stream().mapToInt(integers -> integers[1]).max().getAsInt(); - if (!plugin.getName().equals(first)) { + int first = registeredPlugins.values().stream() + .filter(integers -> integers[1] == maxVersion) + .mapToInt(integers -> integers[0]).min().getAsInt(); + + if (plugin.getMStatsId() != first) { //logger.info("I pass control to " + first); return; } @@ -77,8 +83,8 @@ public class MStats extends BukkitRunnable { logger.info(server.getHost() + " " + registeredPlugins.entrySet().stream().map( e -> e.getKey().getName() - + (first.equals(e.getKey().getName()) ? "*" : "") - + " #" + e.getValue()) + + (e.getValue()[0] == first ? "*" : "") + + " #" + e.getValue()[0]) .collect(Collectors.joining(", ") ) ); @@ -90,9 +96,9 @@ public class MStats extends BukkitRunnable { public void run() { var json = new JsonObject(); var pla = new JsonArray(); - registeredPlugins.forEach((p, id) -> { + registeredPlugins.forEach((p, v) -> { var o1 = new JsonObject(); - o1.addProperty("id", id); + o1.addProperty("id", v[0]); o1.addProperty("version", p.getDescription().getVersion()); pla.add(o1); }); @@ -102,36 +108,52 @@ public class MStats extends BukkitRunnable { json.addProperty("statsVersion", PROT_VERSION); HttpRequest request = HttpRequest - .newBuilder(server) + .newBuilder(server.resolve("/api/server/heartbeat")) .POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(json))) .header("X-Server-Token", token) .build(); - try (HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build()) { - httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> { - JsonObject response = JsonParser.parseString(resp.body()).getAsJsonObject(); + HttpClient client = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.NEVER) + .build(); - if (response.has("message")) { - logger.info("Message from mStats server: " + response.get("message").getAsString()); - } + client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> { + JsonObject response = new Gson().fromJson(resp.body(), JsonObject.class); - if (resp.statusCode() != 200) { - logger.severe("Unable to contact mStats. It's not known why. " + resp.statusCode()); - //logger.severe(resp.body()); - return; - } + if (response.has("message")) { + logger.info("Message from mStats server: " + response.get("message").getAsString()); + } - if (response.has("token")) { - this.token = response.get("token").getAsString(); - } + if (resp.statusCode() != 200) { + logger.severe("Unable to contact mStats. It's not known why. " + resp.statusCode()); + //logger.severe(resp.body()); + return; + } - if (response.has("version")) { - // TODO - } - }).exceptionally(e -> { - logger.severe("Unable to contact mStats. Error. " + e.getMessage()); - return null; - }); - } + if (response.has("token")) { + this.token = response.get("token").getAsString(); + } + + if (response.has("version")) { + // TODO + } + }).exceptionally(e -> { + logger.severe("Unable to contact mStats. Error. " + e.getMessage()); + return null; + }); + } + + public void stop() { + HttpRequest request = HttpRequest + .newBuilder(server.resolve("/api/server/remove")) + .POST(HttpRequest.BodyPublishers.noBody()) + .header("X-Server-Token", token) + .build(); + + HttpClient httpClient = HttpClient.newBuilder() + .followRedirects(HttpClient.Redirect.NEVER) + .build(); + + httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding()); } } diff --git a/src/main/java/eu/m724/mstats/MStatsPlugin.java b/src/main/java/eu/m724/mstats/MStatsPlugin.java index 7f21c20..efa839b 100644 --- a/src/main/java/eu/m724/mstats/MStatsPlugin.java +++ b/src/main/java/eu/m724/mstats/MStatsPlugin.java @@ -6,7 +6,8 @@ import java.net.URI; public class MStatsPlugin extends JavaPlugin { private int id = -1; - private URI server = URI.create("https://mstats.m724.eu/api/server/heartbeat"); + private URI server = URI.create("https://mstats.m724.eu"); + private MStats mStats; /** * Enable mStats for this plugin
@@ -16,23 +17,44 @@ public class MStatsPlugin extends JavaPlugin { */ public void mStats(int id) { this.id = id; - new MStats(this).init(); + this.mStats = new MStats(this); + mStats.init(); } /** - * Set the mStats backend server + * Set the mStats backend server
+ * This must be done before {@link MStatsPlugin#mStats(int)} * * @param server the server URL */ public void mStatsServer(String server) { - this.server = URI.create(server + "/api/server/heartbeat"); + this.server = URI.create(server); } + /** + * Tells mStats to remove this server + */ + public void mStatsDisable() { + mStats.stop(); + } + + /** + * + * @return the mStats plugin ID + */ public int getMStatsId() { return id; } + /** + * + * @return the mStats server + */ public URI getMStatsServer() { return server; } + + public int getMStatsVersion() { + return 1; + } }