diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java index a206fdf6..46dc854a 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/ProtocolLib.java @@ -654,4 +654,8 @@ public class ProtocolLib extends JavaPlugin { public Statistics getStatistics() { return statistics; } + + public ProtocolConfig getProtocolConfig() { + return config; + } } diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java index 993839e4..654b29c3 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Metrics.java @@ -1,5 +1,6 @@ package com.comphenix.protocol.metrics; +import org.apache.commons.lang3.tuple.Pair; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -128,6 +129,10 @@ public class Metrics { } } + void logFailedRequests(boolean value) { + logFailedRequests = value; + } + /** * Adds a custom chart. * @@ -177,10 +182,10 @@ public class Metrics { JSONObject data = new JSONObject(); String pluginName = plugin.getDescription().getName(); - String pluginVersion = plugin.getDescription().getVersion(); + Pair pluginVersion = Statistics.splitVersion(); // ProtocolLib - remove build number data.put("pluginName", pluginName); // Append the name of the plugin - data.put("pluginVersion", pluginVersion); // Append the version of the plugin + data.put("pluginVersion", pluginVersion.getLeft()); // Append the version of the plugin JSONArray customCharts = new JSONArray(); for (CustomChart customChart : charts) { // Add the data of the custom charts diff --git a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Statistics.java b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Statistics.java index 8bf1193b..67b74e32 100644 --- a/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Statistics.java +++ b/modules/ProtocolLib/src/main/java/com/comphenix/protocol/metrics/Statistics.java @@ -20,46 +20,50 @@ package com.comphenix.protocol.metrics; import java.io.IOException; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Callable; - -import org.bukkit.plugin.Plugin; +import com.comphenix.protocol.ProtocolLib; import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketListener; +import org.apache.commons.lang3.tuple.Pair; +import org.bukkit.plugin.Plugin; + public class Statistics { // Metrics private Metrics metrics; - - public Statistics(Plugin plugin) throws IOException { + + public Statistics(ProtocolLib plugin) throws IOException { metrics = new Metrics(plugin); - + metrics.logFailedRequests(plugin.getProtocolConfig().isDebug()); + // Determine who is using this library addPluginUserGraph(metrics); } - + private void addPluginUserGraph(Metrics metrics) { - - metrics.addCustomChart(new Metrics.AdvancedPie("Plugin Users", new Callable>() { - @Override - public Map call() throws Exception { - return getPluginUsers(ProtocolLibrary.getProtocolManager()); - } - })); + metrics.addCustomChart(new Metrics.AdvancedPie("Plugin Users", this::getPluginUsers)); + metrics.addCustomChart(new Metrics.SimplePie("buildVersion", () -> splitVersion().getRight())); } - + + public static Pair splitVersion() { + String version = ProtocolLibrary.getPlugin().getDescription().getVersion(); + if (version.contains("-b")) { + String[] split = version.split("-b"); + return Pair.of(split[0], split[1]); + } else { + return Pair.of(version, "Unknown"); + } + } + // Retrieve loaded plugins - private Map getPluginUsers(ProtocolManager manager) { - - Map users = new HashMap(); - - for (PacketListener listener : manager.getPacketListeners()) { - + private Map getPluginUsers() { + Map users = new HashMap<>(); + + for (PacketListener listener : ProtocolLibrary.getProtocolManager().getPacketListeners()) { String name = PacketAdapter.getPluginName(listener); - + // Increment occurence if (!users.containsKey(name)) { users.put(name, 1); @@ -67,7 +71,7 @@ public class Statistics { users.put(name, users.get(name) + 1); } } - + return users; } }