diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index 0537fd83..8cc4004d 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -52,8 +52,10 @@ public final class UserConnection implements ProxiedPlayer @Getter private final Object switchMutex = new Object(); - public UserConnection(Channel channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List loginMessages) + public UserConnection(BungeeCord bungee, Channel channel, PendingConnection pendingConnection, Packet2Handshake handshake, Packet1Login forgeLogin, List loginMessages) { + this.bungee = bungee; + this.ch = channel; this.handshake = handshake; this.pendingConnection = pendingConnection; this.forgeLogin = forgeLogin; diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 6710dce2..89ec8511 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -142,7 +142,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { Preconditions.checkState( thisState == State.LOGIN, "Not expecting LOGIN" ); - UserConnection userCon = new UserConnection( socket, this, stream, handshake, forgeLogin, loginMessages ); + UserConnection userCon = new UserConnection( (BungeeCord) bungee, ch, this, handshake, forgeLogin, loginMessages ); ServerInfo server = bungee.getReconnectHandler().getServer( userCon ); userCon.connect( server ); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java b/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java index 58c68615..6aff1861 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/DefinedPacket.java @@ -1,6 +1,5 @@ package net.md_5.bungee.packet; -import com.google.common.base.Preconditions; import io.netty.buffer.ByteBuf; import io.netty.buffer.ReferenceCounted; import io.netty.buffer.Unpooled; @@ -21,26 +20,20 @@ public abstract class DefinedPacket implements ByteBuf { ByteBuf.class, ReferenceCounted.class }) - private ByteBuf out; - /** - * Packet id. - */ - public final int id; + private ByteBuf buf; - public DefinedPacket(int id, byte[] buf) + public DefinedPacket(int id, ByteBuf buf) { - out = Unpooled.wrappedBuffer( buf ); + this.buf = buf; if ( readUnsignedByte() != id ) { throw new IllegalArgumentException( "Wasn't expecting packet id " + Util.hex( id ) ); } - this.id = id; } public DefinedPacket(int id) { - out = Unpooled.buffer(); - this.id = id; + buf = Unpooled.buffer(); writeByte( id ); } @@ -90,6 +83,8 @@ public abstract class DefinedPacket implements ByteBuf public abstract void handle(PacketHandler handler) throws Exception; @SuppressWarnings("unchecked") private static Class[] classes = new Class[ 256 ]; + @SuppressWarnings("unchecked") + private static Constructor[] consructors = new Constructor[ 256 ]; public static DefinedPacket packet(ByteBuf buf) { @@ -100,7 +95,13 @@ public abstract class DefinedPacket implements ByteBuf { try { - Constructor constructor = clazz.getDeclaredConstructor( byte[].class ); + Constructor constructor = consructors[id]; + if ( constructor == null ) + { + constructor = clazz.getDeclaredConstructor( ByteBuf.class ); + consructors[id] = constructor; + } + if ( constructor != null ) { ret = constructor.newInstance( buf ); @@ -108,12 +109,8 @@ public abstract class DefinedPacket implements ByteBuf } catch ( IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException ex ) { } - } else - { - return null; } - Preconditions.checkState( ret != null, "Don't know how to deal with packet ID %s", Util.hex( id ) ); return ret; } diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet0KeepAlive.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet0KeepAlive.java index 310d99ed..4b0ba51e 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet0KeepAlive.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet0KeepAlive.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -10,9 +11,9 @@ public class Packet0KeepAlive extends DefinedPacket public int id; - public Packet0KeepAlive(byte[] buffer) + public Packet0KeepAlive(ByteBuf buf) { - super( 0x00, buffer ); + super( 0x00, buf ); id = readInt(); } diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java index 1c505a7f..b1a4ec02 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet1Login.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -28,7 +29,7 @@ public class Packet1Login extends DefinedPacket writeByte( maxPlayers ); } - public Packet1Login(byte[] buf) + public Packet1Login(ByteBuf buf) { super( 0x01, buf ); this.entityId = readInt(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet2Handshake.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet2Handshake.java index 9d3824c5..e5a54c59 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet2Handshake.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet2Handshake.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -22,7 +23,7 @@ public class Packet2Handshake extends DefinedPacket writeInt( port ); } - public Packet2Handshake(byte[] buf) + public Packet2Handshake(ByteBuf buf) { super( 0x02, buf ); this.procolVersion = readByte(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet3Chat.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet3Chat.java index 812c963e..0791b68e 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet3Chat.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet3Chat.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -16,7 +17,7 @@ public class Packet3Chat extends DefinedPacket writeString( message ); } - public Packet3Chat(byte[] buf) + public Packet3Chat(ByteBuf buf) { super( 0x03, buf ); this.message = readString(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/Packet9Respawn.java b/proxy/src/main/java/net/md_5/bungee/packet/Packet9Respawn.java index 365e485e..66231a1f 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/Packet9Respawn.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/Packet9Respawn.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -26,7 +27,7 @@ public class Packet9Respawn extends DefinedPacket writeString( levelType ); } - public Packet9Respawn(byte[] buf) + public Packet9Respawn(ByteBuf buf) { super( 0x09, buf ); this.dimension = readInt(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketC9PlayerListItem.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketC9PlayerListItem.java index b9b8f541..655b31de 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketC9PlayerListItem.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketC9PlayerListItem.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -12,9 +13,9 @@ public class PacketC9PlayerListItem extends DefinedPacket public boolean online; public int ping; - public PacketC9PlayerListItem(byte[] packet) + public PacketC9PlayerListItem(ByteBuf buf) { - super( 0xC9, packet ); + super( 0xC9, buf ); username = readString(); online = readBoolean(); ping = readShort(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java index 08bdc103..47887656 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketCDClientStatus.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -21,7 +22,7 @@ public class PacketCDClientStatus extends DefinedPacket writeByte( payload ); } - public PacketCDClientStatus(byte[] buf) + public PacketCDClientStatus(ByteBuf buf) { super( 0xCD, buf ); } diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketFAPluginMessage.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketFAPluginMessage.java index f58e6cc7..f12b0fb0 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketFAPluginMessage.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketFAPluginMessage.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -20,7 +21,7 @@ public class PacketFAPluginMessage extends DefinedPacket this.data = data; } - public PacketFAPluginMessage(byte[] buf) + public PacketFAPluginMessage(ByteBuf buf) { super( 0xFA, buf ); this.tag = readString(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketFCEncryptionResponse.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketFCEncryptionResponse.java index 0abd9d08..6670e6e5 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketFCEncryptionResponse.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketFCEncryptionResponse.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -25,7 +26,7 @@ public class PacketFCEncryptionResponse extends DefinedPacket writeArray( verifyToken ); } - public PacketFCEncryptionResponse(byte[] buf) + public PacketFCEncryptionResponse(ByteBuf buf) { super( 0xFC, buf ); this.sharedSecret = readArray(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketFDEncryptionRequest.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketFDEncryptionRequest.java index 226af9a4..fa6e63f2 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketFDEncryptionRequest.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketFDEncryptionRequest.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -23,7 +24,7 @@ public class PacketFDEncryptionRequest extends DefinedPacket this.verifyToken = verifyToken; } - public PacketFDEncryptionRequest(byte[] buf) + public PacketFDEncryptionRequest(ByteBuf buf) { super( 0xFD, buf ); serverId = readString(); diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketFEPing.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketFEPing.java index 1838a445..88e31929 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketFEPing.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketFEPing.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -8,9 +9,12 @@ import lombok.ToString; public class PacketFEPing extends DefinedPacket { - public PacketFEPing(byte[] buffer) + public byte version; + + public PacketFEPing(ByteBuf buffer) { super( 0xFE, buffer ); + version = readByte(); } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/packet/PacketFFKick.java b/proxy/src/main/java/net/md_5/bungee/packet/PacketFFKick.java index f2f816c9..7eeb12c2 100644 --- a/proxy/src/main/java/net/md_5/bungee/packet/PacketFFKick.java +++ b/proxy/src/main/java/net/md_5/bungee/packet/PacketFFKick.java @@ -1,5 +1,6 @@ package net.md_5.bungee.packet; +import io.netty.buffer.ByteBuf; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -16,7 +17,7 @@ public class PacketFFKick extends DefinedPacket writeString( message ); } - public PacketFFKick(byte[] buf) + public PacketFFKick(ByteBuf buf) { super( 0xFF, buf ); this.message = readString();