diff --git a/src/main/java/us/myles/ViaVersion/ConnectionInfo.java b/src/main/java/us/myles/ViaVersion/ConnectionInfo.java index bdb0651cd..7e1dd89cb 100644 --- a/src/main/java/us/myles/ViaVersion/ConnectionInfo.java +++ b/src/main/java/us/myles/ViaVersion/ConnectionInfo.java @@ -1,5 +1,6 @@ package us.myles.ViaVersion; +import io.netty.channel.socket.SocketChannel; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import us.myles.ViaVersion.packets.State; @@ -7,12 +8,17 @@ import us.myles.ViaVersion.packets.State; import java.util.UUID; public class ConnectionInfo { + private final SocketChannel channel; private int protocol = 0; private State state = State.HANDSHAKE; private int compression = 0; private Object lastPacket; private java.util.UUID UUID; + public ConnectionInfo(SocketChannel socketChannel) { + this.channel = socketChannel; + } + public int getProtocol() { return protocol; } @@ -56,4 +62,8 @@ public class ConnectionInfo { public Player getPlayer() { return UUID == null ? null : Bukkit.getPlayer(UUID); } + + public SocketChannel getChannel() { + return channel; + } } diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaInboundHandler.java b/src/main/java/us/myles/ViaVersion/handlers/ViaInboundHandler.java index d8405501e..8423e5dc4 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaInboundHandler.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaInboundHandler.java @@ -13,11 +13,9 @@ import us.myles.ViaVersion.transformers.IncomingTransformer; @ChannelHandler.Sharable public class ViaInboundHandler extends ChannelInboundHandlerAdapter { private final IncomingTransformer incomingTransformer; - private final ViaVersionInitializer init; - public ViaInboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) { - this.init = init; - this.incomingTransformer = new IncomingTransformer(c, info, init); + public ViaInboundHandler(ConnectionInfo info) { + this.incomingTransformer = new IncomingTransformer(info); } @Override diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundHandler.java b/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundHandler.java index 79feada07..c42b00dfb 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundHandler.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundHandler.java @@ -10,11 +10,11 @@ import us.myles.ViaVersion.transformers.OutgoingTransformer; @ChannelHandler.Sharable public class ViaOutboundHandler extends ChannelOutboundHandlerAdapter { private final OutgoingTransformer outgoingTransformer; - private final ViaVersionInitializer init; + private final ConnectionInfo info; - public ViaOutboundHandler(Channel c, ConnectionInfo info, ViaVersionInitializer init) { - this.init = init; - this.outgoingTransformer = new OutgoingTransformer(c, info, init); + public ViaOutboundHandler(ConnectionInfo info) { + this.info = info; + this.outgoingTransformer = new OutgoingTransformer(info); } @Override diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundPacketHandler.java b/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundPacketHandler.java index dd3028f89..45ed2b823 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundPacketHandler.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaOutboundPacketHandler.java @@ -11,7 +11,7 @@ import java.lang.reflect.Constructor; public class ViaOutboundPacketHandler extends ChannelOutboundHandlerAdapter { private final ConnectionInfo info; - public ViaOutboundPacketHandler(Channel c, ConnectionInfo info) { + public ViaOutboundPacketHandler(ConnectionInfo info) { this.info = info; } diff --git a/src/main/java/us/myles/ViaVersion/handlers/ViaVersionInitializer.java b/src/main/java/us/myles/ViaVersion/handlers/ViaVersionInitializer.java index 5bf25b7b5..cb915227a 100644 --- a/src/main/java/us/myles/ViaVersion/handlers/ViaVersionInitializer.java +++ b/src/main/java/us/myles/ViaVersion/handlers/ViaVersionInitializer.java @@ -10,11 +10,6 @@ import java.lang.reflect.Method; public class ViaVersionInitializer extends ChannelInitializer { private final ChannelInitializer oldInit; private Method method; - private ConnectionInfo info; - private ViaInboundHandler inbound; - private ViaOutboundHandler outbound; - private SocketChannel socketChannel; - private ViaOutboundPacketHandler outbound2; public ViaVersionInitializer(ChannelInitializer oldInit) { this.oldInit = oldInit; @@ -28,24 +23,16 @@ public class ViaVersionInitializer extends ChannelInitializer { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { - info = new ConnectionInfo(); + ConnectionInfo info = new ConnectionInfo(socketChannel); // Add originals this.method.invoke(this.oldInit, socketChannel); // Add our transformers - this.socketChannel = socketChannel; - this.inbound = new ViaInboundHandler(socketChannel, info, this); - this.outbound = new ViaOutboundHandler(socketChannel, info, this); - this.outbound2 = new ViaOutboundPacketHandler(socketChannel, info); - socketChannel.pipeline().addBefore("decoder", "via_incoming", this.inbound); - socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", this.outbound2); - socketChannel.pipeline().addBefore("encoder", "via_outgoing", this.outbound); + ViaInboundHandler inbound = new ViaInboundHandler(info); + ViaOutboundHandler outbound = new ViaOutboundHandler(info); + ViaOutboundPacketHandler outbound2 = new ViaOutboundPacketHandler(info); + socketChannel.pipeline().addBefore("decoder", "via_incoming", inbound); + socketChannel.pipeline().addBefore("packet_handler", "via_outgoing2", outbound2); + socketChannel.pipeline().addBefore("encoder", "via_outgoing", outbound); } - - public void remove(){ - socketChannel.pipeline().remove("via_incoming"); - socketChannel.pipeline().remove("via_outgoing"); - socketChannel.pipeline().remove("via_outgoing2"); - } - } diff --git a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java index 26091fd9c..027ecaa0f 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/IncomingTransformer.java @@ -1,13 +1,10 @@ package us.myles.ViaVersion.transformers; import io.netty.buffer.ByteBuf; -import io.netty.channel.Channel; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import us.myles.ViaVersion.*; -import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.packets.PacketType; import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.util.PacketUtil; @@ -17,14 +14,10 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class IncomingTransformer { - private final Channel channel; private final ConnectionInfo info; - private final ViaVersionInitializer init; - public IncomingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) { - this.channel = channel; + public IncomingTransformer(ConnectionInfo info) { this.info = info; - this.init = init; } public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException { @@ -51,8 +44,10 @@ public class IncomingTransformer { PacketUtil.writeVarInt(protVer <= 102 ? protVer : 47, output); // pretend to be older if (protVer <= 102) { - // Not 1.9 remove pipes - this.init.remove(); + // not 1.9, remove pipes + info.getChannel().pipeline().remove("via_incoming"); + info.getChannel().pipeline().remove("via_outgoing"); + info.getChannel().pipeline().remove("via_outgoing2"); } String serverAddress = PacketUtil.readString(input); PacketUtil.writeString(serverAddress, output); @@ -110,7 +105,7 @@ public class IncomingTransformer { try { Class setSlot = ReflectionUtil.nms("PacketPlayOutSetSlot"); Object setSlotPacket = setSlot.getConstructors()[1].newInstance(windowID, slot, null); - channel.writeAndFlush(setSlotPacket); // slot is empty + info.getChannel().writeAndFlush(setSlotPacket); // slot is empty slot = -999; // we're evil, they'll throw item on the ground } catch (ClassNotFoundException e) { e.printStackTrace(); diff --git a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java index 41fcc441c..d5b4be08d 100644 --- a/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java +++ b/src/main/java/us/myles/ViaVersion/transformers/OutgoingTransformer.java @@ -2,9 +2,7 @@ package us.myles.ViaVersion.transformers; import com.google.gson.Gson; import com.google.gson.JsonObject; -import com.sun.xml.internal.bind.v2.runtime.reflect.Lister; import io.netty.buffer.ByteBuf; -import io.netty.channel.Channel; import org.bukkit.entity.EntityType; import org.spacehq.mc.protocol.data.game.chunk.Column; import org.spacehq.mc.protocol.util.NetUtil; @@ -12,7 +10,6 @@ import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.ConnectionInfo; import us.myles.ViaVersion.ViaVersionPlugin; import us.myles.ViaVersion.api.ViaVersion; -import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.metadata.MetaIndex; import us.myles.ViaVersion.metadata.NewType; import us.myles.ViaVersion.metadata.Type; @@ -31,18 +28,14 @@ import static us.myles.ViaVersion.util.PacketUtil.*; public class OutgoingTransformer { private static Gson gson = new Gson(); - private final Channel channel; private final ConnectionInfo info; - private final ViaVersionInitializer init; private final ViaVersionPlugin plugin = (ViaVersionPlugin) ViaVersion.getInstance(); private boolean cancel = false; private Map uuidMap = new HashMap(); private Map clientEntityTypes = new HashMap(); - public OutgoingTransformer(Channel channel, ConnectionInfo info, ViaVersionInitializer init) { - this.channel = channel; + public OutgoingTransformer(ConnectionInfo info) { this.info = info; - this.init = init; } public void transform(int packetID, ByteBuf input, ByteBuf output) throws CancelException {