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