Update bStats Metrics to use Gson

This commit is contained in:
md678685 2019-04-24 13:54:33 +01:00
parent cfba203981
commit af4cfd3fe9

View File

@ -1,13 +1,12 @@
package com.earth2me.essentials.metrics; package com.earth2me.essentials.metrics;
import com.google.gson.*;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.*; import java.io.*;
@ -68,6 +67,8 @@ public class Metrics {
// A list with all custom charts // A list with all custom charts
private final List<CustomChart> charts = new ArrayList<>(); private final List<CustomChart> charts = new ArrayList<>();
private final Gson gson = new Gson();
/** /**
* Class constructor. * Class constructor.
@ -186,24 +187,24 @@ public class Metrics {
* *
* @return The plugin specific data. * @return The plugin specific data.
*/ */
public JSONObject getPluginData() { public JsonObject getPluginData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
String pluginName = plugin.getDescription().getName().replace("Essentials", "EssentialsX"); String pluginName = plugin.getDescription().getName().replace("Essentials", "EssentialsX");
String pluginVersion = plugin.getDescription().getVersion(); String pluginVersion = plugin.getDescription().getVersion();
data.put("pluginName", pluginName); // Append the name of the plugin data.addProperty("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", pluginVersion); // Append the version of the plugin data.addProperty("pluginVersion", pluginVersion); // Append the version of the plugin
JSONArray customCharts = new JSONArray(); 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;
} }
@ -213,7 +214,7 @@ public class Metrics {
* *
* @return The server specific data. * @return The server specific data.
*/ */
private JSONObject getServerData() { private JsonObject getServerData() {
// Minecraft specific data // Minecraft specific data
int playerAmount; int playerAmount;
try { try {
@ -236,19 +237,19 @@ public class Metrics {
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("playerAmount", playerAmount); data.addProperty("playerAmount", playerAmount);
data.put("onlineMode", onlineMode); data.addProperty("onlineMode", onlineMode);
data.put("bukkitVersion", bukkitVersion); data.addProperty("bukkitVersion", bukkitVersion);
data.put("javaVersion", javaVersion); data.addProperty("javaVersion", javaVersion);
data.put("osName", osName); data.addProperty("osName", osName);
data.put("osArch", osArch); data.addProperty("osArch", osArch);
data.put("osVersion", osVersion); data.addProperty("osVersion", osVersion);
data.put("coreCount", coreCount); data.addProperty("coreCount", coreCount);
return data; return data;
} }
@ -257,9 +258,9 @@ 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();
// Search for all other bStats Metrics classes to get their plugin data // Search for all other bStats Metrics classes to get their plugin data
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) { for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try { try {
@ -267,26 +268,23 @@ public class Metrics {
for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) { for (RegisteredServiceProvider<?> provider : Bukkit.getServicesManager().getRegistrations(service)) {
try { try {
pluginData.add(provider.getService().getMethod("getPluginData").invoke(provider.getProvider())); pluginData.add(gson.toJsonTree(provider.getService().getMethod("getPluginData").invoke(provider.getProvider())));
} catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { } } catch (NullPointerException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { }
} }
} catch (NoSuchFieldException ignored) { } } catch (NoSuchFieldException ignored) { }
} }
data.put("plugins", pluginData); data.add("plugins", pluginData);
// Create a new thread for the connection to the bStats server // Create a new thread for the connection to the bStats server
new Thread(new Runnable() { new Thread(() -> {
@Override try {
public void run() { // Send the data
try { sendData(plugin, data);
// Send the data } catch (Exception e) {
sendData(plugin, data); // Something went wrong! :(
} catch (Exception e) { if (logFailedRequests) {
// Something went wrong! :( plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
if (logFailedRequests) {
plugin.getLogger().log(Level.WARNING, "Could not submit plugin stats of " + plugin.getName(), e);
}
} }
} }
}).start(); }).start();
@ -299,7 +297,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(Plugin plugin, JSONObject data) throws Exception { private static void sendData(Plugin plugin, JsonObject data) throws Exception {
if (data == null) { if (data == null) {
throw new IllegalArgumentException("Data cannot be null!"); throw new IllegalArgumentException("Data cannot be null!");
} }
@ -382,16 +380,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) {
Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t); Bukkit.getLogger().log(Level.WARNING, "Failed to get data for custom chart with id " + chartId, t);
@ -401,7 +399,7 @@ public class Metrics {
return chart; return chart;
} }
protected abstract JSONObject getChartData() throws Exception; protected abstract JsonObject getChartData() throws Exception;
} }
@ -424,14 +422,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;
} }
} }
@ -455,9 +453,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
@ -469,13 +467,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;
} }
} }
@ -499,9 +497,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
@ -509,22 +507,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;
} }
} }
@ -548,14 +546,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;
} }
@ -580,9 +578,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
@ -594,13 +592,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;
} }
@ -625,20 +623,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;
} }
@ -663,9 +661,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
@ -677,17 +675,17 @@ 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;
} }
} }