diff --git a/pom.xml b/pom.xml index 343661f..9ffd0dd 100644 --- a/pom.xml +++ b/pom.xml @@ -84,7 +84,7 @@ commandbook - org.bstats + org.bstats.bStats-Metrics bstats-bukkit diff --git a/src/main/java/me/ryanhamshire/GriefPrevention/metrics/Metrics.java b/src/main/java/me/ryanhamshire/GriefPrevention/metrics/Metrics.java index d7c87da..369a36f 100644 --- a/src/main/java/me/ryanhamshire/GriefPrevention/metrics/Metrics.java +++ b/src/main/java/me/ryanhamshire/GriefPrevention/metrics/Metrics.java @@ -3,17 +3,20 @@ package me.ryanhamshire.GriefPrevention.metrics; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; 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.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; @@ -55,14 +58,23 @@ public class Metrics { // The url to which the data is sent private static final String URL = "https://bStats.org/submitData/bukkit"; + // Is bStats enabled on this server? + private boolean enabled; + // Should failed requests be logged? 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 private static String serverUUID; // The plugin - private final JavaPlugin plugin; + private final Plugin plugin; // A list with all custom charts private final List charts = new ArrayList<>(); @@ -72,7 +84,7 @@ public class Metrics { * * @param plugin The plugin which stats should be submitted. */ - public Metrics(JavaPlugin plugin) { + public Metrics(Plugin plugin) { if (plugin == null) { throw new IllegalArgumentException("Plugin cannot be null!"); } @@ -92,6 +104,10 @@ public class Metrics { config.addDefault("serverUuid", UUID.randomUUID().toString()); // Should failed request be logged? 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 config.options().header( @@ -106,9 +122,13 @@ public class Metrics { } // Load the data + enabled = config.getBoolean("enabled", true); serverUUID = config.getString("serverUuid"); 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; // Search for all other bStats Metrics classes to see if we are the first one for (Class service : Bukkit.getServicesManager().getKnownServices()) { @@ -127,6 +147,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. * @@ -213,8 +242,7 @@ public class Metrics { playerAmount = Bukkit.getOnlinePlayers().size(); // Just use the new method if the Reflection failed } int onlineMode = Bukkit.getOnlineMode() ? 1 : 0; - String bukkitVersion = org.bukkit.Bukkit.getVersion(); - bukkitVersion = bukkitVersion.substring(bukkitVersion.indexOf("MC: ") + 4, bukkitVersion.length() - 1); + String bukkitVersion = Bukkit.getVersion(); // OS/Java specific data String javaVersion = System.getProperty("java.version"); @@ -268,7 +296,7 @@ public class Metrics { public void run() { try { // Send the data - sendData(data); + sendData(plugin, data); } catch (Exception e) { // Something went wrong! :( if (logFailedRequests) { @@ -282,16 +310,20 @@ public class Metrics { /** * 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. * @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) { throw new IllegalArgumentException("Data cannot be null!"); } if (Bukkit.isPrimaryThread()) { 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(); // Compress the data to save bandwidth @@ -313,7 +345,18 @@ public class Metrics { outputStream.flush(); 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()); + } } /**