Added a plugin users report to metrics.

This commit is contained in:
Kristian S. Stangeland 2012-09-13 17:06:48 +02:00
parent 83f313ce89
commit ed5ce19795
3 changed files with 114 additions and 19 deletions

View File

@ -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() {

View File

@ -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));
}
}

View File

@ -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<String, Integer> 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<String, Integer> getPluginUsers(ProtocolManager manager) {
Map<String, Integer> users = new HashMap<String, Integer>();
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;
}
}