Update main plugin to 1.18, convert Metrics to gson

This commit is contained in:
Martoph 2021-12-06 22:45:31 -06:00
parent 08000bda0d
commit 92b741ce0a
2 changed files with 69 additions and 68 deletions

View File

@ -1,12 +1,13 @@
package me.libraryaddict.disguise.utilities.metrics; package me.libraryaddict.disguise.utilities.metrics;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import me.libraryaddict.disguise.utilities.DisguiseUtilities; import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.ServicePriority; import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -174,23 +175,23 @@ 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(); String pluginName = plugin.getDescription().getName();
data.put("pluginName", pluginName); // Append the name of the plugin data.addProperty("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", version); // Append the version of the plugin data.addProperty("pluginVersion", version); // 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;
} }
@ -200,7 +201,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 = Bukkit.getOnlinePlayers().size(); int playerAmount = Bukkit.getOnlinePlayers().size();
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
@ -214,19 +215,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;
} }
@ -235,9 +236,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 {
@ -248,13 +249,13 @@ public class Metrics {
} }
// Found one! // Found one!
try { try {
pluginData.add(service.getMethod("getPluginData").invoke(Bukkit.getServicesManager().load(service))); pluginData.add(new Gson().toJson(service.getMethod("getPluginData").invoke(Bukkit.getServicesManager().load(service))));
} }
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) { catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException 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(new Runnable() {
@ -281,7 +282,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!");
} }
@ -350,16 +351,16 @@ public class Metrics {
this.chartId = chartId; this.chartId = chartId;
} }
protected JSONObject getRequestJsonObject() { protected 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) {
@ -370,7 +371,7 @@ public class Metrics {
return chart; return chart;
} }
protected abstract JSONObject getChartData(); protected abstract JsonObject getChartData();
} }
/** /**
@ -395,14 +396,14 @@ public class Metrics {
public abstract String getValue(); public abstract String getValue();
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
String value = getValue(); String value = getValue();
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;
} }
} }
@ -431,9 +432,9 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap); public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>()); HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -445,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;
} }
} }
@ -478,14 +479,14 @@ public class Metrics {
public abstract int getValue(); public abstract int getValue();
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
int value = getValue(); int value = getValue();
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;
} }
} }
@ -514,9 +515,9 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap); public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>()); HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -528,13 +529,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;
} }
} }
@ -563,20 +564,20 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap); public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>()); HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
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;
} }
} }
@ -605,9 +606,9 @@ public class Metrics {
public abstract HashMap<String, int[]> getValues(HashMap<String, int[]> valueMap); public abstract HashMap<String, int[]> getValues(HashMap<String, int[]> valueMap);
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
HashMap<String, int[]> map = getValues(new HashMap<String, int[]>()); HashMap<String, int[]> map = getValues(new HashMap<String, int[]>());
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -619,17 +620,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;
} }
} }
@ -656,15 +657,15 @@ public class Metrics {
public abstract Country getValue(); public abstract Country getValue();
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
Country value = getValue(); Country value = getValue();
if (value == null) { if (value == null) {
// Null = skip the chart // Null = skip the chart
return null; return null;
} }
data.put("value", value.getCountryIsoTag()); data.addProperty("value", value.getCountryIsoTag());
return data; return data;
} }
} }
@ -693,9 +694,9 @@ public class Metrics {
public abstract HashMap<Country, Integer> getValues(HashMap<Country, Integer> valueMap); public abstract HashMap<Country, Integer> getValues(HashMap<Country, Integer> valueMap);
@Override @Override
protected JSONObject getChartData() { protected JsonObject getChartData() {
JSONObject data = new JSONObject(); JsonObject data = new JsonObject();
JSONObject values = new JSONObject(); JsonObject values = new JsonObject();
HashMap<Country, Integer> map = getValues(new HashMap<Country, Integer>()); HashMap<Country, Integer> map = getValues(new HashMap<Country, Integer>());
if (map == null || map.isEmpty()) { if (map == null || map.isEmpty()) {
// Null = skip the chart // Null = skip the chart
@ -707,13 +708,13 @@ public class Metrics {
continue; // Skip this invalid continue; // Skip this invalid
} }
allSkipped = false; allSkipped = false;
values.put(entry.getKey().getCountryIsoTag(), entry.getValue()); values.addProperty(entry.getKey().getCountryIsoTag(), 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;
} }
} }

View File

@ -23,7 +23,7 @@
<lombok.version>1.18.22</lombok.version> <lombok.version>1.18.22</lombok.version>
<protocollib.version>4.7.0</protocollib.version> <protocollib.version>4.7.0</protocollib.version>
<spigot.version>1.17-R0.1-SNAPSHOT</spigot.version> <spigot.version>1.18-R0.1-SNAPSHOT</spigot.version>
<junit.version>4.13.2</junit.version> <junit.version>4.13.2</junit.version>
<paper-api.version>1.16.5-R0.1-SNAPSHOT</paper-api.version> <paper-api.version>1.16.5-R0.1-SNAPSHOT</paper-api.version>
<bungeecord-chat.version>1.12-SNAPSHOT</bungeecord-chat.version> <bungeecord-chat.version>1.12-SNAPSHOT</bungeecord-chat.version>