Added a monitor every packet listener.

This commit is contained in:
Kristian S. Stangeland 2012-10-13 22:19:01 +02:00
parent a173824b3f
commit 98ae5c6e29
2 changed files with 212 additions and 124 deletions

View File

@ -0,0 +1,75 @@
package com.comphenix.protocol.events;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.plugin.Plugin;
import com.comphenix.protocol.Packets;
import com.comphenix.protocol.events.ConnectionSide;
import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.ListeningWhitelist;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.reflect.FieldAccessException;
/**
* Represents a listener that is notified of every sent and recieved packet.
*
* @author Kristian
*/
public abstract class MonitorAdapter implements PacketListener {
private Plugin plugin;
private ListeningWhitelist sending = ListeningWhitelist.EMPTY_WHITELIST;
private ListeningWhitelist receiving = ListeningWhitelist.EMPTY_WHITELIST;
public MonitorAdapter(Plugin plugin, ConnectionSide side) {
this(plugin, side, plugin.getLogger());
}
public MonitorAdapter(Plugin plugin, ConnectionSide side, Logger logger) {
super();
this.plugin = plugin;
// Recover in case something goes wrong
try {
if (side.isForServer())
this.sending = new ListeningWhitelist(ListenerPriority.MONITOR, Packets.Server.getSupported());
if (side.isForClient())
this.receiving = new ListeningWhitelist(ListenerPriority.MONITOR, Packets.Client.getSupported());
} catch (FieldAccessException e) {
if (side.isForServer())
this.sending = new ListeningWhitelist(ListenerPriority.MONITOR, Packets.Server.getRegistry().values());
if (side.isForClient())
this.receiving = new ListeningWhitelist(ListenerPriority.MONITOR, Packets.Client.getRegistry().values());
logger.log(Level.WARNING, "Defaulting to 1.3 packets.", e);
}
}
@Override
public void onPacketReceiving(PacketEvent event) {
// Empty for now
}
@Override
public void onPacketSending(PacketEvent event) {
// Empty for now
}
@Override
public ListeningWhitelist getSendingWhitelist() {
return sending;
}
@Override
public ListeningWhitelist getReceivingWhitelist() {
return receiving;
}
@Override
public Plugin getPlugin() {
return plugin;
}
}

View File

