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>
</option>
</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" />
</component>
</project>

View file

@ -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
}
}

View file

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

View file

@ -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<Plugin, Integer> registeredPlugins = new HashMap<>();
private final Map<Plugin, Integer[]> 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());
}
}

View file

@ -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<br>
@ -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<br>
* 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;
}
}