Fix metrics, remove JSONSimple dependency

This commit is contained in:
RednedEpic 2019-09-21 11:00:13 -05:00 committed by RednedEpic
parent 2a0d7eb73b
commit 0bcf4aa7ec
7 changed files with 139 additions and 106 deletions

View file

@ -101,13 +101,6 @@
<version>1.1-SNAPSHOT</version> <version>1.1-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
<scope>compile</scope>
</dependency>
<dependency> <dependency>
<groupId>com.github.steveice10</groupId> <groupId>com.github.steveice10</groupId>
<artifactId>mcprotocollib</artifactId> <artifactId>mcprotocollib</artifactId>

View file

@ -54,7 +54,11 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.text.DecimalFormat; 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.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; 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; public static final BedrockPacketCodec BEDROCK_PACKET_CODEC = Bedrock_v361.V361_CODEC;
private static final String NAME = "Geyser"; public static final String NAME = "Geyser";
private static final String VERSION = "1.0-SNAPSHOT"; public static final String VERSION = "1.0-SNAPSHOT";
private final Map<Object, Player> players = new HashMap<>(); private final Map<Object, Player> players = new HashMap<>();
@ -114,7 +118,7 @@ public class GeyserConnector implements Connector {
logger.info("******************************************"); logger.info("******************************************");
try { 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); config = FileUtils.loadConfig(configFile, GeyserConfiguration.class);
} catch (IOException ex) { } catch (IOException ex) {
@ -151,9 +155,11 @@ public class GeyserConnector implements Connector {
} }
}).join(); }).join();
metrics = new Metrics("GeyserMC", config.getUUID(), true, java.util.logging.Logger.getLogger("")); 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("servers", () -> 1));
metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount)); metrics.addCustomChart(new Metrics.SingleLineChart("players", Geyser::getPlayerCount));
}
double completeTime = (System.currentTimeMillis() - startupTime) / 1000D; double completeTime = (System.currentTimeMillis() - startupTime) / 1000D;
logger.info(String.format("Done (%ss)! Run /help for help!", new DecimalFormat("#.###").format(completeTime))); logger.info(String.format("Done (%ss)! Run /help for help!", new DecimalFormat("#.###").format(completeTime)));

View file

@ -35,20 +35,4 @@ public class BedrockConfiguration {
private String motd1; private String motd1;
private String motd2; private String motd2;
public String getAddress() {
return address;
}
public int getPort() {
return port;
}
public String getMotd1() {
return motd1;
}
public String getMotd2() {
return motd2;
}
} }

View file

@ -47,6 +47,5 @@ public class GeyserConfiguration {
@JsonProperty("debug-mode") @JsonProperty("debug-mode")
private boolean debugMode; private boolean debugMode;
@JsonProperty("uuid") private MetricInfo metrics;
private String UUID;
} }

View file

@ -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;
}

View file