@ -1,124 +1,137 @@
/* /*
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
* Copyright (C) 2012 Kristian S. Stangeland * Copyright (C) 2012 Kristian S. Stangeland
* *
* This program is free software; you can redistribute it and/or modify it under the terms of the * This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 2 of * GNU General Public License as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version. * the License, or (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. * See the GNU General Public License for more details.
* *
* You should have received a copy of the GNU General Public License along with this program; * You should have received a copy of the GNU General Public License along with this program;
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA * 02111-1307 USA
*/ */
package com.comphenix.protocol.events; package com.comphenix.protocol.events;
import org.bukkit.plugin.Plugin; import java.util.Set;
/** import org.bukkit.plugin.Plugin;
* Represents a packet listener with useful constructors.
* /**
* @author Kristian * Represents a packet listener with useful constructors.
*/ *
public abstract class PacketAdapter implements PacketListener { * @author Kristian
*/
protected Plugin plugin; public abstract class PacketAdapter implements PacketListener {
protected ConnectionSide connectionSide;
protected ListeningWhitelist receivingWhitelist = ListeningWhitelist.EMPTY_WHITELIST; protected Plugin plugin;
protected ListeningWhitelist sendingWhitelist = ListeningWhitelist.EMPTY_WHITELIST; protected ConnectionSide connectionSide;
protected ListeningWhitelist receivingWhitelist = ListeningWhitelist.EMPTY_WHITELIST;
/** protected ListeningWhitelist sendingWhitelist = ListeningWhitelist.EMPTY_WHITELIST;
* Initialize a packet listener with default priority.
* @param plugin - the plugin that spawned this listener. /**
* @param connectionSide - the packet type the listener is looking for. * Initialize a packet listener with default priority.
* @param packets - the packet IDs the listener is looking for. * @param plugin - the plugin that spawned this listener.
*/ * @param connectionSide - the packet type the listener is looking for.
public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, Integer... packets) { * @param packets - the packet IDs the listener is looking for.
this(plugin, connectionSide, ListenerPriority.NORMAL, packets); */
} public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, Integer... packets) {
this(plugin, connectionSide, ListenerPriority.NORMAL, packets);
/** }
* Initialize a packet listener for a single connection side.
* @param plugin - the plugin that spawned this listener. /**
* @param connectionSide - the packet type the listener is looking for. * Initialize a packet listener for a single connection side.
* @param listenerPriority - the event priority. * @param plugin - the plugin that spawned this listener.
* @param packets - the packet IDs the listener is looking for. * @param connectionSide - the packet type the listener is looking for.
*/ * @param listenerPriority - the event priority.
public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Integer... packets) { * @param packets - the packet IDs the listener is looking for.
if (plugin == null) */
throw new IllegalArgumentException("plugin cannot be null"); public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Set<Integer> packets) {
if (connectionSide == null) this(plugin, connectionSide, listenerPriority, packets.toArray(new Integer[0]));
throw new IllegalArgumentException("connectionSide cannot be null"); }
if (listenerPriority == null)
throw new IllegalArgumentException("listenerPriority cannot be null"); /**
if (packets == null) * Initialize a packet listener for a single connection side.
throw new IllegalArgumentException("packets cannot be null"); * @param plugin - the plugin that spawned this listener.
* @param connectionSide - the packet type the listener is looking for.
// Add whitelists * @param listenerPriority - the event priority.
if (connectionSide.isForServer()) * @param packets - the packet IDs the listener is looking for.
sendingWhitelist = new ListeningWhitelist(listenerPriority, packets); */
if (connectionSide.isForClient()) public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Integer... packets) {
receivingWhitelist = new ListeningWhitelist(listenerPriority, packets); if (plugin == null)
throw new IllegalArgumentException("plugin cannot be null");
this.plugin = plugin; if (connectionSide == null)
this.connectionSide = connectionSide; throw new IllegalArgumentException("connectionSide cannot be null");
} if (listenerPriority == null)
throw new IllegalArgumentException("listenerPriority cannot be null");
@Override if (packets == null)
public void onPacketReceiving(PacketEvent event) { throw new IllegalArgumentException("packets cannot be null");
// Default is to do nothing
} // Add whitelists
if (connectionSide.isForServer())
@Override sendingWhitelist = new ListeningWhitelist(listenerPriority, packets);
public void onPacketSending(PacketEvent event) { if (connectionSide.isForClient())
// And here too receivingWhitelist = new ListeningWhitelist(listenerPriority, packets);
}
this.plugin = plugin;
@Override this.connectionSide = connectionSide;
public ListeningWhitelist getReceivingWhitelist() { }
return receivingWhitelist;
} @Override
public void onPacketReceiving(PacketEvent event) {
@Override // Default is to do nothing
public ListeningWhitelist getSendingWhitelist() { }
return sendingWhitelist;
} @Override
public void onPacketSending(PacketEvent event) {
@Override // And here too
public Plugin getPlugin() { }
return plugin;
} @Override
public ListeningWhitelist getReceivingWhitelist() {
/** return receivingWhitelist;
* Retrieves the name of the plugin that has been associated with the listener. }
* @return Name of the associated plugin.
*/ @Override
public static String getPluginName(PacketListener listener) { public ListeningWhitelist getSendingWhitelist() {
return sendingWhitelist;
Plugin plugin = listener.getPlugin(); }
// Try to get the plugin name @Override
try { public Plugin getPlugin() {
if (plugin == null) return plugin;
return "UNKNOWN"; }
else
return plugin.getName(); /**
* Retrieves the name of the plugin that has been associated with the listener.
} catch (NoSuchMethodError e) { * @return Name of the associated plugin.
return plugin.toString(); */
} public static String getPluginName(PacketListener listener) {
}
Plugin plugin = listener.getPlugin();
@Override
public String toString() { // Try to get the plugin name
// This is used by the error reporter try {
return String.format("PacketAdapter[plugin=%s, sending=%s, receiving=%s]", if (plugin == null)
getPluginName(this), return "UNKNOWN";
sendingWhitelist, else
receivingWhitelist); return plugin.getName();
}
} } catch (NoSuchMethodError e) {
return plugin.toString();
}
}
@Override
public String toString() {
// This is used by the error reporter
return String.format("PacketAdapter[plugin=%s, sending=%s, receiving=%s]",
getPluginName(this),
sendingWhitelist,
receivingWhitelist);
}
}