43 lines
1.3 KiB
Python
43 lines
1.3 KiB
Python
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()
|