@ -1,8 +1,9 @@
package org.geysermc.connector.metrics; package org.geysermc.connector.metrics;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.geysermc.api.Geyser; import org.geysermc.api.Geyser;
import org.json.simple.JSONArray; import org.geysermc.connector.GeyserConnector;
import org.json.simple.JSONObject;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -12,8 +13,6 @@ import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.logging.Level; import java.util.logging.Level;
@ -93,20 +92,22 @@ public class Metrics {
* *
* @return The plugin specific data. * @return The plugin specific data.
*/ */
private JSONObject getPluginData() { private JsonObject getPluginData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
data.put("pluginName", name); // Append the name of the server software data.addProperty("pluginName", name); // Append the name of the server software
JSONArray customCharts = new JSONArray(); data.addProperty("pluginVersion", GeyserConnector.VERSION); // Append the name of the server software
JsonArray customCharts = new JsonArray();
for (CustomChart customChart : charts) { for (CustomChart customChart : charts) {
// Add the data of the custom 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 if (chart == null) { // If the chart is null, we skip it
continue; continue;
} }
customCharts.add(chart); customCharts.add(chart);
} }
data.put("customCharts", customCharts); data.add("customCharts", customCharts);
return data; return data;
} }
@ -116,21 +117,24 @@ public class Metrics {
* *
* @return The server specific data. * @return The server specific data.
*/ */
private JSONObject getServerData() { private JsonObject getServerData() {
// OS specific data // OS specific data
int playerAmount = Geyser.getPlayerCount();
String osName = System.getProperty("os.name"); String osName = System.getProperty("os.name");
String osArch = System.getProperty("os.arch"); String osArch = System.getProperty("os.arch");
String osVersion = System.getProperty("os.version"); String osVersion = System.getProperty("os.version");
int coreCount = Runtime.getRuntime().availableProcessors(); 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.addProperty("playerAmount", playerAmount);
data.put("osArch", osArch); data.addProperty("osName", osName);
data.put("osVersion", osVersion); data.addProperty("osArch", osArch);
data.put("coreCount", coreCount); data.addProperty("osVersion", osVersion);
data.addProperty("coreCount", coreCount);
return data; return data;
} }
@ -139,12 +143,13 @@ public class Metrics {
* Collects the data and sends it afterwards. * Collects the data and sends it afterwards.
*/ */
private void submitData() { private void submitData() {
final JSONObject data = getServerData(); final JsonObject data = getServerData();
JSONArray pluginData = new JSONArray(); JsonArray pluginData = new JsonArray();
pluginData.add(getPluginData()); pluginData.add(getPluginData());
data.put("plugins", pluginData); data.add("plugins", pluginData);
new Thread(() -> {
try { try {
// We are still in the Thread of the timer, so nothing get blocked :) // We are still in the Thread of the timer, so nothing get blocked :)
sendData(data); sendData(data);
@ -154,6 +159,7 @@ public class Metrics {
logger.log(Level.WARNING, "Could not submit stats of " + name, e); 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. * @param data The data to send.
* @throws Exception If the request failed. * @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) { if (data == null) {
throw new IllegalArgumentException("Data cannot be null!"); throw new IllegalArgumentException("Data cannot be null!");
} }
@ -228,16 +234,16 @@ public class Metrics {
this.chartId = chartId; this.chartId = chartId;
} }
private JSONObject getRequestJsonObject() { private JsonObject getRequestJsonObject() {
JSONObject chart = new JSONObject(); JsonObject chart = new JsonObject();
chart.put("chartId", chartId); chart.addProperty("chartId", chartId);
try { try {
JSONObject data = getChartData(); JsonObject data = getChartData();
if (data == null) { if (data == null) {
// If the data is null we don't send the chart. // If the data is null we don't send the chart.
return null; return null;
} }
chart.put("data", data); chart.add("data", data);
} catch (Throwable t) { } catch (Throwable t) {
if (logFailedRequests) { if (logFailedRequests) {
logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); logger.log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
@ -247,7 +253,7 @@ public class Metrics {
return chart; return chart;
} }
protected abstract JSONObject getChartData() throws Exception; protected abstract JsonObject getChartData() throws Exception;
} }
@ -270,14 +276,14 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
String value = callable.call(); String value = callable.call();
if (value == null || value.isEmpty()) { if (value == null || value.isEmpty()) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("value", value); data.addProperty("value", value);
return data; return data;
} }
} }
@ -301,9 +307,9 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -315,13 +321,13 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
values.put(entry.getKey(), entry.getValue()); values.addProperty(entry.getKey(), entry.getValue());
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("values", values); data.add("values", values);
return data; return data;
} }
} }
@ -345,9 +351,9 @@ public class Metrics {
} }
@Override @Override
public JSONObject getChartData() throws Exception { public JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
Map<String, Map<String, Integer>> map = callable.call(); Map<String, Map<String, Integer>> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -355,22 +361,22 @@ public class Metrics {
} }
boolean reallyAllSkipped = true; boolean reallyAllSkipped = true;
for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) { for (Map.Entry<String, Map<String, Integer>> entryValues : map.entrySet()) {
JSONObject value = new JSONObject(); JsonObject value = new JsonObject();
boolean allSkipped = true; boolean allSkipped = true;
for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) { for (Map.Entry<String, Integer> valueEntry : map.get(entryValues.getKey()).entrySet()) {
value.put(valueEntry.getKey(), valueEntry.getValue()); value.addProperty(valueEntry.getKey(), valueEntry.getValue());
allSkipped = false; allSkipped = false;
} }
if (!allSkipped) { if (!allSkipped) {
reallyAllSkipped = false; reallyAllSkipped = false;
values.put(entryValues.getKey(), value); values.add(entryValues.getKey(), value);
} }
} }
if (reallyAllSkipped) { if (reallyAllSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("values", values); data.add("values", values);
return data; return data;
} }
} }
@ -394,14 +400,14 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
int value = callable.call(); int value = callable.call();
if (value == 0) { if (value == 0) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("value", value); data.addProperty("value", value);
return data; return data;
} }
@ -426,9 +432,9 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -440,13 +446,13 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
values.put(entry.getKey(), entry.getValue()); values.addProperty(entry.getKey(), entry.getValue());
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("values", values); data.add("values", values);
return data; return data;
} }
@ -471,20 +477,20 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
Map<String, Integer> map = callable.call(); Map<String, Integer> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
for (Map.Entry<String, Integer> entry : map.entrySet()) { for (Map.Entry<String, Integer> entry : map.entrySet()) {
JSONArray categoryValues = new JSONArray(); JsonArray categoryValues = new JsonArray();
categoryValues.add(entry.getValue()); categoryValues.add(entry.getValue());
values.put(entry.getKey(), categoryValues); values.add(entry.getKey(), categoryValues);
} }
data.put("values", values); data.add("values", values);
return data; return data;
} }
@ -509,9 +515,9 @@ public class Metrics {
} }
@Override @Override
protected JSONObject getChartData() throws Exception { protected JsonObject getChartData() throws Exception {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
Map<String, int[]> map = callable.call(); Map<String, int[]> map = callable.call();
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -523,20 +529,19 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
JSONArray categoryValues = new JSONArray(); JsonArray categoryValues = new JsonArray();
for (int categoryValue : entry.getValue()) { for (int categoryValue : entry.getValue()) {
categoryValues.add(categoryValue); categoryValues.add(categoryValue);
} }
values.put(entry.getKey(), categoryValues); values.add(entry.getKey(), categoryValues);
} }
if (allSkipped) { if (allSkipped) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("values", values); data.add("values", values);
return data; return data;
} }
} }
} }

View file

@ -3,7 +3,8 @@
# #
# A bridge between Minecraft: Bedrock Edition and Minecraft: Java Edition. # 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: bedrock:
@ -45,8 +46,15 @@ max-players: 100
# If debug messages should be sent through console # If debug messages should be sent through console
debug-mode: false debug-mode: false
# UUID: DON'T CHANGE! # bStats is a stat tracker that is entirely anonymous and tracks only basic information
uuid: UUIDTESTUUIDTEST # 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