Made the "getPlugin" method mandatory for all listeners.

It's pretty useful for the data collector, as well as the utility
method "removePacketListeners".
This commit is contained in:
Kristian S. Stangeland 2012-09-13 17:29:37 +02:00
parent ed5ce19795
commit 0653dc8c15
6 changed files with 29 additions and 27 deletions

View File

@ -9,8 +9,6 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin; 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.Plotter;
import com.comphenix.protocol.metrics.Statistics; import com.comphenix.protocol.metrics.Statistics;
public class ProtocolLibrary extends JavaPlugin { public class ProtocolLibrary extends JavaPlugin {

View File

@ -40,7 +40,7 @@ public interface ProtocolManager {
* Note that this only works for listeners that derive from PacketAdapter. * Note that this only works for listeners that derive from PacketAdapter.
* @param plugin - the plugin to unload. * @param plugin - the plugin to unload.
*/ */
public void removePacketAdapters(Plugin plugin); public void removePacketListeners(Plugin plugin);
/** /**
* Send a packet to the given player. * Send a packet to the given player.

View File

@ -50,10 +50,7 @@ public abstract class PacketAdapter implements PacketListener {
return packetsID; return packetsID;
} }
/** @Override
* Retrieves the plugin associated with this listener.
* @return The associated plugin.
*/
public Plugin getPlugin() { public Plugin getPlugin() {
return plugin; return plugin;
} }
@ -62,10 +59,17 @@ public abstract class PacketAdapter implements PacketListener {
* Retrieves the name of the plugin that has been associated with the listener. * Retrieves the name of the plugin that has been associated with the listener.
* @return Name of the associated plugin. * @return Name of the associated plugin.
*/ */
public String getPluginName() { public static String getPluginName(PacketListener listener) {
Plugin plugin = listener.getPlugin();
// Try to get the plugin name // Try to get the plugin name
try { try {
return plugin.getName(); if (plugin == null)
return "UNKNOWN";
else
return plugin.getName();
} catch (NoSuchMethodError e) { } catch (NoSuchMethodError e) {
return plugin.toString(); return plugin.toString();
} }
@ -75,7 +79,7 @@ public abstract class PacketAdapter implements PacketListener {
public String toString() { 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]",
getPluginName(), getConnectionSide().name(), getPluginName(this), getConnectionSide().name(),
Joiner.on(", ").join(packetsID)); Joiner.on(", ").join(packetsID));
} }
} }

View File

@ -2,6 +2,8 @@ package com.comphenix.protocol.events;
import java.util.Set; import java.util.Set;
import org.bukkit.plugin.Plugin;
public interface PacketListener { public interface PacketListener {
@ -31,4 +33,10 @@ public interface PacketListener {
* @return Packets IDs. * @return Packets IDs.
*/ */
public Set<Integer> getPacketsID(); public Set<Integer> getPacketsID();
/**
* Retrieve the plugin that created list packet listener.
* @return The plugin, or NULL if not available.
*/
public Plugin getPlugin();
} }

View File

@ -27,11 +27,11 @@ import org.bukkit.plugin.PluginManager;
import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.ConnectionSide; import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener; import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.reflect.FuzzyReflection; import com.comphenix.protocol.reflect.FuzzyReflection;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
@ -107,17 +107,15 @@ public final class PacketFilterManager implements ProtocolManager {
} }
@Override @Override
public void removePacketAdapters(Plugin plugin) { public void removePacketListeners(Plugin plugin) {
// Iterate through every packet listener // Iterate through every packet listener
for (Object listener : packetListeners.toArray()) { for (Object element : packetListeners.toArray()) {
if (listener instanceof PacketAdapter) { PacketListener listener = (PacketListener) element;
PacketAdapter adapter = (PacketAdapter) listener;
// Remove the listener
// Remove the listener if (Objects.equal(listener.getPlugin(), plugin)) {
if (adapter.getPlugin().equals(plugin)) { packetListeners.remove(listener);
packetListeners.remove(listener);
}
} }
} }
} }

View File

@ -49,13 +49,7 @@ public class Statistics {
for (PacketListener listener : manager.getPacketListeners()) { for (PacketListener listener : manager.getPacketListeners()) {
String name = "UNKNOWN"; String name = PacketAdapter.getPluginName(listener);
if (listener instanceof PacketAdapter) {
PacketAdapter adapter = (PacketAdapter) listener;
name = adapter.getPluginName();
}
// Increment occurence // Increment occurence
if (!users.containsKey(name)) { if (!users.containsKey(name)) {