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,20 +203,22 @@ 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) {
ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer(); // Packet large enough
Utils.writeVarIntBuf(compressedBuf, dataLength); ByteBuf compressedBuf = directBuffer ? BufUtils.getBuffer(true) : Unpooled.buffer();
final Deflater deflater = DEFLATER.get(); Utils.writeVarIntBuf(compressedBuf, dataLength);
compress(deflater, null, packetBuf, compressedBuf); final Deflater deflater = DEFLATER.get();
packetBuf = compressedBuf; compress(deflater, null, packetBuf, compressedBuf);
} else { packetBuf = compressedBuf;
// No compression } else {
ByteBuf uncompressedLengthBuffer = Unpooled.buffer(); // Packet too small
Utils.writeVarIntBuf(uncompressedLengthBuffer, 0); ByteBuf uncompressedLengthBuffer = Unpooled.buffer();
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf); Utils.writeVarIntBuf(uncompressedLengthBuffer, 0);
packetBuf = Unpooled.wrappedBuffer(uncompressedLengthBuffer, packetBuf);
}
} }
// Write the final length of the packet // Write the final length of the packet