From 29d71e05e4d44e92c095df01fdb9c3cbe59697d3 Mon Sep 17 00:00:00 2001 From: "Kristian S. Stangeland" Date: Thu, 5 Dec 2013 21:27:22 +0100 Subject: [PATCH] Properly cancel written packets. --- .../com/comphenix/protocol/PacketType.java | 11 +++++++- .../protocol/events/PacketAdapter.java | 26 +++++++++++++++++++ .../protocol/injector/netty/ChannelProxy.java | 7 ++--- .../injector/packet/PacketRegistry.java | 2 +- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java index 93ca6607..0bdb64f2 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java @@ -9,6 +9,7 @@ import java.util.concurrent.Future; import org.bukkit.Bukkit; +import com.comphenix.protocol.events.ConnectionSide; import com.comphenix.protocol.injector.packet.PacketRegistry; import com.comphenix.protocol.reflect.ObjectEnum; import com.comphenix.protocol.utility.MinecraftVersion; @@ -376,7 +377,15 @@ public class PacketType implements Serializable { /** * Indicate that packets of this type will be sent by the current server. */ - SERVER + SERVER; + + /** + * Retrieve the equivialent connection side. + * @return The connection side. + */ + public ConnectionSide toSide() { + return this == CLIENT ? ConnectionSide.CLIENT_SIDE : ConnectionSide.SERVER_SIDE; + } } // Lookup of packet types 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 cc360515..a44d87e4 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/events/PacketAdapter.java @@ -55,6 +55,26 @@ public abstract class PacketAdapter implements PacketListener { ); } + /** + * Initialize a packet listener with the given parameters. + * @param plugin - the plugin. + * @param listenerPriority - the priority. + * @param types - the packet types. + */ + public PacketAdapter(Plugin plugin, PacketType... types) { + this(plugin, ListenerPriority.NORMAL, types); + } + + /** + * Initialize a packet listener with the given parameters. + * @param plugin - the plugin. + * @param listenerPriority - the priority. + * @param types - the packet types. + */ + public PacketAdapter(Plugin plugin, ListenerPriority listenerPriority, PacketType... types) { + this(params(plugin, types).listenerPriority(listenerPriority)); + } + /** * Initialize a packet listener with default priority. *

@@ -500,6 +520,12 @@ public abstract class PacketAdapter implements PacketListener { * @return This builder, for chaining. */ public AdapterParameteters types(@Nonnull PacketType... packets) { + // Set the connection side as well + if (connectionSide == null) { + for (PacketType type : packets) { + this.connectionSide = ConnectionSide.add(this.connectionSide, type.getSender().toSide()); + } + } this.packets = Preconditions.checkNotNull(packets, "packets cannot be NULL"); return this; } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelProxy.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelProxy.java index 801b1168..e2244e55 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelProxy.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/netty/ChannelProxy.java @@ -45,14 +45,15 @@ abstract class ChannelProxy implements Channel { (Class) ChannelProxy.class.getClassLoader(). loadClass("net.minecraft.util.io.netty.channel.SucceededChannelFuture"); - FUTURE_CONSTRUCTOR = succededFuture.getConstructor(Channel.class, EventExecutor.class); + FUTURE_CONSTRUCTOR = succededFuture.getDeclaredConstructor(Channel.class, EventExecutor.class); + FUTURE_CONSTRUCTOR.setAccessible(true); } return FUTURE_CONSTRUCTOR.newInstance(this, null); } catch (ClassNotFoundException e) { - throw new RuntimeException("Cannot get succeeded future."); + throw new RuntimeException("Cannot get succeeded future.", e); } catch (Exception e) { - throw new RuntimeException("Cannot construct completed future."); + throw new RuntimeException("Cannot construct completed future.", e); } } diff --git a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java index 2c80b692..32f54e86 100644 --- a/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java +++ b/ProtocolLib/src/main/java/com/comphenix/protocol/injector/packet/PacketRegistry.java @@ -298,7 +298,7 @@ public class PacketRegistry { * @return Set of packet types. */ public static Set toPacketTypes(Set ids) { - return toPacketTypes(ids); + return toPacketTypes(ids, null); } /**