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;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import me.libraryaddict.disguise.utilities.DisguiseUtilities;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.ServicePriority;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayOutputStream;
@ -174,23 +175,23 @@ public class Metrics {
*
* @return The plugin specific data.
*/
public JSONObject getPluginData() {
JSONObject data = new JSONObject();
public JsonObject getPluginData() {
JsonObject data = new JsonObject();
String pluginName = plugin.getDescription().getName();
data.put("pluginName", pluginName); // Append the name of the plugin
data.put("pluginVersion", version); // Append the version of the plugin
JSONArray customCharts = new JSONArray();
data.addProperty("pluginName", pluginName); // Append the name of the plugin
data.addProperty("pluginVersion", version); // Append the version of the plugin
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;
}
@ -200,7 +201,7 @@ public class Metrics {
*
* @return The server specific data.
*/
private JSONObject getServerData() {
private JsonObject getServerData() {
// Minecraft specific data
int playerAmount = Bukkit.getOnlinePlayers().size();
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
@ -214,19 +215,19 @@ public class Metrics {
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("playerAmount", playerAmount);
data.put("onlineMode", onlineMode);
data.put("bukkitVersion", bukkitVersion);
data.addProperty("playerAmount", playerAmount);
data.addProperty("onlineMode", onlineMode);
data.addProperty("bukkitVersion", bukkitVersion);
data.put("javaVersion", javaVersion);
data.put("osName", osName);
data.put("osArch", osArch);
data.put("osVersion", osVersion);
data.put("coreCount", coreCount);
data.addProperty("javaVersion", javaVersion);
data.addProperty("osName", osName);
data.addProperty("osArch", osArch);
data.addProperty("osVersion", osVersion);
data.addProperty("coreCount", coreCount);
return data;
}
@ -235,9 +236,9 @@ 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();
// Search for all other bStats Metrics classes to get their plugin data
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
try {
@ -248,13 +249,13 @@ public class Metrics {
}
// Found one!
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) {
}
}
data.put("plugins", pluginData);
data.add("plugins", pluginData);
// Create a new thread for the connection to the bStats server
new Thread(new Runnable() {
@ -281,7 +282,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!");
}
@ -350,16 +351,16 @@ public class Metrics {
this.chartId = chartId;
}
protected JSONObject getRequestJsonObject() {
JSONObject chart = new JSONObject();
chart.put("chartId", chartId);
protected 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) {
@ -370,7 +371,7 @@ public class Metrics {
return chart;
}
protected abstract JSONObject getChartData();
protected abstract JsonObject getChartData();
}
/**
@ -395,14 +396,14 @@ public class Metrics {
public abstract String getValue();
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
String value = getValue();
if (value == null || value.isEmpty()) {
// Null = skip the chart
return null;
}
data.put("value", value);
data.addProperty("value", value);
return data;
}
}
@ -431,9 +432,9 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
if (map == null || map.isEmpty()) {
// Null = skip the chart
@ -445,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;
}
}
@ -478,14 +479,14 @@ public class Metrics {
public abstract int getValue();
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
int value = getValue();
if (value == 0) {
// Null = skip the chart
return null;
}
data.put("value", value);
data.addProperty("value", value);
return data;
}
}
@ -514,9 +515,9 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
if (map == null || map.isEmpty()) {
// Null = skip the chart
@ -528,13 +529,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;
}
}
@ -563,20 +564,20 @@ public class Metrics {
public abstract HashMap<String, Integer> getValues(HashMap<String, Integer> valueMap);
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
JsonObject values = new JsonObject();
HashMap<String, Integer> map = getValues(new HashMap<String, Integer>());
if (map == null || map.isEmpty()) {
// Null = skip the chart
return null;
}
for (Map.Entry<String, Integer> 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;
}
}
@ -605,9 +606,9 @@ public class Metrics {
public abstract HashMap<String, int[]> getValues(HashMap<String, int[]> valueMap);
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
JsonObject values = new JsonObject();
HashMap<String, int[]> map = getValues(new HashMap<String, int[]>());
if (map == null || map.isEmpty()) {
// Null = skip the chart
@ -619,17 +620,17 @@ 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;
}
}
@ -656,15 +657,15 @@ public class Metrics {
public abstract Country getValue();
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
Country value = getValue();
if (value == null) {
// Null = skip the chart
return null;
}
data.put("value", value.getCountryIsoTag());
data.addProperty("value", value.getCountryIsoTag());
return data;
}
}
@ -693,9 +694,9 @@ public class Metrics {
public abstract HashMap<Country, Integer> getValues(HashMap<Country, Integer> valueMap);
@Override
protected JSONObject getChartData() {
JSONObject data = new JSONObject();
JSONObject values = new JSONObject();
protected JsonObject getChartData() {
JsonObject data = new JsonObject();
JsonObject values = new JsonObject();
HashMap<Country, Integer> map = getValues(new HashMap<Country, Integer>());
if (map == null || map.isEmpty()) {
// Null = skip the chart
@ -707,13 +708,13 @@ public class Metrics {
continue; // Skip this invalid
}
allSkipped = false;
values.put(entry.getKey().getCountryIsoTag(), entry.getValue());
values.addProperty(entry.getKey().getCountryIsoTag(), entry.getValue());
}
if (allSkipped) {
// Null = skip the chart
return null;
}
data.put("values", values);
data.add("values", values);
return data;
}
}

View File

@ -23,7 +23,7 @@
<lombok.version>1.18.22</lombok.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>
<paper-api.version>1.16.5-R0.1-SNAPSHOT</paper-api.version>
<bungeecord-chat.version>1.12-SNAPSHOT</bungeecord-chat.version>