From 0bcf4aa7ecd4881d8236ae3cf6862acbf2f715eb Mon Sep 17 00:00:00 2001 From: RednedEpic Date: Sat, 21 Sep 2019 11:00:13 -0500 Subject: [PATCH] Fix metrics, remove JSONSimple dependency --- connector/pom.xml | 7 - .../geysermc/connector/GeyserConnector.java | 20 ++- .../configuration/BedrockConfiguration.java | 16 -- .../configuration/GeyserConfiguration.java | 3 +- .../connector/configuration/MetricInfo.java | 38 +++++ .../geysermc/connector/metrics/Metrics.java | 147 +++++++++--------- connector/src/main/resources/config.yml | 14 +- 7 files changed, 139 insertions(+), 106 deletions(-) create mode 100644 connector/src/main/java/org/geysermc/connector/configuration/MetricInfo.java diff --git a/connector/pom.xml b/connector/pom.xml index f3d452a9..cea7aebf 100644 --- a/connector/pom.xml +++ b/connector/pom.xml @@ -101,13 +101,6 @@ 1.1-SNAPSHOT compile - - - com.googlecode.json-simple - json-simple - 1.1.1 - compile - com.github.steveice10 mcprotocollib diff --git a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java index 38dff857..f7e252a3 100644 --- a/connector/src/main/java/org/geysermc/connector/GeyserConnector.java +++ b/connector/src/main/java/org/geysermc/connector/GeyserConnector.java @@ -54,7 +54,11 @@ import java.io.File; import java.io.IOException; import java.net.InetSocketAddress; import java.text.DecimalFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; @@ -64,8 +68,8 @@ public class GeyserConnector implements Connector { public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v361.V361_CODEC; - private static final String NAME = "Geyser"; - private static final String VERSION = "1.0-SNAPSHOT"; + public static final String NAME = "Geyser"; + public static final String VERSION = "1.0-SNAPSHOT"; private final Map players = new HashMap<>(); @@ -114,7 +118,7 @@ public class GeyserConnector implements Connector { logger.info("******************************************"); try { - File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("UUIDTESTUUIDTEST", UUID.randomUUID().toString())); + File configFile = FileUtils.fileOrCopiedFromResource("config.yml", (x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString())); config = FileUtils.loadConfig(configFile, GeyserConfiguration.class); } catch (IOException ex) { @@ -151,9 +155,11 @@ public class GeyserConnector implements Connector { } }).join(); - metrics = new Metrics("GeyserMC", config.getUUID(), true, java.util.logging.Logger.getLogger("")); - metrics.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1)); - metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); + if (config.getMetrics().isEnabled()) { + metrics = new Metrics("GeyserMC", config.getMetrics().getUUID(), true, java.util.logging.Logger.getLogger("")); + metrics.addCustomChart(new Metrics.SingleLineChart("servers", () -> 1)); + metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); + } double completeTime = (System.currentTimeMillis() - startupTime) / 1000D; logger.info(String.format("Done (%ss)! Run /help for help!", new DecimalFormat("#.###").format(completeTime))); diff --git a/connector/src/main/java/org/geysermc/connector/configuration/BedrockConfiguration.java b/connector/src/main/java/org/geysermc/connector/configuration/BedrockConfiguration.java index 56935c2c..2010214e 100644 --- a/connector/src/main/java/org/geysermc/connector/configuration/BedrockConfiguration.java +++ b/connector/src/main/java/org/geysermc/connector/configuration/BedrockConfiguration.java @@ -35,20 +35,4 @@ public class BedrockConfiguration { private String motd1; private String motd2; - - public String getAddress() { - return address; - } - - public int getPort() { - return port; - } - - public String getMotd1() { - return motd1; - } - - public String getMotd2() { - return motd2; - } } \ No newline at end of file diff --git a/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java b/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java index 0c84ad8b..d4013744 100644 --- a/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java +++ b/connector/src/main/java/org/geysermc/connector/configuration/GeyserConfiguration.java @@ -47,6 +47,5 @@ public class GeyserConfiguration { @JsonProperty("debug-mode") private boolean debugMode; - @JsonProperty("uuid") - private String UUID; + private MetricInfo metrics; } \ No newline at end of file diff --git a/connector/src/main/java/org/geysermc/connector/configuration/MetricInfo.java b/connector/src/main/java/org/geysermc/connector/configuration/MetricInfo.java new file mode 100644 index 00000000..d108d4b4 --- /dev/null +++ b/connector/src/main/java/org/geysermc/connector/configuration/MetricInfo.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2019 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.connector.configuration; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Getter; + +@Getter +public class MetricInfo { + + private boolean enabled; + + @JsonProperty("uuid") + private String UUID; +} diff --git a/connector/src/main/java/org/geysermc/connector/metrics/Metrics.java b/connector/src/main/java/org/geysermc/connector/metrics/Metrics.java index 424de9f8..e0cd6baa 100644 --- a/connector/src/main/java/org/geysermc/connector/metrics/Metrics.java +++ b/connector/src/main/java/org/geysermc/connector/metrics/Metrics.java @@ -1,8 +1,9 @@ package org.geysermc.connector.metrics; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import org.geysermc.api.Geyser; -import org.json.simple.JSONArray; -import org.json.simple.JSONObject; +import org.geysermc.connector.GeyserConnector; import javax.net.ssl.HttpsURLConnection; import java.io.ByteArrayOutputStream; @@ -12,8 +13,6 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -93,20 +92,22 @@ public class Metrics { * * @return The plugin specific data. */ - private JSONObject getPluginData() { - JSONObject data = new JSONObject(); + private JsonObject getPluginData() { + JsonObject data = new JsonObject(); - data.put("pluginName", name); // Append the name of the server software - JSONArray customCharts = new JSONArray(); + data.addProperty("pluginName", name); // Append the name of the server software + data.addProperty("pluginVersion", GeyserConnector.VERSION); // Append the name of the server software + + JsonArray customCharts = new JsonArray(); for (CustomChart customChart : charts) { // Add the data of the custom charts - JSONObject chart = customChart.getRequestJsonObject(); + JsonObject chart = customChart.getRequestJsonObject(); if (chart == null) { // If the chart is null, we skip it continue; } customCharts.add(chart); } - data.put("customCharts", customCharts); + data.add("customCharts", customCharts); return data; } @@ -116,21 +117,24 @@ public class Metrics { * * @return The server specific data. */ - private JSONObject getServerData() { + private JsonObject getServerData() { // OS specific data + int playerAmount = Geyser.getPlayerCount(); + String osName = System.getProperty("os.name"); String osArch = System.getProperty("os.arch"); String osVersion = System.getProperty("os.version"); int coreCount = Runtime.getRuntime().availableProcessors(); - JSONObject data = new JSONObject(); + JsonObject data = new JsonObject(); - data.put("serverUUID", serverUUID); + data.addProperty("serverUUID", serverUUID); - data.put("osName", osName); - data.put("osArch", osArch); - data.put("osVersion", osVersion); - data.put("coreCount", coreCount); + data.addProperty("playerAmount", playerAmount); + data.addProperty("osName", osName); + data.addProperty("osArch", osArch); + data.addProperty("osVersion", osVersion); + data.addProperty("coreCount", coreCount); return data; } @@ -139,21 +143,23 @@ public class Metrics { * Collects the data and sends it afterwards. */ private void submitData() { - final JSONObject data = getServerData(); + final JsonObject data = getServerData(); - JSONArray pluginData = new JSONArray(); + JsonArray pluginData = new JsonArray(); pluginData.add(getPluginData()); - data.put("plugins", pluginData); + data.add("plugins", pluginData); - try { - // We are still in the Thread of the timer, so nothing get blocked :) - sendData(data); - } catch (Exception e) { - // Something went wrong! :( - if (logFailedRequests) { - logger.log(Level.WARNING, "Could not submit stats of " + name, e); + new Thread(() -> { + try { + // We are still in the Thread of the timer, so nothing get blocked :) + sendData(data); + } catch (Exception e) { + // Something went wrong! :( + if (logFailedRequests) { + logger.log(Level.WARNING, "Could not submit stats of " + name, e); + } } - } + }).start(); } /** @@ -162,7 +168,7 @@ public class Metrics { * @param data The data to send. * @throws Exception If the request failed. */ - private static void sendData(JSONObject data) throws Exception { + private static void sendData(JsonObject data) throws Exception { if (data == null) { throw new IllegalArgumentException("Data cannot be null!"); } @@ -228,16 +234,16 @@ public class Metrics { this.chartId = chartId; } - private JSONObject getRequestJsonObject() { - JSONObject chart = new JSONObject(); - chart.put("chartId", chartId); + private JsonObject getRequestJsonObject() { + JsonObject chart = new JsonObject(); + chart.addProperty("chartId", chartId); try { - JSONObject data = getChartData(); + JsonObject data = getChartData(); if (data == null) { // If the data is null we don't send the chart. return null; } - chart.put("data", data); + chart.add("data", data); } catch (Throwable t) { if (logFailedRequests) { logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); @@ -247,7 +253,7 @@ public class Metrics { return chart; } - protected abstract JSONObject getChartData() throws Exception; + protected abstract JsonObject getChartData() throws Exception; } @@ -270,14 +276,14 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); String value = callable.call(); if (value == null || value.isEmpty()) { // Null = skip the chart return null; } - data.put("value", value); + data.addProperty("value", value); return data; } } @@ -301,9 +307,9 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); Map map = callable.call(); if (map == null || map.isEmpty()) { // Null = skip the chart @@ -315,13 +321,13 @@ public class Metrics { continue; // Skip this invalid } allSkipped = false; - values.put(entry.getKey(), entry.getValue()); + values.addProperty(entry.getKey(), entry.getValue()); } if (allSkipped) { // Null = skip the chart return null; } - data.put("values", values); + data.add("values", values); return data; } } @@ -345,9 +351,9 @@ public class Metrics { } @Override - public JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); + public JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); Map> map = callable.call(); if (map == null || map.isEmpty()) { // Null = skip the chart @@ -355,22 +361,22 @@ public class Metrics { } boolean reallyAllSkipped = true; for (Map.Entry> entryValues : map.entrySet()) { - JSONObject value = new JSONObject(); + JsonObject value = new JsonObject(); boolean allSkipped = true; for (Map.Entry valueEntry : map.get(entryValues.getKey()).entrySet()) { - value.put(valueEntry.getKey(), valueEntry.getValue()); + value.addProperty(valueEntry.getKey(), valueEntry.getValue()); allSkipped = false; } if (!allSkipped) { reallyAllSkipped = false; - values.put(entryValues.getKey(), value); + values.add(entryValues.getKey(), value); } } if (reallyAllSkipped) { // Null = skip the chart return null; } - data.put("values", values); + data.add("values", values); return data; } } @@ -394,14 +400,14 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); int value = callable.call(); if (value == 0) { // Null = skip the chart return null; } - data.put("value", value); + data.addProperty("value", value); return data; } @@ -426,9 +432,9 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); Map map = callable.call(); if (map == null || map.isEmpty()) { // Null = skip the chart @@ -440,13 +446,13 @@ public class Metrics { continue; // Skip this invalid } allSkipped = false; - values.put(entry.getKey(), entry.getValue()); + values.addProperty(entry.getKey(), entry.getValue()); } if (allSkipped) { // Null = skip the chart return null; } - data.put("values", values); + data.add("values", values); return data; } @@ -471,20 +477,20 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); Map map = callable.call(); if (map == null || map.isEmpty()) { // Null = skip the chart return null; } for (Map.Entry entry : map.entrySet()) { - JSONArray categoryValues = new JSONArray(); + JsonArray categoryValues = new JsonArray(); categoryValues.add(entry.getValue()); - values.put(entry.getKey(), categoryValues); + values.add(entry.getKey(), categoryValues); } - data.put("values", values); + data.add("values", values); return data; } @@ -509,9 +515,9 @@ public class Metrics { } @Override - protected JSONObject getChartData() throws Exception { - JSONObject data = new JSONObject(); - JSONObject values = new JSONObject(); + protected JsonObject getChartData() throws Exception { + JsonObject data = new JsonObject(); + JsonObject values = new JsonObject(); Map map = callable.call(); if (map == null || map.isEmpty()) { // Null = skip the chart @@ -523,20 +529,19 @@ public class Metrics { continue; // Skip this invalid } allSkipped = false; - JSONArray categoryValues = new JSONArray(); + JsonArray categoryValues = new JsonArray(); for (int categoryValue : entry.getValue()) { categoryValues.add(categoryValue); } - values.put(entry.getKey(), categoryValues); + values.add(entry.getKey(), categoryValues); } if (allSkipped) { // Null = skip the chart return null; } - data.put("values", values); + data.add("values", values); return data; } } - } \ No newline at end of file diff --git a/connector/src/main/resources/config.yml b/connector/src/main/resources/config.yml index ff021488..8506b14c 100644 --- a/connector/src/main/resources/config.yml +++ b/connector/src/main/resources/config.yml @@ -3,7 +3,8 @@ # # A bridge between Minecraft: Bedrock Edition and Minecraft: Java Edition. # -# https://github.com/GeyserMC/Geyser +# GitHub: https://github.com/GeyserMC/Geyser +# Discord: https://discord.geysermc.org/ # -------------------------------- bedrock: @@ -45,8 +46,15 @@ max-players: 100 # If debug messages should be sent through console debug-mode: false -# UUID: DON'T CHANGE! -uuid: UUIDTESTUUIDTEST +# bStats is a stat tracker that is entirely anonymous and tracks only basic information +# about Geyser, such as how many people are online, how many servers are using Geyser, +# what OS is being used, etc. You can learn more about bStats here: https://bstats.org/. +# https://bstats.org/plugin/server-implementation/GeyserMC +metrics: + # If metrics should be enabled + enabled: true + # UUID of server, don't change! + uuid: generateduuid