From 98ae5c6e2929bfbd063c022c90bf2cf2f39b6b2e Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Sat, 13 Oct 2012 22:19:01 +0200 Subject: [PATCH] Added a monitor every packet listener. --- .../protocol/events/MonitorAdapter.java | 75 +++++ .../protocol/events/PacketAdapter.java | 261 +++++++++--------- 2 files changed, 212 insertions(+), 124 deletions(-) create mode 100644 ProtocolLib/src/main/java/com/comphenix/protocol/events/MonitorAdapter.java diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/MonitorAdapter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/MonitorAdapter.java new file mode 100644 index 00000000..e3e4e81a --- /dev/null +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/MonitorAdapter.java @@ -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; + } +} + diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java index 58845e87..c49d74ec 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java @@ -1,124 +1,137 @@ -/* - * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. - * Copyright (C) 2012 Kristian S. Stangeland - * - * 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 - * 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; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * 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; - * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - * 02111-1307 USA - */ - -package com.comphenix.protocol.events; - -import org.bukkit.plugin.Plugin; - -/** - * Represents a packet listener with useful constructors. - * - * @author Kristian - */ -public abstract class PacketAdapter implements PacketListener { - - protected Plugin plugin; - 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. - * @param packets - the packet IDs the listener is looking for. - */ - 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. - * @param listenerPriority - the event priority. - * @param packets - the packet IDs the listener is looking for. - */ - public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Integer... packets) { - if (plugin == null) - throw new IllegalArgumentException("plugin cannot be null"); - if (connectionSide == null) - throw new IllegalArgumentException("connectionSide cannot be null"); - if (listenerPriority == null) - throw new IllegalArgumentException("listenerPriority cannot be null"); - if (packets == null) - throw new IllegalArgumentException("packets cannot be null"); - - // Add whitelists - if (connectionSide.isForServer()) - sendingWhitelist = new ListeningWhitelist(listenerPriority, packets); - if (connectionSide.isForClient()) - receivingWhitelist = new ListeningWhitelist(listenerPriority, packets); - - this.plugin = plugin; - this.connectionSide = connectionSide; - } - - @Override - public void onPacketReceiving(PacketEvent event) { - // Default is to do nothing - } - - @Override - public void onPacketSending(PacketEvent event) { - // And here too - } - - @Override - public ListeningWhitelist getReceivingWhitelist() { - return receivingWhitelist; - } - - @Override - public ListeningWhitelist getSendingWhitelist() { - return sendingWhitelist; - } - - @Override - public Plugin getPlugin() { - return plugin; - } - - /** - * Retrieves the name of the plugin that has been associated with the listener. - * @return Name of the associated plugin. - */ - 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(); - } - } - - @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); - } -} +/* + * ProtocolLib - Bukkit server library that allows access to the Minecraft protocol. + * Copyright (C) 2012 Kristian S. Stangeland + * + * 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 + * 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; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * 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; + * if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA + * 02111-1307 USA + */ + +package com.comphenix.protocol.events; + +import java.util.Set; + +import org.bukkit.plugin.Plugin; + +/** + * Represents a packet listener with useful constructors. + * + * @author Kristian + */ +public abstract class PacketAdapter implements PacketListener { + + protected Plugin plugin; + 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. + * @param packets - the packet IDs the listener is looking for. + */ + 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. + * @param listenerPriority - the event priority. + * @param packets - the packet IDs the listener is looking for. + */ + public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Set packets) { + this(plugin, connectionSide, listenerPriority, packets.toArray(new Integer[0])); + } + + /** + * 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. + * @param listenerPriority - the event priority. + * @param packets - the packet IDs the listener is looking for. + */ + public PacketAdapter(Plugin plugin, ConnectionSide connectionSide, ListenerPriority listenerPriority, Integer... packets) { + if (plugin == null) + throw new IllegalArgumentException("plugin cannot be null"); + if (connectionSide == null) + throw new IllegalArgumentException("connectionSide cannot be null"); + if (listenerPriority == null) + throw new IllegalArgumentException("listenerPriority cannot be null"); + if (packets == null) + throw new IllegalArgumentException("packets cannot be null"); + + // Add whitelists + if (connectionSide.isForServer()) + sendingWhitelist = new ListeningWhitelist(listenerPriority, packets); + if (connectionSide.isForClient()) + receivingWhitelist = new ListeningWhitelist(listenerPriority, packets); + + this.plugin = plugin; + this.connectionSide = connectionSide; + } + + @Override + public void onPacketReceiving(PacketEvent event) { + // Default is to do nothing + } + + @Override + public void onPacketSending(PacketEvent event) { + // And here too + } + + @Override + public ListeningWhitelist getReceivingWhitelist() { + return receivingWhitelist; + } + + @Override + public ListeningWhitelist getSendingWhitelist() { + return sendingWhitelist; + } + + @Override + public Plugin getPlugin() { + return plugin; + } + + /** + * Retrieves the name of the plugin that has been associated with the listener. + * @return Name of the associated plugin. + */ + 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(); + } + } + + @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); + } +}