Decode forgotten chunk coordinates correctly

This commit is contained in:
Tanner committed 2026-11-04 04:20:06 +00:00
1 parent 30e98d7e06
commit 05941f500a
2 files changed
+41 -3

No files matched your search

+8 -3
View File
@@ -726,9 +726,11 @@ class ForgetChunkPacket(Packet):
def read(self, file_object): def read(self, file_object):
packed = Long.read(file_object) packed = Long.read(file_object)
self.x = packed >> 32 # ChunkPos stores X in the low 32 bits and Z in the high 32 bits.
z = packed & 0xFFFFFFFF raw_x = packed & 0xFFFFFFFF
self.z = z if z < 0x80000000 else z - 0x100000000 raw_z = (packed >> 32) & 0xFFFFFFFF
self.x = raw_x if raw_x < 0x80000000 else raw_x - 0x100000000
self.z = raw_z if raw_z < 0x80000000 else raw_z - 0x100000000
class PlayGameEventPacket(Packet): class PlayGameEventPacket(Packet):
@@ -1138,6 +1140,9 @@ class ModernPlayingReactor(PacketReactor):
world_state = getattr(self.connection, 'world_state', None) world_state = getattr(self.connection, 'world_state', None)
if world_state is not None: if world_state is not None:
world_state.unload_chunk(packet.x, packet.z) world_state.unload_chunk(packet.x, packet.z)
if os.getenv('PYCRAFT_TRACE_CHUNKS'):
trace('forgot chunk {},{} ({} total)'.format(
packet.x, packet.z, len(world_state.chunks)))
elif packet.packet_name == "chunk cache center": elif packet.packet_name == "chunk cache center":
world_state = getattr(self.connection, 'world_state', None) world_state = getattr(self.connection, 'world_state', None)
if world_state is not None: if world_state is not None:
+33
View File
@@ -0,0 +1,33 @@
import struct
import unittest
from io import BytesIO
from minecraft.networking.modern import ForgetChunkPacket
class TestForgetChunkPacket(unittest.TestCase):
def test_unpacks_signed_chunk_coordinates_in_x_low_z_high_order(self):
x, z = -9, -18
packed = ((z & 0xFFFFFFFF) << 32) | (x & 0xFFFFFFFF)
if packed >= 1 << 63:
packed -= 1 << 64
packet = ForgetChunkPacket()
packet.read(BytesIO(struct.pack('>q', packed)))
self.assertEqual((packet.x, packet.z), (x, z))
def test_unpacks_asymmetric_positive_and_negative_coordinates(self):
x, z = 12, -37
packed = ((z & 0xFFFFFFFF) << 32) | (x & 0xFFFFFFFF)
if packed >= 1 << 63:
packed -= 1 << 64
packet = ForgetChunkPacket()
packet.read(BytesIO(struct.pack('>q', packed)))
self.assertEqual((packet.x, packet.z), (x, z))
if __name__ == '__main__':
unittest.main()