From a48b366b094acd6fb1d8ad5770765f48e9337d55 Mon Sep 17 00:00:00 2001 From: Tanner Date: Thu, 5 Nov 2026 23:47:05 +0000 Subject: [PATCH] Decode 26.3 block entity data packets --- minecraft/networking/modern.py | 26 +++++++++++++++++++++++++- tests/test_entity_metadata.py | 22 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/minecraft/networking/modern.py b/minecraft/networking/modern.py index 3fbe2f3..2a4e7bf 100644 --- a/minecraft/networking/modern.py +++ b/minecraft/networking/modern.py @@ -293,6 +293,29 @@ class ChunkDataPacket(Packet): data_size = VarInt.read(file_object) self.data = file_object.read(data_size) self.sections = decode_chunk_sections(self.data) + self.block_entities = [] + for _ in range(VarInt.read(file_object)): + packed_xz = UnsignedByte.read(file_object) + y = Short.read(file_object) + block_entity_type = VarInt.read(file_object) + data = nbt_plain(read_anonymous_nbt(file_object)) + self.block_entities.append({ + 'x': self.x * 16 + (packed_xz >> 4), + 'y': y, + 'z': self.z * 16 + (packed_xz & 0x0F), + 'type': block_entity_type, + 'data': data, + }) + + +class BlockEntityDataPacket(Packet): + id = 0x06 + packet_name = "block entity data" + + def read(self, file_object): + self.location = Position.read_with_context(file_object, self.context) + self.block_entity_type = VarInt.read(file_object) + self.data = nbt_plain(read_anonymous_nbt(file_object)) class BlockUpdatePacket(Packet): @@ -1203,7 +1226,8 @@ class ModernPlayingReactor(PacketReactor): ClientboundChunkBatchFinished, ClientboundPlayerChatPacket, EntityAnimationPacket, EntitySwingAnimationPacket, BlockChangedAckPacket, AddEntityPacket, SetEntityDataPacket, - RemoveEntitiesPacket, MoveEntityPositionPacket, + BlockEntityDataPacket, RemoveEntitiesPacket, + MoveEntityPositionPacket, MoveEntityPositionRotationPacket, MoveEntityRotationPacket, EntityPositionSyncPacket, PlayerInfoUpdatePacket, PlayerInfoRemovePacket, ContainerOpenPacket, diff --git a/tests/test_entity_metadata.py b/tests/test_entity_metadata.py index 14488ee..fbd7137 100644 --- a/tests/test_entity_metadata.py +++ b/tests/test_entity_metadata.py @@ -2,7 +2,7 @@ import io import unittest from minecraft.networking.modern import ( - ModernPlayingReactor, SetEntityDataPacket, + BlockEntityDataPacket, ModernPlayingReactor, SetEntityDataPacket, ) @@ -16,6 +16,26 @@ def encode_varint(value): class EntityMetadataPacketTests(unittest.TestCase): + def test_block_entity_data_packet_decodes_sign_location_and_is_registered(self): + x, y, z = 5, 64, -3 + packed_position = ((x & 0x3FFFFFF) << 38 | + (z & 0x3FFFFFF) << 12 | (y & 0xFFF)) + payload = (packed_position.to_bytes(8, 'big') + + encode_varint(7) + bytes((0,))) + class Context: + @staticmethod + def protocol_later_eq(_version): + return True + + packet = BlockEntityDataPacket(context=Context()) + packet.read(io.BytesIO(payload)) + + self.assertEqual((packet.location.x, packet.location.y, + packet.location.z), (x, y, z)) + self.assertEqual(packet.block_entity_type, 7) + self.assertIn(BlockEntityDataPacket, + ModernPlayingReactor.get_clientbound_packets(None)) + def test_item_stack_metadata_decodes_and_is_registered(self): entity_id = 42 item_id = 86