61 lines
2.0 KiB
Python
61 lines
2.0 KiB
Python
import io
|
|
import unittest
|
|
|
|
from minecraft.networking.modern import (
|
|
BlockEntityDataPacket, ModernPlayingReactor, SetEntityDataPacket,
|
|
)
|
|
|
|
|
|
def encode_varint(value):
|
|
encoded = bytearray()
|
|
while value & ~0x7F:
|
|
encoded.append((value & 0x7F) | 0x80)
|
|
value >>= 7
|
|
encoded.append(value)
|
|
return bytes(encoded)
|
|
|
|
|
|
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
|
|
payload = (
|
|
encode_varint(entity_id) +
|
|
bytes((8,)) + encode_varint(7) +
|
|
encode_varint(1) + encode_varint(item_id) +
|
|
encode_varint(0) + encode_varint(0) +
|
|
bytes((0xFF,))
|
|
)
|
|
packet = SetEntityDataPacket()
|
|
packet.read(io.BytesIO(payload))
|
|
|
|
self.assertEqual(packet.entity_id, entity_id)
|
|
self.assertEqual(packet.item_stacks[0].item_id, item_id)
|
|
self.assertEqual(packet.item_stacks[0].item_count, 1)
|
|
self.assertIn(SetEntityDataPacket,
|
|
ModernPlayingReactor.get_clientbound_packets(None))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|