diff --git a/minecraft/networking/modern.py b/minecraft/networking/modern.py index b69431d..6eb7355 100644 --- a/minecraft/networking/modern.py +++ b/minecraft/networking/modern.py @@ -28,6 +28,11 @@ def read_signed_varint(file_object): return value - (1 << 32) if value & (1 << 31) else value +def sign_extend(value, bits): + sign_bit = 1 << (bits - 1) + return value - (1 << bits) if value & sign_bit else value + + def nbt_plain(value): raw = getattr(value, 'value', value) if hasattr(raw, 'items'): @@ -285,16 +290,18 @@ class SectionBlocksUpdatePacket(Packet): def read(self, file_object): packed_section = Long.read(file_object) - section_x = packed_section >> 42 - section_y = packed_section << 44 >> 44 - section_z = packed_section << 22 >> 42 + section_x = sign_extend((packed_section >> 42) & 0x3fffff, 22) + section_z = sign_extend((packed_section >> 20) & 0x3fffff, 22) + section_y = sign_extend(packed_section & 0xfffff, 20) self.changes = [] for _ in range(VarInt.read(file_object)): packed_change = VarLong.read(file_object) local = packed_change & 0xFFF + # Multi-block records pack local coordinates as x:z:y, unlike + # the x:y:z order used by the chunk-array linear index. x = (section_x << 4) + ((local >> 8) & 0xF) - y = (section_y << 4) + ((local >> 4) & 0xF) - z = (section_z << 4) + (local & 0xF) + z = (section_z << 4) + ((local >> 4) & 0xF) + y = (section_y << 4) + (local & 0xF) self.changes.append((x, y, z, packed_change >> 12))