mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2024-11-01 00:11:39 +01:00
Added a plugin users report to metrics.
This commit is contained in:
parent
83f313ce89
commit
ed5ce19795
@ -10,6 +10,8 @@ import org.bukkit.plugin.java.JavaPlugin;
|
|||||||
|
|
||||||
import com.comphenix.protocol.injector.PacketFilterManager;
|
import com.comphenix.protocol.injector.PacketFilterManager;
|
||||||
import com.comphenix.protocol.metrics.Metrics;
|
import com.comphenix.protocol.metrics.Metrics;
|
||||||
|
import com.comphenix.protocol.metrics.Metrics.Plotter;
|
||||||
|
import com.comphenix.protocol.metrics.Statistics;
|
||||||
|
|
||||||
public class ProtocolLibrary extends JavaPlugin {
|
public class ProtocolLibrary extends JavaPlugin {
|
||||||
|
|
||||||
@ -18,21 +20,14 @@ public class ProtocolLibrary extends JavaPlugin {
|
|||||||
|
|
||||||
// Error logger
|
// Error logger
|
||||||
private Logger logger;
|
private Logger logger;
|
||||||
|
|
||||||
// Metrics
|
// Metrics and statistisc
|
||||||
private Metrics metrics;
|
private Statistics statistisc;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onLoad() {
|
public void onLoad() {
|
||||||
logger = getLoggerSafely();
|
logger = getLoggerSafely();
|
||||||
protocolManager = new PacketFilterManager(getClassLoader(), logger);
|
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
|
@Override
|
||||||
@ -43,14 +38,24 @@ public class ProtocolLibrary extends JavaPlugin {
|
|||||||
// Player login and logout events
|
// Player login and logout events
|
||||||
protocolManager.registerEvents(manager, this);
|
protocolManager.registerEvents(manager, this);
|
||||||
protocolManager.initializePlayers(server.getOnlinePlayers());
|
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
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
protocolManager.close();
|
protocolManager.close();
|
||||||
protocolManager = null;
|
protocolManager = null;
|
||||||
|
statistisc = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the packet protocol manager.
|
* Retrieves the packet protocol manager.
|
||||||
* @return Packet protocol manager, or NULL if it has been disabled.
|
* @return Packet protocol manager, or NULL if it has been disabled.
|
||||||
@ -59,6 +64,14 @@ public class ProtocolLibrary extends JavaPlugin {
|
|||||||
return protocolManager;
|
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
|
// Get the Bukkit logger first, before we try to create our own
|
||||||
private Logger getLoggerSafely() {
|
private Logger getLoggerSafely() {
|
||||||
|
|
||||||
|
@ -58,20 +58,24 @@ public abstract class PacketAdapter implements PacketListener {
|
|||||||
return plugin;
|
return plugin;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public String toString() {
|
* Retrieves the name of the plugin that has been associated with the listener.
|
||||||
String name = "";
|
* @return Name of the associated plugin.
|
||||||
|
*/
|
||||||
|
public String getPluginName() {
|
||||||
// Try to get the plugin name
|
// Try to get the plugin name
|
||||||
try {
|
try {
|
||||||
name = plugin.getName();
|
return plugin.getName();
|
||||||
} catch (NoSuchMethodError e) {
|
} catch (NoSuchMethodError e) {
|
||||||
name = plugin.toString();
|
return plugin.toString();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
// This is used by the error reporter
|
// This is used by the error reporter
|
||||||
return String.format("PacketAdapter[plugin=%s, side=%s, packets=%s]",
|
return String.format("PacketAdapter[plugin=%s, side=%s, packets=%s]",
|
||||||
name, getConnectionSide().name(),
|
getPluginName(), getConnectionSide().name(),
|
||||||
Joiner.on(", ").join(packetsID));
|
Joiner.on(", ").join(packetsID));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user