fix: Fix everything

This commit is contained in:
Minecon724 2025-02-16 20:23:23 +01:00
parent b8ad14d8e1
commit 182736b605
Signed by untrusted user who does not match committer: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 22 additions and 16 deletions

View file

@ -28,7 +28,6 @@
<properties>
<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>
<repositories>

View file

@ -2,6 +2,7 @@ package eu.m724.mstats;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
@ -26,6 +27,8 @@ public class MStats extends BukkitRunnable {
private final Logger logger = Logger.getLogger("mStats");
private String token = "";
private HttpClient httpClient;
public MStats(MStatsPlugin plugin) {
this.plugin = plugin;
this.server = plugin.getMStatsServer();
@ -79,6 +82,10 @@ public class MStats extends BukkitRunnable {
return;
}
this.httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
// basically "PluginName #21, AnotherPlugin* #32" - (plugin name) + (* if stats runs on the plugin) + (# plugin id) joined with ", "
logger.info(server.getHost() + " " +
registeredPlugins.entrySet().stream().map(
@ -113,15 +120,12 @@ public class MStats extends BukkitRunnable {
.header("X-Server-Token", token)
.build();
HttpClient client = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
JsonObject jsonObject = new Gson().fromJson(resp.body(), JsonObject.class);
client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
JsonObject response = new Gson().fromJson(resp.body(), JsonObject.class);
if (response.has("message")) {
logger.info("Message from mStats server: " + response.get("message").getAsString());
JsonElement message = getIfJsonPrimitive(jsonObject, "message");
if (message != null) {
logger.info("Message from mStats server: " + message.getAsString());
}
if (resp.statusCode() != 200) {
@ -130,11 +134,13 @@ public class MStats extends BukkitRunnable {
return;
}
if (response.has("token")) {
this.token = response.get("token").getAsString();
JsonElement token = getIfJsonPrimitive(jsonObject, "token");
if (token != null) {
this.token = token.getAsString();
}
if (response.has("version")) {
JsonElement version = getIfJsonPrimitive(jsonObject, "token");
if (version != null) {
// TODO
}
}).exceptionally(e -> {
@ -150,10 +156,11 @@ public class MStats extends BukkitRunnable {
.header("X-Server-Token", token)
.build();
HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();
httpClient.sendAsync(request, HttpResponse.BodyHandlers.discarding());
}
private JsonElement getIfJsonPrimitive(JsonObject jsonObject, String memberName) {
JsonElement element = jsonObject.get(memberName);
return element != null && element.isJsonPrimitive() ? element : null;
}
}