From b96386cee5dc9180aa25af9139e19f0d27beffd7 Mon Sep 17 00:00:00 2001 From: TheMode Date: Mon, 10 May 2021 05:47:14 +0200 Subject: [PATCH] PacketUtils#writeFramedPacket cleanup --- .../minestom/server/utils/PacketUtils.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 20dc2cad2..2705adeb4 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -223,11 +223,12 @@ public final class PacketUtils { public static void writeFramedPacket(@NotNull ByteBuf buffer, @NotNull ServerPacket serverPacket) { final int compressionThreshold = MinecraftServer.getCompressionThreshold(); - final boolean compression = compressionThreshold > 0; + final boolean compressionEnabled = compressionThreshold > 0; - if (compression) { - // Dummy var-int - final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer); + // Index of the var-int containing the complete packet length + final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer); + if (compressionEnabled) { + // Index of the uncompressed payload length final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer); // Write packet @@ -258,18 +259,12 @@ public final class PacketUtils { Utils.overrideVarIntHeader(buffer, dataLengthIndex, 0); // -> Uncompressed } } else { - // No compression - - // Write dummy var-int - final int index = Utils.writeEmptyVarIntHeader(buffer); - - // Write packet id + payload + // No compression, write packet id + payload writePacket(buffer, serverPacket); // Rewrite dummy var-int to packet length - final int afterIndex = buffer.writerIndex(); - final int packetSize = (afterIndex - index) - Utils.VARINT_HEADER_SIZE; - Utils.overrideVarIntHeader(buffer, index, packetSize); + final int packetSize = (buffer.writerIndex() - packetLengthIndex) - Utils.VARINT_HEADER_SIZE; + Utils.overrideVarIntHeader(buffer, packetLengthIndex, packetSize); } }