From 7e932eaad4ca0d0c21833cb4f65368fbe5f87d4c Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Fri, 27 Oct 2023 15:35:04 -0400 Subject: [PATCH] Modify `MinecraftCompressorAndLengthEncoder` to handle 8MiB outgoing packets like vanilla supports now --- .../velocitypowered/proxy/protocol/ProtocolUtils.java | 11 ++++++----- .../netty/MinecraftCompressorAndLengthEncoder.java | 10 +++++----- .../proxy/protocol/ProtocolUtilsTest.java | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) 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 e65ee056b..23f9de87c 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -163,16 +163,17 @@ public enum ProtocolUtils { } /** - * Writes the specified {@code value} as a 21-bit Minecraft VarInt to the specified {@code buf}. - * The upper 11 bits will be discarded. + * Writes the specified {@code value} as a 28-bit Minecraft VarInt to the specified {@code buf}. + * The upper 4 bits will be discarded. * * @param buf the buffer to read from * @param value the integer to write */ - public static void write21BitVarInt(ByteBuf buf, int value) { + public static void write28BitVarInt(ByteBuf buf, int value) { // See https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/ - int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14); - buf.writeMedium(w); + int w = (value & 0x7F | 0x80) << 24 | (((value >>> 7) & 0x7F | 0x80) << 16) + | ((value >>> 14) & 0x7F | 0x80) << 8 | (value >>> 21); + buf.writeInt(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 90952a729..d020d93c6 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 << 21) { - throw new DataFormatException("The server sent a very large (over 2MiB compressed) packet."); + if (compressedLength >= 1 << 23) { + throw new DataFormatException("The server sent a very large (over 8MiB compressed) packet."); } int writerIndex = out.writerIndex(); int packetLength = out.readableBytes() - 3; out.writerIndex(0); - ProtocolUtils.write21BitVarInt(out, packetLength); // Rewrite packet length + ProtocolUtils.write28BitVarInt(out, packetLength); // Rewrite packet length out.writerIndex(writerIndex); } @@ -92,7 +92,7 @@ public class MinecraftCompressorAndLengthEncoder extends MessageToByteEncoder