From ed5ce197955e5812c6917d2ab513a7c13e6df1ba Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 13 Sep 2012 17:06:48 +0200 Subject: [PATCH] Added a plugin users report to metrics. --- .../comphenix/protocol/ProtocolLibrary.java | 35 ++++++--- .../protocol/events/PacketAdapter.java | 20 +++-- .../protocol/metrics/Statistics.java | 78 +++++++++++++++++++ 3 files changed, 114 insertions(+), 19 deletions(-) create mode 100644 ProtocolLib/src/com/comphenix/protocol/metrics/Statistics.java diff --git a/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java b/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java index 12cb5b24..8ce98be9 100644 --- a/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java +++ b/ProtocolLib/src/com/comphenix/protocol/ProtocolLibrary.java @@ -10,6 +10,8 @@ import org.bukkit.plugin.java.JavaPlugin; import com.comphenix.protocol.injector.PacketFilterManager; import com.comphenix.protocol.metrics.Metrics; +import com.comphenix.protocol.metrics.Metrics.Plotter; +import com.comphenix.protocol.metrics.Statistics; public class ProtocolLibrary extends JavaPlugin { @@ -18,21 +20,14 @@ public class ProtocolLibrary extends JavaPlugin { // Error logger private Logger logger; - - // Metrics - private Metrics metrics; + + // Metrics and statistisc + private Statistics statistisc; @Override public void onLoad() { logger = getLoggerSafely(); protocolManager = new PacketFilterManager(getClassLoader(), logger); - - try { - metrics = new Metrics(this); - metrics.start(); - } catch (IOException e) { - logger.log(Level.SEVERE, "Unable to enable metrics.", e); - } } @Override @@ -43,14 +38,24 @@ public class ProtocolLibrary extends JavaPlugin { // Player login and logout events protocolManager.registerEvents(manager, this); protocolManager.initializePlayers(server.getOnlinePlayers()); + + // Try to enable statistics + try { + statistisc = new Statistics(this); + } catch (IOException e) { + logger.log(Level.SEVERE, "Unable to enable metrics.", e); + } catch (Throwable e) { + logger.log(Level.SEVERE, "Metrics cannot be enabled. Incompatible Bukkit version.", e); + } } @Override public void onDisable() { protocolManager.close(); protocolManager = null; + statistisc = null; } - + /** * Retrieves the packet protocol manager. * @return Packet protocol manager, or NULL if it has been disabled. @@ -59,6 +64,14 @@ public class ProtocolLibrary extends JavaPlugin { return protocolManager; } + /** + * Retrieve the metrics instance used to measure users of this library. + * @return Metrics instance container. + */ + public Statistics getStatistics() { + return statistisc; + } + // Get the Bukkit logger first, before we try to create our own private Logger getLoggerSafely() { diff --git a/ProtocolLib/src/com/comphenix/protocol/events/PacketAdapter.java b/ProtocolLib/src/com/comphenix/protocol/events/PacketAdapter.java index 2244301b..cea27b0e 100644 --- a/ProtocolLib/src/com/comphenix/protocol/events/PacketAdapter.java +++ b/ProtocolLib/src/com/comphenix/protocol/events/PacketAdapter.java @@ -58,20 +58,24 @@ public abstract class PacketAdapter implements PacketListener { return plugin; } - @Override - public String toString() { - String name = ""; - + /** + * Retrieves the name of the plugin that has been associated with the listener. + * @return Name of the associated plugin. + */ + public String getPluginName() { // Try to get the plugin name try { - name = plugin.getName(); + return plugin.getName(); } catch (NoSuchMethodError e) { - name = plugin.toString(); + return plugin.toString(); } - + } + + @Override + public String toString() { // This is used by the error reporter return String.format("PacketAdapter[plugin=%s, side=%s, packets=%s]", - name, getConnectionSide().name(), + getPluginName(), getConnectionSide().name(), Joiner.on(", ").join(packetsID)); } } diff --git a/ProtocolLib/src/com/comphenix/protocol/metrics/Statistics.java b/ProtocolLib/src/com/comphenix/protocol/metrics/Statistics.java new file mode 100644 index 00000000..4be64d78 --- /dev/null +++ b/ProtocolLib/src/com/comphenix/protocol/metrics/Statistics.java @@ -0,0 +1,78 @@ +package com.comphenix.protocol.metrics; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.plugin.Plugin; + +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketAdapter; +import com.comphenix.protocol.events.PacketListener; +import com.comphenix.protocol.metrics.Metrics.Graph; + +public class Statistics { + + // Metrics + private Metrics metrics; + + public Statistics(Plugin plugin) throws IOException { + metrics = new Metrics(plugin); + metrics.start(); + + // Determine who is using this library + addPluginUserGraph(metrics); + } + + private void addPluginUserGraph(Metrics metrics) { + + Graph pluginUsers = metrics.createGraph("Plugin Users"); + + for (Map.Entry entry : getPluginUsers(ProtocolLibrary.getProtocolManager()).entrySet()) { + final int count = entry.getValue(); + + // Plot plugins of this type + pluginUsers.addPlotter(new Metrics.Plotter(entry.getKey()) { + @Override + public int getValue() { + return count; + } + }); + } + } + + // Retrieve loaded plugins + private Map getPluginUsers(ProtocolManager manager) { + + Map users = new HashMap(); + + for (PacketListener listener : manager.getPacketListeners()) { + + String name = "UNKNOWN"; + + if (listener instanceof PacketAdapter) { + PacketAdapter adapter = (PacketAdapter) listener; + name = adapter.getPluginName(); + } + + + // Increment occurence + if (!users.containsKey(name)) { + users.put(name, 1); + } else { + users.put(name, users.get(name) + 1); + } + } + + return users; + } + + /** + * Retrieve the current metrics object. + * @return Metrics object. + */ + public Metrics getMetrics() { + return metrics; + } +}