diff --git a/pom.xml b/pom.xml index ee6c78a..602d069 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,6 @@ 11 UTF-8 - 1.21.1-R0.1-SNAPSHOT diff --git a/src/main/java/eu/m724/mstats/MStats.java b/src/main/java/eu/m724/mstats/MStats.java index edf0191..204c14b 100644 --- a/src/main/java/eu/m724/mstats/MStats.java +++ b/src/main/java/eu/m724/mstats/MStats.java @@ -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; + } }