From db714f94905bee3516de03bbc21e1ed4914d162a Mon Sep 17 00:00:00 2001 From: joo Date: Sun, 27 May 2018 17:12:50 +0100 Subject: [PATCH] Fix: MapPacket.write_fields() is incorrect. --- .../packets/clientbound/play/map_packet.py | 15 ++++++++--- tests/test_packets_with_logic.py | 27 +++++++++++-------- tests/test_serialization.py | 8 ++++-- 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/map_packet.py b/minecraft/networking/packets/clientbound/play/map_packet.py index ca271d6..11f2626 100644 --- a/minecraft/networking/packets/clientbound/play/map_packet.py +++ b/minecraft/networking/packets/clientbound/play/map_packet.py @@ -114,11 +114,20 @@ class MapPacket(Packet): VarInt.send(len(self.icons), packet_buffer) for icon in self.icons: - type_and_direction = (icon.direction << 4) & 0xF0 - type_and_direction |= (icon.type & 0xF) - UnsignedByte.send(type_and_direction, packet_buffer) + if self.context.protocol_version >= 373: + VarInt.send(icon.type, packet_buffer) + else: + type_and_direction = (icon.type << 4) & 0xF0 + type_and_direction |= (icon.direction & 0xF) + UnsignedByte.send(type_and_direction, packet_buffer) Byte.send(icon.location[0], packet_buffer) Byte.send(icon.location[1], packet_buffer) + if self.context.protocol_version >= 373: + UnsignedByte.send(icon.direction, packet_buffer) + if self.context.protocol_version >= 364: + Boolean.send(icon.display_name is not None, packet_buffer) + if icon.display_name is not None: + String.send(icon.display_name, packet_buffer) UnsignedByte.send(self.width, packet_buffer) if self.width: diff --git a/tests/test_packets_with_logic.py b/tests/test_packets_with_logic.py index 8fb3697..14e57d6 100644 --- a/tests/test_packets_with_logic.py +++ b/tests/test_packets_with_logic.py @@ -45,26 +45,28 @@ class MapPacketTest(unittest.TestCase): @staticmethod def make_map_packet( context, width=2, height=2, offset=(2, 2), pixels=b"this"): + packet = MapPacket(context) packet.map_id = 1 packet.scale = 42 packet.is_tracking_position = True packet.icons = [] - packet.icons.append( - MapPacket.MapIcon(type=2, direction=2, location=(1, 1)) - ) - packet.icons.append( - MapPacket.MapIcon(type=3, direction=3, location=(3, 3)) - ) + d_name = u'Marshmallow' if context.protocol_version >= 364 else None + packet.icons.append(MapPacket.MapIcon( + type=2, direction=2, location=(1, 1), display_name=d_name + )) + packet.icons.append(MapPacket.MapIcon( + type=3, direction=3, location=(3, 3) + )) packet.width = width - packet.height = height - packet.offset = offset - packet.pixels = pixels + packet.height = height if width else 0 + packet.offset = offset if width else None + packet.pixels = pixels if width else None return packet - def packet_roundtrip(self, context): - packet = self.make_map_packet(context) + def packet_roundtrip(self, context, **kwds): + packet = self.make_map_packet(context, **kwds) packet_buffer = PacketBuffer() packet.write(packet_buffer) @@ -93,6 +95,9 @@ class MapPacketTest(unittest.TestCase): def test_packet_roundtrip(self): self.packet_roundtrip(ConnectionContext(protocol_version=106)) self.packet_roundtrip(ConnectionContext(protocol_version=107)) + self.packet_roundtrip(ConnectionContext(protocol_version=379)) + self.packet_roundtrip(ConnectionContext(protocol_version=379), + width=0) def test_map_set(self): map_set = MapPacket.MapSet() diff --git a/tests/test_serialization.py b/tests/test_serialization.py index aaa6c69..989f5b4 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -4,7 +4,8 @@ import unittest from minecraft.networking.types import ( Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray, - VarIntPrefixedByteArray, UUID, String as StringType + VarIntPrefixedByteArray, UUID, String as StringType, Position, + TrailingByteArray, UnsignedLong, ) from minecraft.networking.packets import PacketBuffer @@ -15,6 +16,7 @@ TEST_DATA = { Byte: [-22, 22], Short: [-340, 22, 350], UnsignedShort: [0, 400], + UnsignedLong: [0, 400], Integer: [-1000, 1000], VarInt: [1, 250, 50000, 10000000], Long: [50000000], @@ -22,8 +24,10 @@ TEST_DATA = { Double: [36.004002], ShortPrefixedByteArray: [bytes(245)], VarIntPrefixedByteArray: [bytes(1234)], + TrailingByteArray: [b'Q^jO<5*|+o LGc('], UUID: ["12345678-1234-5678-1234-567812345678"], - StringType: ["hello world"] + StringType: ["hello world"], + Position: [(758, 0, 691)], }