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 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 {

View File

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

View File

@ -50,10 +50,7 @@ public abstract class PacketAdapter implements PacketListener {
return packetsID;
}
/**
* Retrieves the plugin associated with this listener.
* @return The associated plugin.
*/
@Override
public Plugin getPlugin() {
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.
* @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 {
if (plugin == null)
return "UNKNOWN";
else
return plugin.getName();
} catch (NoSuchMethodError e) {
return plugin.toString();
}
@ -75,7 +79,7 @@ public abstract class PacketAdapter implements PacketListener {
public String toString() {
// This is used by the error reporter
return String.format("PacketAdapter[plugin=%s, side=%s, packets=%s]",
getPluginName(), getConnectionSide().name(),
getPluginName(this), getConnectionSide().name(),
Joiner.on(", ").join(packetsID));
}
}

View File

@ -2,6 +2,8 @@ package com.comphenix.protocol.events;
import java.util.Set;
import org.bukkit.plugin.Plugin;
public interface PacketListener {
@ -31,4 +33,10 @@ public interface PacketListener {
* @return Packets IDs.
*/
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.events.ConnectionSide;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.reflect.FuzzyReflection;
import com.google.common.base.Objects;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
@ -107,20 +107,18 @@ public final class PacketFilterManager implements ProtocolManager {
}
@Override
public void removePacketAdapters(Plugin plugin) {
public void removePacketListeners(Plugin plugin) {
// Iterate through every packet listener
for (Object listener : packetListeners.toArray()) {
if (listener instanceof PacketAdapter) {
PacketAdapter adapter = (PacketAdapter) listener;
for (Object element : packetListeners.toArray()) {
PacketListener listener = (PacketListener) element;
// Remove the listener
if (adapter.getPlugin().equals(plugin)) {
if (Objects.equal(listener.getPlugin(), plugin)) {
packetListeners.remove(listener);
}
}
}
}
/**
* Invokes the given packet event for every registered listener.

View File

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