diff --git a/minecraft/networking/modern.py b/minecraft/networking/modern.py index 2a4e7bf..5bf123f 100644 --- a/minecraft/networking/modern.py +++ b/minecraft/networking/modern.py @@ -318,6 +318,18 @@ class BlockEntityDataPacket(Packet): self.data = nbt_plain(read_anonymous_nbt(file_object)) +class BlockEventPacket(Packet): + """Clientbound block event (chest viewer-count updates, etc.).""" + id = 0x07 + packet_name = "block event" + + def read(self, file_object): + self.location = Position.read_with_context(file_object, self.context) + self.action = UnsignedByte.read(file_object) + self.parameter = UnsignedByte.read(file_object) + self.block_id = VarInt.read(file_object) + + class BlockUpdatePacket(Packet): id = 0x08 packet_name = "block update" @@ -1226,7 +1238,7 @@ class ModernPlayingReactor(PacketReactor): ClientboundChunkBatchFinished, ClientboundPlayerChatPacket, EntityAnimationPacket, EntitySwingAnimationPacket, BlockChangedAckPacket, AddEntityPacket, SetEntityDataPacket, - BlockEntityDataPacket, RemoveEntitiesPacket, + BlockEntityDataPacket, BlockEventPacket, RemoveEntitiesPacket, MoveEntityPositionPacket, MoveEntityPositionRotationPacket, MoveEntityRotationPacket, EntityPositionSyncPacket, PlayerInfoUpdatePacket, diff --git a/tests/test_block_event.py b/tests/test_block_event.py new file mode 100644 index 0000000..c8fa3f2 --- /dev/null +++ b/tests/test_block_event.py @@ -0,0 +1,42 @@ +import io +import unittest + +from minecraft.networking.modern import BlockEventPacket, ModernPlayingReactor + + +def encode_varint(value): + encoded = bytearray() + while value & ~0x7F: + encoded.append((value & 0x7F) | 0x80) + value >>= 7 + encoded.append(value) + return bytes(encoded) + + +class BlockEventPacketTests(unittest.TestCase): + def test_decodes_chest_viewer_event_and_is_registered(self): + x, y, z = -12, 68, 45 + packed_position = ((x & 0x3FFFFFF) << 38 | + (z & 0x3FFFFFF) << 12 | (y & 0xFFF)) + payload = (packed_position.to_bytes(8, 'big') + + bytes((1, 2)) + encode_varint(17)) + + class Context: + @staticmethod + def protocol_later_eq(_version): + return True + + packet = BlockEventPacket(context=Context()) + packet.read(io.BytesIO(payload)) + + self.assertEqual((packet.location.x, packet.location.y, + packet.location.z), (x, y, z)) + self.assertEqual(packet.action, 1) + self.assertEqual(packet.parameter, 2) + self.assertEqual(packet.block_id, 17) + self.assertIn(BlockEventPacket, + ModernPlayingReactor.get_clientbound_packets(None)) + + +if __name__ == '__main__': + unittest.main()