Decode clientbound block events
This commit is contained in:
1 parent
a48b366b09
commit
d6505489d5
2 files changed
+55
-1
No files matched your search
@@ -318,6 +318,18 @@ class BlockEntityDataPacket(Packet):
|
|||||||
self.data = nbt_plain(read_anonymous_nbt(file_object))
|
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):
|
class BlockUpdatePacket(Packet):
|
||||||
id = 0x08
|
id = 0x08
|
||||||
packet_name = "block update"
|
packet_name = "block update"
|
||||||
@@ -1226,7 +1238,7 @@ class ModernPlayingReactor(PacketReactor):
|
|||||||
ClientboundChunkBatchFinished, ClientboundPlayerChatPacket,
|
ClientboundChunkBatchFinished, ClientboundPlayerChatPacket,
|
||||||
EntityAnimationPacket, EntitySwingAnimationPacket,
|
EntityAnimationPacket, EntitySwingAnimationPacket,
|
||||||
BlockChangedAckPacket, AddEntityPacket, SetEntityDataPacket,
|
BlockChangedAckPacket, AddEntityPacket, SetEntityDataPacket,
|
||||||
BlockEntityDataPacket, RemoveEntitiesPacket,
|
BlockEntityDataPacket, BlockEventPacket, RemoveEntitiesPacket,
|
||||||
MoveEntityPositionPacket,
|
MoveEntityPositionPacket,
|
||||||
MoveEntityPositionRotationPacket, MoveEntityRotationPacket,
|
MoveEntityPositionRotationPacket, MoveEntityRotationPacket,
|
||||||
EntityPositionSyncPacket, PlayerInfoUpdatePacket,
|
EntityPositionSyncPacket, PlayerInfoUpdatePacket,
|
||||||
|
|||||||
@@ -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()
|
||||||
Reference in new issue
Block a user