PacketUtils#writeFramedPacket cleanup

This commit is contained in:
TheMode 2021-05-10 05:47:14 +02:00
parent 653417be75
commit b96386cee5

View File

@ -223,11 +223,12 @@ public final class PacketUtils {
public static void writeFramedPacket(@NotNull ByteBuf buffer, public static void writeFramedPacket(@NotNull ByteBuf buffer,
@NotNull ServerPacket serverPacket) { @NotNull ServerPacket serverPacket) {
final int compressionThreshold = MinecraftServer.getCompressionThreshold(); final int compressionThreshold = MinecraftServer.getCompressionThreshold();
final boolean compression = compressionThreshold > 0; final boolean compressionEnabled = compressionThreshold > 0;
if (compression) { // Index of the var-int containing the complete packet length
// Dummy var-int final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
final int packetLengthIndex = Utils.writeEmptyVarIntHeader(buffer); if (compressionEnabled) {
// Index of the uncompressed payload length
final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer); final int dataLengthIndex = Utils.writeEmptyVarIntHeader(buffer);
// Write packet // Write packet
@ -258,18 +259,12 @@ public final class PacketUtils {
Utils.overrideVarIntHeader(buffer, dataLengthIndex, 0); // -> Uncompressed Utils.overrideVarIntHeader(buffer, dataLengthIndex, 0); // -> Uncompressed
} }
} else { } else {
// No compression // No compression, write packet id + payload
// Write dummy var-int
final int index = Utils.writeEmptyVarIntHeader(buffer);
// Write packet id + payload
writePacket(buffer, serverPacket); writePacket(buffer, serverPacket);
// Rewrite dummy var-int to packet length // Rewrite dummy var-int to packet length
final int afterIndex = buffer.writerIndex(); final int packetSize = (buffer.writerIndex() - packetLengthIndex) - Utils.VARINT_HEADER_SIZE;
final int packetSize = (afterIndex - index) - Utils.VARINT_HEADER_SIZE; Utils.overrideVarIntHeader(buffer, packetLengthIndex, packetSize);
Utils.overrideVarIntHeader(buffer, index, packetSize);
} }
} }