From 99e4ab22e395afe234223e17243d7b6bd4c7da11 Mon Sep 17 00:00:00 2001 From: Myles Date: Mon, 13 Feb 2017 17:22:52 +0000 Subject: [PATCH] Only inject channels when server protocol is known #631 --- .../handlers/BukkitChannelInitializer.java | 32 +++++++++++-------- .../handlers/SpongeChannelInitializer.java | 30 ++++++++++------- 2 files changed, 37 insertions(+), 25 deletions(-) diff --git a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitChannelInitializer.java b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitChannelInitializer.java index a4529ad3a..268eaf72f 100644 --- a/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitChannelInitializer.java +++ b/bukkit/src/main/java/us/myles/ViaVersion/bukkit/handlers/BukkitChannelInitializer.java @@ -7,6 +7,7 @@ import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import us.myles.ViaVersion.bukkit.classgenerator.ClassGenerator; import us.myles.ViaVersion.bukkit.classgenerator.HandlerConstructor; @@ -33,20 +34,25 @@ public class BukkitChannelInitializer extends ChannelInitializer @Override protected void initChannel(SocketChannel socketChannel) throws Exception { - UserConnection info = new UserConnection(socketChannel); - // init protocol - new ProtocolPipeline(info); - // Add originals - this.method.invoke(this.original, socketChannel); + // Ensure ViaVersion is loaded + if (ProtocolRegistry.SERVER_PROTOCOL != -1) { + UserConnection info = new UserConnection(socketChannel); + // init protocol + new ProtocolPipeline(info); + // Add originals + this.method.invoke(this.original, socketChannel); - HandlerConstructor constructor = ClassGenerator.getConstructor(); - // Add our transformers - MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); - ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); - BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info); + HandlerConstructor constructor = ClassGenerator.getConstructor(); + // Add our transformers + MessageToByteEncoder encoder = constructor.newEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); + ByteToMessageDecoder decoder = constructor.newDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); + BukkitPacketHandler chunkHandler = new BukkitPacketHandler(info); - socketChannel.pipeline().replace("encoder", "encoder", encoder); - socketChannel.pipeline().replace("decoder", "decoder", decoder); - socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); + socketChannel.pipeline().replace("encoder", "encoder", encoder); + socketChannel.pipeline().replace("decoder", "decoder", decoder); + socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); + } else { + this.method.invoke(this.original, socketChannel); + } } } diff --git a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java index 88248d769..319f9e26e 100644 --- a/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java +++ b/sponge/src/main/java/us/myles/ViaVersion/sponge/handlers/SpongeChannelInitializer.java @@ -7,6 +7,7 @@ import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.MessageToByteEncoder; import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.protocol.ProtocolPipeline; +import us.myles.ViaVersion.api.protocol.ProtocolRegistry; import java.lang.reflect.Method; @@ -31,18 +32,23 @@ public class SpongeChannelInitializer extends ChannelInitializer @Override protected void initChannel(SocketChannel socketChannel) throws Exception { - UserConnection info = new UserConnection(socketChannel); - // init protocol - new ProtocolPipeline(info); - // Add originals - this.method.invoke(this.original, socketChannel); - // Add our transformers - MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); - ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); - SpongePacketHandler chunkHandler = new SpongePacketHandler(info); + // Ensure ViaVersion is loaded + if (ProtocolRegistry.SERVER_PROTOCOL != -1) { + UserConnection info = new UserConnection(socketChannel); + // init protocol + new ProtocolPipeline(info); + // Add originals + this.method.invoke(this.original, socketChannel); + // Add our transformers + MessageToByteEncoder encoder = new SpongeEncodeHandler(info, (MessageToByteEncoder) socketChannel.pipeline().get("encoder")); + ByteToMessageDecoder decoder = new SpongeDecodeHandler(info, (ByteToMessageDecoder) socketChannel.pipeline().get("decoder")); + SpongePacketHandler chunkHandler = new SpongePacketHandler(info); - socketChannel.pipeline().replace("encoder", "encoder", encoder); - socketChannel.pipeline().replace("decoder", "decoder", decoder); - socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); + socketChannel.pipeline().replace("encoder", "encoder", encoder); + socketChannel.pipeline().replace("decoder", "decoder", decoder); + socketChannel.pipeline().addAfter("packet_handler", "viaversion_packet_handler", chunkHandler); + } else { + this.method.invoke(this.original, socketChannel); + } } }