From 52c0671f4f6b774863d9a0169f787506fcf61d88 Mon Sep 17 00:00:00 2001 From: joo Date: Sat, 19 May 2018 18:04:48 +0100 Subject: [PATCH] Add support for Minecraft snapshots 18w03a-18w20c (protocol 354-377). --- minecraft/__init__.py | 24 +++++++++ .../clientbound/play/block_change_packet.py | 3 -- .../packets/clientbound/play/map_packet.py | 52 +++++++++---------- minecraft/networking/packets/packet.py | 2 +- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/minecraft/__init__.py b/minecraft/__init__.py index e6adbb6..eafaae4 100644 --- a/minecraft/__init__.py +++ b/minecraft/__init__.py @@ -81,6 +81,30 @@ SUPPORTED_MINECRAFT_VERSIONS = { '17w50a': 351, '18w01a': 352, '18w02a': 353, + '18w03a': 354, + '18w03b': 355, + '18w05a': 356, + '18w06a': 357, + '18w07a': 358, + '18w07b': 359, + '18w07c': 360, + '18w08a': 361, + '18w08b': 362, + '18w09a': 363, + '18w10a': 364, + '18w10b': 365, + '18w10c': 366, + '18w10d': 367, + '18w11a': 368, + '18w14a': 369, + '18w14b': 370, + '18w15a': 371, + '18w16a': 372, + '18w19a': 373, + '18w19b': 374, + '18w20a': 375, + '18w20b': 376, + '18w20c': 377, } SUPPORTED_PROTOCOL_VERSIONS = \ diff --git a/minecraft/networking/packets/clientbound/play/block_change_packet.py b/minecraft/networking/packets/clientbound/play/block_change_packet.py index 94bbdef..381168b 100644 --- a/minecraft/networking/packets/clientbound/play/block_change_packet.py +++ b/minecraft/networking/packets/clientbound/play/block_change_packet.py @@ -54,9 +54,6 @@ class MultiBlockChangePacket(Packet): self.y = y_coordinate self.z = (horizontal_position & 0x0F) - def __str__(self): - return self.__repr__() - @classmethod def get_subclass(cls, context): return MultiBlockChangePacket.OpaqueRecord \ diff --git a/minecraft/networking/packets/clientbound/play/map_packet.py b/minecraft/networking/packets/clientbound/play/map_packet.py index d37b62d..65b66bb 100644 --- a/minecraft/networking/packets/clientbound/play/map_packet.py +++ b/minecraft/networking/packets/clientbound/play/map_packet.py @@ -3,7 +3,7 @@ from minecraft.networking.packets import ( ) from minecraft.networking.types import ( - VarInt, Byte, Boolean, UnsignedByte, VarIntPrefixedByteArray + VarInt, Byte, Boolean, UnsignedByte, VarIntPrefixedByteArray, String ) @@ -19,19 +19,17 @@ class MapPacket(Packet): packet_name = 'map' class MapIcon(object): - __slots__ = 'type', 'direction', 'location' + __slots__ = 'type', 'direction', 'location', 'display_name' - def __init__(self, type, direction, location): + def __init__(self, type, direction, location, display_name=None): self.type = type self.direction = direction self.location = location + self.display_name = display_name def __repr__(self): - return ('MapIcon(type=%s, direction=%s, location=%s)' - % (self.type, self.direction, self.location)) - - def __str__(self): - return self.__repr__() + fs = ('%s=%r' % (at, getattr(self, at)) for at in self.__slots__) + return 'MapIcon(%s)' % ', '.join(fs) class Map(object): __slots__ = ('id', 'scale', 'icons', 'pixels', 'width', 'height', @@ -47,11 +45,8 @@ class MapPacket(Packet): self.is_tracking_position = True def __repr__(self): - return ('Map(id=%s, scale=%s, icons=%s, width=%s, height=%s)' % ( - self.id, self.scale, self.icons, self.width, self.height)) - - def __str__(self): - return self.__repr__() + fs = ('%s=%r' % (at, getattr(self, at)) for at in self.__slots__) + return 'Map(%s)' % ', '.join(fs) class MapSet(object): __slots__ = 'maps_by_id' @@ -60,12 +55,9 @@ class MapPacket(Packet): self.maps_by_id = dict() def __repr__(self): - maps = [str(map) for map in self.maps_by_id.values()] + maps = (str(map) for map in self.maps_by_id.values()) return 'MapSet(%s)' % ', '.join(maps) - def __str__(self): - return self.__repr__() - def read(self, file_object): self.map_id = VarInt.read(file_object) self.scale = Byte.read(file_object) @@ -78,11 +70,22 @@ class MapPacket(Packet): icon_count = VarInt.read(file_object) self.icons = [] for i in range(icon_count): - type, direction = divmod(UnsignedByte.read(file_object), 16) + if self.context.protocol_version >= 373: + type = VarInt.read(file_object) + else: + type, direction = divmod(UnsignedByte.read(file_object), 16) x = Byte.read(file_object) z = Byte.read(file_object) - icon = MapPacket.MapIcon(type, direction, (x, z)) + if self.context.protocol_version >= 373: + direction = UnsignedByte.read(file_object) + if self.context.protocol_version >= 364: + has_name = Boolean.read(file_object) + display_name = String.read(file_object) if has_name else None + else: + display_name = None + icon = MapPacket.MapIcon(type, direction, (x, z), display_name) self.icons.append(icon) + self.width = UnsignedByte.read(file_object) if self.width: self.height = UnsignedByte.read(file_object) @@ -140,10 +143,7 @@ class MapPacket(Packet): self._write_buffer(socket, packet_buffer, compression_threshold) def __repr__(self): - return 'MapPacket(%s)' % ', '.join( - '%s=%r' % (k, v) - for (k, v) in self.__dict__.items() - if k != 'pixels') - - def __str__(self): - return self.__repr__() + return '%sMapPacket(%s)' % ( + ('0x%02X ' % self.id) if self.id is not None else '', + ', '.join('%s=%r' % (k, v) for (k, v) in self.__dict__.items() + if k not in ('pixels', '_context', 'id', 'definition'))) diff --git a/minecraft/networking/packets/packet.py b/minecraft/networking/packets/packet.py index e6acbf1..ace3224 100644 --- a/minecraft/networking/packets/packet.py +++ b/minecraft/networking/packets/packet.py @@ -102,7 +102,7 @@ class Packet(object): self._write_buffer(socket, packet_buffer, compression_threshold) - def __str__(self): + def __repr__(self): str = type(self).__name__ if self.id is not None: str = '0x%02X %s' % (self.id, str)