diff --git a/minecraft/networking/modern.py b/minecraft/networking/modern.py index 149ae45..09d65e5 100644 --- a/minecraft/networking/modern.py +++ b/minecraft/networking/modern.py @@ -726,9 +726,11 @@ class ForgetChunkPacket(Packet): def read(self, file_object): packed = Long.read(file_object) - self.x = packed >> 32 - z = packed & 0xFFFFFFFF - self.z = z if z < 0x80000000 else z - 0x100000000 + # ChunkPos stores X in the low 32 bits and Z in the high 32 bits. + raw_x = packed & 0xFFFFFFFF + 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): @@ -1138,6 +1140,9 @@ class ModernPlayingReactor(PacketReactor): world_state = getattr(self.connection, 'world_state', None) if world_state is not None: 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": world_state = getattr(self.connection, 'world_state', None) if world_state is not None: diff --git a/tests/test_modern_chunk_packets.py b/tests/test_modern_chunk_packets.py new file mode 100644 index 0000000..4448c6e --- /dev/null +++ b/tests/test_modern_chunk_packets.py @@ -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()