Fix packet writing when compression is disabled

This commit is contained in:
themode 2021-03-22 19:37:16 +01:00
parent 7cd2d1b896
commit 4c3215bf1b

View File

@ -203,21 +203,23 @@ public final class PacketUtils {
final int dataLength = packetBuf.readableBytes(); final int dataLength = packetBuf.readableBytes();
final int compressionThreshold = MinecraftServer.getCompressionThreshold(); final int compressionThreshold = MinecraftServer.getCompressionThreshold();
final boolean compression = compressionThreshold > 0 && dataLength >= compressionThreshold; final boolean compression = compressionThreshold > 0;
if (compression) { if (compression) {
// Compression if (dataLength >= compressionThreshold) {
// Packet large enough
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer(); ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
Utils.writeVarIntBuf(compressedBuf, dataLength); Utils.writeVarIntBuf(compressedBuf, dataLength);
final Deflater deflater = DEFLATER.get(); final Deflater deflater = DEFLATER.get();
compress(deflater, null, packetBuf, compressedBuf); compress(deflater, null, packetBuf, compressedBuf);
packetBuf = compressedBuf; packetBuf = compressedBuf;
} else { } else {
// No compression // Packet too small
ByteBuf uncompressedLengthBuffer = Unpooled.buffer(); ByteBuf uncompressedLengthBuffer = Unpooled.buffer();
Utils.writeVarIntBuf(uncompressedLengthBuffer, 0); Utils.writeVarIntBuf(uncompressedLengthBuffer, 0);
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf); packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf);
} }
}
// Write the final length of the packet // Write the final length of the packet
ByteBuf packetLengthBuffer = Unpooled.buffer(); ByteBuf packetLengthBuffer = Unpooled.buffer();