fix: Fix everything
This commit is contained in:
parent
b8ad14d8e1
commit
182736b605
2 changed files with 22 additions and 16 deletions
1
pom.xml
1
pom.xml
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue