diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java index 23f9de87c..e65ee056b 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -163,17 +163,16 @@ public enum ProtocolUtils { } /** - * Writes the specified {@code value} as a 28-bit Minecraft VarInt to the specified {@code buf}. - * The upper 4 bits will be discarded. + * Writes the specified {@code value} as a 21-bit Minecraft VarInt to the specified {@code buf}. + * The upper 11 bits will be discarded. * * @param buf the buffer to read from * @param value the integer to write */ - public static void write28BitVarInt(ByteBuf buf, int value) { + public static void write21BitVarInt(ByteBuf buf, int value) { // See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/ - int w = (value & 0x7F | 0x80) << 24 | (((value >>> 7) & 0x7F | 0x80) << 16) - | ((value >>> 14) & 0x7F | 0x80) << 8 | (value >>> 21); - buf.writeInt(w); + int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14); + buf.writeMedium(w); } public static String readString(ByteBuf buf) { diff --git a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressorAndLengthEncoder.java b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressorAndLengthEncoder.java index d020d93c6..90952a729 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressorAndLengthEncoder.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/netty/MinecraftCompressorAndLengthEncoder.java @@ -57,7 +57,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder= 1 << 23) { - throw new DataFormatException("The server sent a very large (over 8MiB compressed) packet."); + if (compressedLength >= 1 << 21) { + throw new DataFormatException("The server sent a very large (over 2MiB compressed) packet."); } int writerIndex = out.writerIndex(); int packetLength = out.readableBytes() - 3; out.writerIndex(0); - ProtocolUtils.write28BitVarInt(out, packetLength); // Rewrite packet length + ProtocolUtils.write21BitVarInt(out, packetLength); // Rewrite packet length out.writerIndex(writerIndex); } @@ -92,7 +92,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder