Updated build.gradle

This commit is contained in:
Sekwah 2020-06-28 22:44:20 +01:00 committed by Sekwah
parent faeb50de39
commit 2423d17793
3 changed files with 146 additions and 121 deletions

View File

@ -1,14 +0,0 @@
package com.sekwah.advancedportals.fabric;
import net.fabricmc.api.ModInitializer;
public class AdvancedPortalsMod implements ModInitializer {
@Override
public void onInitialize() {
// This code runs as soon as Minecraft is in a mod-load-ready state.
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
System.out.println("Hello Fabric world!");
}
}

View File

@ -1,18 +0,0 @@
package com.sekwah.advancedportals.fabric.mixins.common;
import net.minecraft.entity.player.PlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(PlayerEntity.class)
public class PlayerMixin {
@Inject(method="setPosition", at=@At("HEAD"))
public void setPosition(double x, double y, double z, CallbackInfo info) {
System.out.println("MOVE");
}
}

View File

@ -1,22 +1,21 @@
package com.sekwah.advancedportals.metrics; package com.sekwah.advancedportals.metrics;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
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.RegisteredServiceProvider; import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.ServicePriority; 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 javax.net.ssl.HttpsURLConnection;
import java.io.ByteArrayOutputStream; import java.io.*;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.logging.Level; import java.util.logging.Level;
@ -24,9 +23,10 @@ import java.util.zip.GZIPOutputStream;
/** /**
* bStats collects some data for plugin authors. * bStats collects some data for plugin authors.
* * <p>
* Check out https://bStats.org/ to learn more about bStats! * Check out https://bStats.org/ to learn more about bStats!
*/ */
@SuppressWarnings({"WeakerAccess", "unused"})
public class Metrics { public class Metrics {
static { static {
@ -49,14 +49,23 @@ public class Metrics {
// The url to which the data is sent // The url to which the data is sent
private static final String URL = "https://bStats.org/submitData/bukkit"; private static final String URL = "https://bStats.org/submitData/bukkit";
// Is bStats enabled on this server?
private boolean enabled;
// Should failed requests be logged? // Should failed requests be logged?
private static boolean logFailedRequests; private static boolean logFailedRequests;
// Should the sent data be logged?
private static boolean logSentData;
// Should the response text be logged?
private static boolean logResponseStatusText;
// The uuid of the server // The uuid of the server
private static String serverUUID; private static String serverUUID;
// The plugin // The plugin
private final JavaPlugin plugin; private final Plugin plugin;
// 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<>();
@ -66,7 +75,7 @@ public class Metrics {
* *
* @param plugin The plugin which stats should be submitted. * @param plugin The plugin which stats should be submitted.
*/ */
public Metrics(JavaPlugin plugin) { public Metrics(Plugin plugin) {
if (plugin == null) { if (plugin == null) {
throw new IllegalArgumentException("Plugin cannot be null!"); throw new IllegalArgumentException("Plugin cannot be null!");
} }
@ -86,6 +95,10 @@ public class Metrics {
config.addDefault("serverUuid", UUID.randomUUID().toString()); config.addDefault("serverUuid", UUID.randomUUID().toString());
// Should failed request be logged? // Should failed request be logged?
config.addDefault("logFailedRequests", false); config.addDefault("logFailedRequests", false);
// Should the sent data be logged?
config.addDefault("logSentData", false);
// Should the response text be logged?
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats // Inform the server owners about bStats
config.options().header( config.options().header(
@ -100,9 +113,13 @@ public class Metrics {
} }
// Load the data // Load the data
enabled = config.getBoolean("enabled", true);
serverUUID = config.getString("serverUuid"); serverUUID = config.getString("serverUuid");
logFailedRequests = config.getBoolean("logFailedRequests", false); logFailedRequests = config.getBoolean("logFailedRequests", false);
if (config.getBoolean("enabled", true)) { logSentData = config.getBoolean("logSentData", false);
logResponseStatusText = config.getBoolean("logResponseStatusText", false);
if (enabled) {
boolean found = false; boolean found = false;
// Search for all other bStats Metrics classes to see if we are the first one // Search for all other bStats Metrics classes to see if we are the first one
for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) { for (Class<?> service : Bukkit.getServicesManager().getKnownServices()) {
@ -121,6 +138,15 @@ public class Metrics {
} }
} }
/**
* Checks if bStats is enabled.
*
* @return Whether bStats is enabled or not.
*/
public boolean isEnabled() {
return enabled;
}
/** /**
* Adds a custom chart. * Adds a custom chart.
* *
@ -147,12 +173,7 @@ public class Metrics {
} }
// Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler // Nevertheless we want our code to run in the Bukkit main thread, so we have to use the Bukkit scheduler
// Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;) // Don't be afraid! The connection to the bStats server is still async, only the stats collection is sync ;)
Bukkit.getScheduler().runTask(plugin, new Runnable() { Bukkit.getScheduler().runTask(plugin, () -> submitData());
@Override
public void run() {
submitData();
}
});
} }
}, 1000 * 60 * 5, 1000 * 60 * 30); }, 1000 * 60 * 5, 1000 * 60 * 30);
// Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start // Submit the data every 30 minutes, first time after 5 minutes to give other plugins enough time to start
@ -166,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(); String pluginName = plugin.getDescription().getName();
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;
} }
@ -193,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 {
@ -207,8 +228,8 @@ public class Metrics {
playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed
} }
int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; int onlineMode = Bukkit.getOnlineMode() ? 1 : 0;
String bukkitVersion = org.bukkit.Bukkit.getVersion(); String bukkitVersion = Bukkit.getVersion();
bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1); String bukkitName = Bukkit.getName();
// OS/Java specific data // OS/Java specific data
String javaVersion = System.getProperty("java.version"); String javaVersion = System.getProperty("java.version");
@ -217,19 +238,20 @@ 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.addProperty("bukkitName", bukkitName);
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;
} }
@ -238,9 +260,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 +270,33 @@ 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())); Object plugin = provider.getService().getMethod("getPluginData").invoke(provider.getProvider());
if (plugin instanceof JsonObject) {
pluginData.add((JsonObject) plugin);
} else { // old bstats version compatibility
try {
Class<?> jsonObjectJsonSimple = Class.forName("org.json.simple.JSONObject");
if (plugin.getClass().isAssignableFrom(jsonObjectJsonSimple)) {
Method jsonStringGetter = jsonObjectJsonSimple.getDeclaredMethod("toJSONString");
jsonStringGetter.setAccessible(true);
String jsonString = (String) jsonStringGetter.invoke(plugin);
JsonObject object = new JsonParser().parse(jsonString).getAsJsonObject();
pluginData.add(object);
}
} catch (ClassNotFoundException e) {
// minecraft version 1.14+
if (logFailedRequests) {
this.plugin.getLogger().log(Level.SEVERE, "Encountered unexpected exception", e);
}
continue; // continue looping since we cannot do any other thing.
}
}
} 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(new Runnable() {
@ -262,7 +304,7 @@ public class Metrics {
public void run() { public void run() {
try { try {
// Send the data // Send the data
sendData(data); sendData(plugin, data);
} catch (Exception e) { } catch (Exception e) {
// Something went wrong! :( // Something went wrong! :(
if (logFailedRequests) { if (logFailedRequests) {
@ -276,16 +318,20 @@ public class Metrics {
/** /**
* Sends the data to the bStats server. * Sends the data to the bStats server.
* *
* @param plugin Any plugin. It's just used to get a logger instance.
* @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(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!");
} }
if (Bukkit.isPrimaryThread()) { if (Bukkit.isPrimaryThread()) {
throw new IllegalAccessException("This method must not be called from the main thread!"); throw new IllegalAccessException("This method must not be called from the main thread!");
} }
if (logSentData) {
plugin.getLogger().info("Sending data to bStats: " + data.toString());
}
HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection(); HttpsURLConnection connection = (HttpsURLConnection) new URL(URL).openConnection();
// Compress the data to save bandwidth // Compress the data to save bandwidth
@ -307,7 +353,18 @@ public class Metrics {
outputStream.flush(); outputStream.flush();
outputStream.close(); outputStream.close();
connection.getInputStream().close(); // We don't care about the response - Just send our data :) InputStream inputStream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder builder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
builder.append(line);
}
bufferedReader.close();
if (logResponseStatusText) {
plugin.getLogger().info("Sent data to bStats and received response: " + builder.toString());
}
} }
/** /**
@ -323,7 +380,7 @@ public class Metrics {
} }
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(outputStream); GZIPOutputStream gzip = new GZIPOutputStream(outputStream);
gzip.write(str.getBytes("UTF-8")); gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close(); gzip.close();
return outputStream.toByteArray(); return outputStream.toByteArray();
} }
@ -348,16 +405,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);
@ -367,7 +424,7 @@ public class Metrics {
return chart; return chart;
} }
protected abstract JSONObject getChartData() throws Exception; protected abstract JsonObject getChartData() throws Exception;
} }
@ -390,14 +447,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;
} }
} }
@ -421,9 +478,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
@ -435,13 +492,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;
} }
} }
@ -465,9 +522,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
@ -475,22 +532,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;
} }
} }
@ -514,14 +571,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;
} }
@ -546,9 +603,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
@ -560,13 +617,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;
} }
@ -591,20 +648,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;
} }
@ -629,9 +686,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
@ -643,19 +700,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;
} }
}
} }
}