From fb3f21abc6362bbed6e343ef559fb3d5d25342d1 Mon Sep 17 00:00:00 2001 From: Andrew Steinborn Date: Thu, 6 May 2021 00:48:19 -0400 Subject: [PATCH] More bitshifting magic --- .../velocitypowered/proxy/protocol/ProtocolUtils.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 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 1f696bb7a..c7e25f528 100644 --- a/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java +++ b/proxy/src/main/java/com/velocitypowered/proxy/protocol/ProtocolUtils.java @@ -120,14 +120,13 @@ public enum ProtocolUtils { * @param value the integer to write */ public static void writeVarInt(ByteBuf buf, int value) { - int bytes = varIntBytes(value); // Optimization: focus on 1-3 byte VarInts as they are the most common - if (bytes == 1) { - buf.writeByte(value & 0x7f); - } else if (bytes == 2) { + if ((value & (0xFFFFFFFF << 7)) == 0) { + buf.writeByte(value); + } else if ((value & (0xFFFFFFFF << 14)) == 0) { int w = (value & 0x7F | 0x80) << 8 | (value >>> 7); buf.writeShort(w); - } else if (bytes == 3) { + } else if ((value & (0xFFFFFFFF << 21)) == 0) { int w = (value & 0x7F | 0x80) << 16 | ((value >>> 7) & 0x7F | 0x80) << 8 | (value >>> 14); buf.writeMedium(w); } else {