This commit is contained in:
Minecon724 2025-01-05 11:10:55 +01:00
parent 07ab4ad929
commit 62b4f91c5f
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
5 changed files with 88 additions and 44 deletions

View file

@ -8,7 +8,7 @@
</list> </list>
</option> </option>
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="temurin-11" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View file

@ -14,15 +14,16 @@ libraries:
- eu.m724:mstats-spigot:0.1.0 - eu.m724:mstats-spigot:0.1.0
``` ```
In main class: In `JavaPlugin` class:
```java ```java
import eu.m724.mstats.MStatsPlugin; import eu.m724.mstats.MStatsPlugin;
public class MyPlugin extends MStatsPlugin { public class MyPlugin extends MStatsPlugin { // instead of JavaPlugin
@Override @Override
public void onEnable() { 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 mStats(1); // Replace 1 of course with your plugin ID
} }
} }

View file

@ -26,8 +26,7 @@
<description>mStats client for Spigot</description> <description>mStats client for Spigot</description>
<properties> <properties>
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.release>11</maven.compiler.release>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.spigot.version>1.21.1-R0.1-SNAPSHOT</project.spigot.version> <project.spigot.version>1.21.1-R0.1-SNAPSHOT</project.spigot.version>
</properties> </properties>
@ -47,7 +46,7 @@
<dependency> <dependency>
<groupId>org.spigotmc</groupId> <groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId> <artifactId>spigot-api</artifactId>
<version>1.21.1-R0.1-SNAPSHOT</version> <version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>

View file

@ -3,7 +3,6 @@ package eu.m724.mstats;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -20,10 +19,10 @@ import java.util.stream.Collectors;
public class MStats extends BukkitRunnable { public class MStats extends BukkitRunnable {
static final int PROT_VERSION = 1; static final int PROT_VERSION = 1;
private final Plugin plugin; private final MStatsPlugin plugin;
private final URI server; private final URI server;
private final Map<Plugin, Integer> registeredPlugins = new HashMap<>(); private final Map<Plugin, Integer[]> registeredPlugins = new HashMap<>();
private final Logger logger = Logger.getLogger("mStats"); private final Logger logger = Logger.getLogger("mStats");
private String token = ""; private String token = "";
@ -56,8 +55,11 @@ public class MStats extends BukkitRunnable {
method = plugin.getClass().getMethod("getMStatsServer"); method = plugin.getClass().getMethod("getMStatsServer");
URI server = (URI) method.invoke(plugin); URI server = (URI) method.invoke(plugin);
method = plugin.getClass().getMethod("getMStatsVersion");
int version = (int) method.invoke(plugin);
if (id > 0 && server.equals(this.server)) if (id > 0 && server.equals(this.server))
registeredPlugins.put(plugin, id); registeredPlugins.put(plugin, new Integer[] { id, version });
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
// plugin is not a mstats plugin // plugin is not a mstats plugin
} catch (InvocationTargetException | IllegalAccessException e) { } 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); //logger.info("I pass control to " + first);
return; return;
} }
@ -77,8 +83,8 @@ public class MStats extends BukkitRunnable {
logger.info(server.getHost() + " " + logger.info(server.getHost() + " " +
registeredPlugins.entrySet().stream().map( registeredPlugins.entrySet().stream().map(
e -> e.getKey().getName() e -> e.getKey().getName()
+ (first.equals(e.getKey().getName()) ? "*" : "") + (e.getValue()[0] == first ? "*" : "")
+ " #" + e.getValue()) + " #" + e.getValue()[0])
.collect(Collectors.joining(", ") .collect(Collectors.joining(", ")
) )
); );
@ -90,9 +96,9 @@ public class MStats extends BukkitRunnable {
public void run() { public void run() {
var json = new JsonObject(); var json = new JsonObject();
var pla = new JsonArray(); var pla = new JsonArray();
registeredPlugins.forEach((p, id) -> { registeredPlugins.forEach((p, v) -> {
var o1 = new JsonObject(); var o1 = new JsonObject();
o1.addProperty("id", id); o1.addProperty("id", v[0]);
o1.addProperty("version", p.getDescription().getVersion()); o1.addProperty("version", p.getDescription().getVersion());
pla.add(o1); pla.add(o1);
}); });
@ -102,14 +108,17 @@ public class MStats extends BukkitRunnable {
json.addProperty("statsVersion", PROT_VERSION); json.addProperty("statsVersion", PROT_VERSION);
HttpRequest request = HttpRequest HttpRequest request = HttpRequest
.newBuilder(server) .newBuilder(server.resolve("/api/server/heartbeat"))
.POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(json))) .POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(json)))
.header("X-Server-Token", token) .header("X-Server-Token", token)
.build(); .build();
try (HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build()) { HttpClient client = HttpClient.newBuilder()
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> { .followRedirects(HttpClient.Redirect.NEVER)
JsonObject response = JsonParser.parseString(resp.body()).getAsJsonObject(); .build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
JsonObject response = new Gson().fromJson(resp.body(), JsonObject.class);
if (response.has("message")) { if (response.has("message")) {
logger.info("Message from mStats server: " + response.get("message").getAsString()); logger.info("Message from mStats server: " + response.get("message").getAsString());
@ -133,5 +142,18 @@ public class MStats extends BukkitRunnable {
return null; 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());
} }
} }

View file

@ -6,7 +6,8 @@ import java.net.URI;
public class MStatsPlugin extends JavaPlugin { public class MStatsPlugin extends JavaPlugin {
private int id = -1; 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<br> * Enable mStats for this plugin<br>
@ -16,23 +17,44 @@ public class MStatsPlugin extends JavaPlugin {
*/ */
public void mStats(int id) { public void mStats(int id) {
this.id = 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<br>
* This must be done before {@link MStatsPlugin#mStats(int)}
* *
* @param server the server URL * @param server the server URL
*/ */
public void mStatsServer(String server) { 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() { public int getMStatsId() {
return id; return id;
} }
/**
*
* @return the mStats server
*/
public URI getMStatsServer() { public URI getMStatsServer() {
return server; return server;
} }
public int getMStatsVersion() {
return 1;
}
} }