From d7b560a9f428f9e79444a204c7cf2e57eaff0433 Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 May 2019 01:28:56 +0100 Subject: [PATCH 01/13] Implement FacePlayerPacket Called when using the teleport chat command and you specify a facing parameter. `/teleport [] facing` --- .../packets/clientbound/play/__init__.py | 5 ++ .../clientbound/play/face_player_packet.py | 60 +++++++++++++++++++ minecraft/networking/types/enum.py | 9 ++- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 minecraft/networking/packets/clientbound/play/face_player_packet.py diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 7d31b44..5e1137e 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -15,6 +15,7 @@ from .spawn_object_packet import SpawnObjectPacket from .block_change_packet import BlockChangePacket, MultiBlockChangePacket from .explosion_packet import ExplosionPacket from .sound_effect_packet import SoundEffectPacket +from . face_player_packet import FacePlayerPacket # Formerly known as state_playing_clientbound. @@ -48,6 +49,10 @@ def get_packets(context): packets |= { SoundEffectPacket, } + if context.protocol_version >= 352: + packets |= { + FacePlayerPacket + } return packets diff --git a/minecraft/networking/packets/clientbound/play/face_player_packet.py b/minecraft/networking/packets/clientbound/play/face_player_packet.py new file mode 100644 index 0000000..f2796ac --- /dev/null +++ b/minecraft/networking/packets/clientbound/play/face_player_packet.py @@ -0,0 +1,60 @@ +from minecraft.networking.types import ( + VarInt, Double, Boolean, FeetEyes +) + +from minecraft.networking.packets import Packet + + +class FacePlayerPacket(Packet): + @staticmethod + def get_id(context): + return 0x34 if context.protocol_version >= 471 else \ + 0x32 if context.protocol_version >= 451 else \ + 0x31 if context.protocol_version >= 389 else \ + 0x30 + + packet_name = 'face player' + + def read(self, file_object): + if self.context.protocol_version >= 353: + self.feet_or_eyes = VarInt.read(file_object) + self.x = Double.read(file_object) + self.y = Double.read(file_object) + self.z = Double.read(file_object) + is_entity = Boolean.read(file_object) + if is_entity: + # If the entity given by entity ID cannot be found, + # this packet should be treated as if is_entity was false. + self.entity_id = VarInt.read(file_object) + self.entity_feet_or_eyes = VarInt.read(file_object) + + else: # Protocol version 352 + is_entity = Boolean.read(file_object) + self.entity_id = VarInt.read(file_object) if is_entity else None + if not is_entity: + self.x = Double.read(file_object) + self.y = Double.read(file_object) + self.z = Double.read(file_object) + + def write_fields(self, packet_buffer): + if self.context.protocol_version >= 353: + VarInt.send(self.feet_or_eyes, packet_buffer) + Double.send(self.x, packet_buffer) + Double.send(self.y, packet_buffer) + Double.send(self.z, packet_buffer) + if self.entity_id: + VarInt.send(self.entity_id, packet_buffer) + VarInt.send(self.entity_feet_or_eyes, packet_buffer) + + else: # Protocol version 352 + if self.entity_id: + Boolean.send(True, packet_buffer) + VarInt.send(self.entity_id, packet_buffer) + else: + Boolean.send(False, packet_buffer) + Double.send(self.x, packet_buffer) + Double.send(self.y, packet_buffer) + Double.send(self.z, packet_buffer) + + # FacePlayerPacket.FeetEyes is an alias for FeetEyes + FeetEyes = FeetEyes \ No newline at end of file diff --git a/minecraft/networking/types/enum.py b/minecraft/networking/types/enum.py index 8d43374..c50b5ff 100644 --- a/minecraft/networking/types/enum.py +++ b/minecraft/networking/types/enum.py @@ -12,7 +12,7 @@ from .utility import Vector __all__ = ( 'Enum', 'BitFieldEnum', 'AbsoluteHand', 'RelativeHand', 'BlockFace', - 'Difficulty', 'Dimension', 'GameMode' + 'Difficulty', 'Dimension', 'GameMode', 'FeetEyes' ) @@ -106,3 +106,10 @@ class GameMode(Enum): CREATIVE = 1 ADVENTURE = 2 SPECTATOR = 3 + + +# Designation of an entity's head or eyes. +# Used in the Face Player Packet +class FeetEyes(Enum): + FEET = 0 + EYES = 1 From 7361f761f5103afa0d285561086557582428db59 Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 May 2019 13:51:31 +0100 Subject: [PATCH 02/13] Implement type 'Angle', packet 'EntityLookPacket' and fix Packet Types Remarks: I chose to implement an angle between 0-360 degrees as opposed to -180 - 180. linear transformation, the maths was far simpler converting an UnsignedByte into positive values instead of a Byte into negative and positive --- .../packets/clientbound/play/__init__.py | 25 +++++++++++++++++-- .../clientbound/play/spawn_object_packet.py | 6 ++--- minecraft/networking/types/basic.py | 19 +++++++++++--- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 5e1137e..692507b 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -5,6 +5,7 @@ from minecraft.networking.packets import ( from minecraft.networking.types import ( Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, + Angle ) from .combat_event_packet import CombatEventPacket @@ -40,6 +41,7 @@ def get_packets(context): RespawnPacket, PluginMessagePacket, PlayerListHeaderAndFooterPacket, + EntityLookPacket } if context.protocol_version <= 47: packets |= { @@ -178,8 +180,8 @@ class SpawnPlayerPacket(Packet): else {'y': FixedPointInteger}, {'z': Double} if context.protocol_version >= 100 else {'z': FixedPointInteger}, - {'yaw': Float}, - {'pitch': Float}, + {'yaw': Angle}, + {'pitch': Angle}, # TODO: read entity metadata {'current_item': Short} if context.protocol_version <= 49 else {} ]) @@ -296,3 +298,22 @@ class PlayerListHeaderAndFooterPacket(Packet): definition = [ {'header': String}, {'footer': String}] + + +class EntityLookPacket(Packet): + @staticmethod + def get_id(context): + return 0x2A if context.protocol_version >= 389 else \ + 0x29 if context.protocol_version >= 345 else \ + 0x28 if context.protocol_version >= 318 else \ + 0x27 if context.protocol_version >= 94 else \ + 0x28 if context.protocol_version >= 70 else \ + 0x16 + + packet_name = 'entity look' + definition = [ + {'entity_id': VarInt}, + {'yaw': Angle}, + {'pitch': Angle}, + {'on_ground': Boolean} + ] \ No newline at end of file diff --git a/minecraft/networking/packets/clientbound/play/spawn_object_packet.py b/minecraft/networking/packets/clientbound/play/spawn_object_packet.py index c8e48b7..3c93de9 100644 --- a/minecraft/networking/packets/clientbound/play/spawn_object_packet.py +++ b/minecraft/networking/packets/clientbound/play/spawn_object_packet.py @@ -2,7 +2,7 @@ from minecraft.networking.packets import Packet from minecraft.networking.types.utility import descriptor from minecraft.networking.types import ( - VarInt, UUID, Byte, Double, Integer, UnsignedByte, Short, Enum, Vector, + VarInt, UUID, Byte, Double, Integer, Angle, Short, Enum, Vector, PositionAndLook, attribute_alias, multi_attribute_alias, ) @@ -103,7 +103,7 @@ class SpawnObjectPacket(Packet): for attr in 'x', 'y', 'z': setattr(self, attr, xyz_type.read(file_object)) for attr in 'pitch', 'yaw': - setattr(self, attr, UnsignedByte.read(file_object)) + setattr(self, attr, Angle.read(file_object)) self.data = Integer.read(file_object) if self.context.protocol_version >= 49 or self.data > 0: @@ -125,7 +125,7 @@ class SpawnObjectPacket(Packet): for coord in self.x, self.y, self.z: xyz_type.send(coord, packet_buffer) for coord in self.pitch, self.yaw: - UnsignedByte.send(coord, packet_buffer) + Angle.send(coord, packet_buffer) Integer.send(self.data, packet_buffer) if self.context.protocol_version >= 49 or self.data > 0: diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index ab43198..5d48b23 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -11,9 +11,10 @@ from .utility import Vector __all__ = ( 'Type', 'Boolean', 'UnsignedByte', 'Byte', 'Short', 'UnsignedShort', - 'Integer', 'FixedPointInteger', 'VarInt', 'Long', 'UnsignedLong', 'Float', - 'Double', 'ShortPrefixedByteArray', 'VarIntPrefixedByteArray', - 'TrailingByteArray', 'String', 'UUID', 'Position', + 'Integer', 'FixedPointInteger', 'Angle', 'VarInt', 'Long', + 'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray', + 'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID', + 'Position', ) @@ -117,6 +118,17 @@ class FixedPointInteger(Type): Integer.send(int(value * 32), socket) +class Angle(Type): + @staticmethod + def read(file_object): + # Linearly transform angle in steps of 1/256 into steps of 1/360 + return 360 * UnsignedByte.read(file_object) / 255 + + def send(value, socket): + # Normalize angle between 0 and 255 and convert to int. + UnsignedByte.send(int(255 * (value / 360)), socket) + + class VarInt(Type): @staticmethod def read(file_object): @@ -311,3 +323,4 @@ class Position(Type, Vector): if context.protocol_version >= 443 else (x & 0x3FFFFFF) << 38 | (y & 0xFFF) << 26 | (z & 0x3FFFFFF)) UnsignedLong.send(value, socket) + From 0fc8c3bbfe86007846c0589a6094668ce4cca9fe Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 May 2019 13:52:13 +0100 Subject: [PATCH 03/13] Add tests for new Angle type. --- tests/test_serialization.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index dc07572..0daff0f 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -3,7 +3,7 @@ import unittest from minecraft.networking.types import ( Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, - Integer, FixedPointInteger, VarInt, Long, Float, Double, + Integer, FixedPointInteger, Angle, VarInt, Long, Float, Double, ShortPrefixedByteArray, VarIntPrefixedByteArray, UUID, String as StringType, Position, TrailingByteArray, UnsignedLong, ) @@ -25,6 +25,7 @@ TEST_DATA = { UnsignedLong: [0, 400], Integer: [-1000, 1000], FixedPointInteger: [float(-13098.3435), float(-0.83), float(1000)], + Angle: [0, 360.0, 90, 47.12947238973, 214.588235], VarInt: [1, 250, 50000, 10000000], Long: [50000000], Float: [21.000301], @@ -58,6 +59,9 @@ class SerializationTest(unittest.TestCase): if data_type is FixedPointInteger: self.assertAlmostEqual( test_data, deserialized, delta=1.0/32.0) + elif data_type is Angle: + print(test_data - deserialized) + self.assertAlmostEqual(test_data, deserialized, delta=360/255) elif data_type is Float or data_type is Double: self.assertAlmostEquals(test_data, deserialized, 3) else: From 3c594a1386b01b055f0c7dd23a940d2ae8c116dc Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 May 2019 13:59:23 +0100 Subject: [PATCH 04/13] Remove debugging print statement and make send static --- minecraft/networking/packets/clientbound/play/__init__.py | 2 +- minecraft/networking/types/basic.py | 2 +- tests/test_serialization.py | 1 - 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 692507b..d548588 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -316,4 +316,4 @@ class EntityLookPacket(Packet): {'yaw': Angle}, {'pitch': Angle}, {'on_ground': Boolean} - ] \ No newline at end of file + ] diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index 5d48b23..437a4ec 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -124,6 +124,7 @@ class Angle(Type): # Linearly transform angle in steps of 1/256 into steps of 1/360 return 360 * UnsignedByte.read(file_object) / 255 + @staticmethod def send(value, socket): # Normalize angle between 0 and 255 and convert to int. UnsignedByte.send(int(255 * (value / 360)), socket) @@ -323,4 +324,3 @@ class Position(Type, Vector): if context.protocol_version >= 443 else (x & 0x3FFFFFF) << 38 | (y & 0xFFF) << 26 | (z & 0x3FFFFFF)) UnsignedLong.send(value, socket) - diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 0daff0f..fb05a76 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -60,7 +60,6 @@ class SerializationTest(unittest.TestCase): self.assertAlmostEqual( test_data, deserialized, delta=1.0/32.0) elif data_type is Angle: - print(test_data - deserialized) self.assertAlmostEqual(test_data, deserialized, delta=360/255) elif data_type is Float or data_type is Double: self.assertAlmostEquals(test_data, deserialized, 3) From 22008c5c5c9312ccc3f452d3ee511e0919d287c5 Mon Sep 17 00:00:00 2001 From: Zachy Date: Wed, 15 May 2019 14:18:49 +0100 Subject: [PATCH 05/13] New line at end of file --- .../networking/packets/clientbound/play/face_player_packet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minecraft/networking/packets/clientbound/play/face_player_packet.py b/minecraft/networking/packets/clientbound/play/face_player_packet.py index f2796ac..83e3f20 100644 --- a/minecraft/networking/packets/clientbound/play/face_player_packet.py +++ b/minecraft/networking/packets/clientbound/play/face_player_packet.py @@ -57,4 +57,4 @@ class FacePlayerPacket(Packet): Double.send(self.z, packet_buffer) # FacePlayerPacket.FeetEyes is an alias for FeetEyes - FeetEyes = FeetEyes \ No newline at end of file + FeetEyes = FeetEyes From 93f6d269dac6326bd414623497968f0248980267 Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 02:09:27 +0100 Subject: [PATCH 06/13] Fix tests to work with new Angle type@ --- .../packets/clientbound/play/__init__.py | 3 +-- minecraft/networking/types/basic.py | 2 +- tests/test_packets.py | 23 +++++++++++++++---- tests/test_serialization.py | 3 ++- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index d548588..b1683e4 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -4,8 +4,7 @@ from minecraft.networking.packets import ( from minecraft.networking.types import ( Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short, - VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, - Angle + VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, Angle ) from .combat_event_packet import CombatEventPacket diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index 437a4ec..114e931 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -127,7 +127,7 @@ class Angle(Type): @staticmethod def send(value, socket): # Normalize angle between 0 and 255 and convert to int. - UnsignedByte.send(int(255 * (value / 360)), socket) + UnsignedByte.send(round(255 * (value / 360)), socket) class VarInt(Type): diff --git a/tests/test_packets.py b/tests/test_packets.py index f6736f3..a50f631 100644 --- a/tests/test_packets.py +++ b/tests/test_packets.py @@ -201,7 +201,7 @@ class TestReadWritePackets(unittest.TestCase): pos_look = PositionAndLook( position=(Vector(68.0, 38.0, 76.0) if context.protocol_version >= 100 else Vector(68, 38, 76)), - yaw=16, pitch=23) + yaw=263.494, pitch=180) velocity = Vector(21, 55, 41) entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG @@ -235,8 +235,10 @@ class TestReadWritePackets(unittest.TestCase): packet2.data = 0 if context.protocol_version < 49: del packet2.velocity - self._test_read_write_packet(packet, context) - self._test_read_write_packet(packet2, context) + self._test_read_write_packet(packet, context, + yaw=360/255, pitch=360/255) + self._test_read_write_packet(packet2, context, + yaw=360/255, pitch=360/255) def test_sound_effect_packet(self): for protocol_version in TEST_VERSIONS: @@ -254,7 +256,12 @@ class TestReadWritePackets(unittest.TestCase): clientbound.play.SoundEffectPacket.SoundCategory.NEUTRAL self._test_read_write_packet(packet, context) - def _test_read_write_packet(self, packet_in, context=None): + def _test_read_write_packet(self, packet_in, context=None, **kwargs): + """ + If kwargs are specified, the key will be tested against the + respective delta value. Useful for testing FixedPointNumbers + where there is precision lost in the resulting value. + """ if context is None: for protocol_version in TEST_VERSIONS: logging.debug('protocol_version = %r' % protocol_version) @@ -272,4 +279,12 @@ class TestReadWritePackets(unittest.TestCase): packet_out = type(packet_in)(context=context) packet_out.read(packet_buffer) self.assertIs(type(packet_in), type(packet_out)) + + for packet_attr, precision in kwargs.items(): + packet_attribute_in = packet_in.__dict__.pop(packet_attr) + packet_attribute_out = packet_out.__dict__.pop(packet_attr) + self.assertAlmostEqual(packet_attribute_in, + packet_attribute_out, + delta=precision) + self.assertEqual(packet_in.__dict__, packet_out.__dict__) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index fb05a76..8cc4547 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -60,7 +60,8 @@ class SerializationTest(unittest.TestCase): self.assertAlmostEqual( test_data, deserialized, delta=1.0/32.0) elif data_type is Angle: - self.assertAlmostEqual(test_data, deserialized, delta=360/255) + self.assertAlmostEqual(test_data, deserialized, + delta=360/255) elif data_type is Float or data_type is Double: self.assertAlmostEquals(test_data, deserialized, 3) else: From a60b513e745cda46b48eeb27e7bc3a998ebaac2a Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 02:15:01 +0100 Subject: [PATCH 07/13] Fix import order --- minecraft/networking/packets/clientbound/play/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index b1683e4..2225033 100644 --- a/minecraft/networking/packets/clientbound/play/__init__.py +++ b/minecraft/networking/packets/clientbound/play/__init__.py @@ -3,8 +3,8 @@ from minecraft.networking.packets import ( ) from minecraft.networking.types import ( - Integer, FixedPointInteger, UnsignedByte, Byte, Boolean, UUID, Short, - VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode, Angle + Integer, FixedPointInteger, Angle, UnsignedByte, Byte, Boolean, UUID, + Short, VarInt, Double, Float, String, Enum, Difficulty, Dimension, GameMode ) from .combat_event_packet import CombatEventPacket @@ -15,7 +15,7 @@ from .spawn_object_packet import SpawnObjectPacket from .block_change_packet import BlockChangePacket, MultiBlockChangePacket from .explosion_packet import ExplosionPacket from .sound_effect_packet import SoundEffectPacket -from . face_player_packet import FacePlayerPacket +from .face_player_packet import FacePlayerPacket # Formerly known as state_playing_clientbound. From 0b127da0cac45fb8a59a1834ce9ed8a744de2cb0 Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 21:31:33 +0100 Subject: [PATCH 08/13] Feedback: Correct Fixed Point conversion & take mod of angle send value --- minecraft/networking/types/basic.py | 5 +++-- tests/test_packets.py | 4 ++-- tests/test_serialization.py | 8 ++++---- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index 114e931..84481ed 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -122,12 +122,13 @@ class Angle(Type): @staticmethod def read(file_object): # Linearly transform angle in steps of 1/256 into steps of 1/360 - return 360 * UnsignedByte.read(file_object) / 255 + return 360 * UnsignedByte.read(file_object) / 256 @staticmethod def send(value, socket): # Normalize angle between 0 and 255 and convert to int. - UnsignedByte.send(round(255 * (value / 360)), socket) + + UnsignedByte.send(round(256 * ((value % 360) / 360)), socket) class VarInt(Type): diff --git a/tests/test_packets.py b/tests/test_packets.py index a50f631..045cda8 100644 --- a/tests/test_packets.py +++ b/tests/test_packets.py @@ -236,9 +236,9 @@ class TestReadWritePackets(unittest.TestCase): if context.protocol_version < 49: del packet2.velocity self._test_read_write_packet(packet, context, - yaw=360/255, pitch=360/255) + yaw=360/256, pitch=360/256) self._test_read_write_packet(packet2, context, - yaw=360/255, pitch=360/255) + yaw=360/256, pitch=360/256) def test_sound_effect_packet(self): for protocol_version in TEST_VERSIONS: diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 8cc4547..4e58092 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -25,7 +25,7 @@ TEST_DATA = { UnsignedLong: [0, 400], Integer: [-1000, 1000], FixedPointInteger: [float(-13098.3435), float(-0.83), float(1000)], - Angle: [0, 360.0, 90, 47.12947238973, 214.588235], + Angle: [0, 360.0, 720, 47.12947238973, -108.7], VarInt: [1, 250, 50000, 10000000], Long: [50000000], Float: [21.000301], @@ -60,10 +60,10 @@ class SerializationTest(unittest.TestCase): self.assertAlmostEqual( test_data, deserialized, delta=1.0/32.0) elif data_type is Angle: - self.assertAlmostEqual(test_data, deserialized, - delta=360/255) + self.assertAlmostEqual(test_data % 360, deserialized, + delta=360/256) elif data_type is Float or data_type is Double: - self.assertAlmostEquals(test_data, deserialized, 3) + self.assertAlmostEqual(test_data, deserialized, 3) else: self.assertEqual(test_data, deserialized) From e8a0e34aefeffaec19bd5b7fcf8728eecabfd46f Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 21:38:57 +0100 Subject: [PATCH 09/13] Feedback: Replace FeetEyes convention with OriginPoint for forward compat --- .../packets/clientbound/play/face_player_packet.py | 14 +++++++------- minecraft/networking/types/enum.py | 6 +++--- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/face_player_packet.py b/minecraft/networking/packets/clientbound/play/face_player_packet.py index 83e3f20..a9a9abf 100644 --- a/minecraft/networking/packets/clientbound/play/face_player_packet.py +++ b/minecraft/networking/packets/clientbound/play/face_player_packet.py @@ -1,5 +1,5 @@ from minecraft.networking.types import ( - VarInt, Double, Boolean, FeetEyes + VarInt, Double, Boolean, OriginPoint ) from minecraft.networking.packets import Packet @@ -17,7 +17,7 @@ class FacePlayerPacket(Packet): def read(self, file_object): if self.context.protocol_version >= 353: - self.feet_or_eyes = VarInt.read(file_object) + self.origin = VarInt.read(file_object) self.x = Double.read(file_object) self.y = Double.read(file_object) self.z = Double.read(file_object) @@ -26,7 +26,7 @@ class FacePlayerPacket(Packet): # If the entity given by entity ID cannot be found, # this packet should be treated as if is_entity was false. self.entity_id = VarInt.read(file_object) - self.entity_feet_or_eyes = VarInt.read(file_object) + self.entity_origin = VarInt.read(file_object) else: # Protocol version 352 is_entity = Boolean.read(file_object) @@ -38,13 +38,13 @@ class FacePlayerPacket(Packet): def write_fields(self, packet_buffer): if self.context.protocol_version >= 353: - VarInt.send(self.feet_or_eyes, packet_buffer) + VarInt.send(self.origin, packet_buffer) Double.send(self.x, packet_buffer) Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer) if self.entity_id: VarInt.send(self.entity_id, packet_buffer) - VarInt.send(self.entity_feet_or_eyes, packet_buffer) + VarInt.send(self.entity_origin, packet_buffer) else: # Protocol version 352 if self.entity_id: @@ -56,5 +56,5 @@ class FacePlayerPacket(Packet): Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer) - # FacePlayerPacket.FeetEyes is an alias for FeetEyes - FeetEyes = FeetEyes + # FacePlayerPacket.OriginPoint is an alias for OriginPoint + OriginPoint = OriginPoint diff --git a/minecraft/networking/types/enum.py b/minecraft/networking/types/enum.py index c50b5ff..61aa238 100644 --- a/minecraft/networking/types/enum.py +++ b/minecraft/networking/types/enum.py @@ -12,7 +12,7 @@ from .utility import Vector __all__ = ( 'Enum', 'BitFieldEnum', 'AbsoluteHand', 'RelativeHand', 'BlockFace', - 'Difficulty', 'Dimension', 'GameMode', 'FeetEyes' + 'Difficulty', 'Dimension', 'GameMode', 'OriginPoint' ) @@ -108,8 +108,8 @@ class GameMode(Enum): SPECTATOR = 3 -# Designation of an entity's head or eyes. +# Currently designates an entity's feet or eyes. # Used in the Face Player Packet -class FeetEyes(Enum): +class OriginPoint(Enum): FEET = 0 EYES = 1 From b0a9a3693c4196592953d4c93fa8a7bad351dbef Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 21:45:21 +0100 Subject: [PATCH 10/13] Remove white space --- minecraft/networking/types/basic.py | 1 - 1 file changed, 1 deletion(-) diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index 84481ed..4ccf627 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -127,7 +127,6 @@ class Angle(Type): @staticmethod def send(value, socket): # Normalize angle between 0 and 255 and convert to int. - UnsignedByte.send(round(256 * ((value % 360) / 360)), socket) From a6c11bbb3495c7e9435a437d72512396d7635561 Mon Sep 17 00:00:00 2001 From: Zachy Date: Fri, 17 May 2019 21:56:32 +0100 Subject: [PATCH 11/13] Flake8: Correct E501 line too long (81 > 79 characters) --- tests/test_serialization.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 4e58092..b991913 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -60,7 +60,8 @@ class SerializationTest(unittest.TestCase): self.assertAlmostEqual( test_data, deserialized, delta=1.0/32.0) elif data_type is Angle: - self.assertAlmostEqual(test_data % 360, deserialized, + self.assertAlmostEqual(test_data % 360, + deserialized, delta=360/256) elif data_type is Float or data_type is Double: self.assertAlmostEqual(test_data, deserialized, 3) From a3357762d7f98bbca9933831ac5ae62ca2b4cf9c Mon Sep 17 00:00:00 2001 From: joodicator Date: Sat, 18 May 2019 01:39:12 +0200 Subject: [PATCH 12/13] Add tests for FacePlayerPacket, Angle; fix bugs; misc. changes * Add alias FacePlayerPacket.target for x, y, z fields. * Replace FacePlayerPacket.OriginPoint type alias with Origin and EntityOrigin aliases. --- .../clientbound/play/face_player_packet.py | 19 ++++++++--- tests/test_packets.py | 32 +++++++++++++++---- tests/test_serialization.py | 25 ++++++++++----- 3 files changed, 56 insertions(+), 20 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/face_player_packet.py b/minecraft/networking/packets/clientbound/play/face_player_packet.py index a9a9abf..39b76ec 100644 --- a/minecraft/networking/packets/clientbound/play/face_player_packet.py +++ b/minecraft/networking/packets/clientbound/play/face_player_packet.py @@ -1,5 +1,5 @@ from minecraft.networking.types import ( - VarInt, Double, Boolean, OriginPoint + VarInt, Double, Boolean, OriginPoint, Vector, multi_attribute_alias ) from minecraft.networking.packets import Packet @@ -15,6 +15,9 @@ class FacePlayerPacket(Packet): packet_name = 'face player' + # Access the 'x', 'y', 'z' fields as a Vector. + target = multi_attribute_alias(Vector, 'x', 'y', 'z') + def read(self, file_object): if self.context.protocol_version >= 353: self.origin = VarInt.read(file_object) @@ -27,6 +30,8 @@ class FacePlayerPacket(Packet): # this packet should be treated as if is_entity was false. self.entity_id = VarInt.read(file_object) self.entity_origin = VarInt.read(file_object) + else: + self.entity_id = None else: # Protocol version 352 is_entity = Boolean.read(file_object) @@ -42,12 +47,15 @@ class FacePlayerPacket(Packet): Double.send(self.x, packet_buffer) Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer) - if self.entity_id: + if self.entity_id is not None: + Boolean.send(True, packet_buffer) VarInt.send(self.entity_id, packet_buffer) VarInt.send(self.entity_origin, packet_buffer) + else: + Boolean.send(False, packet_buffer) else: # Protocol version 352 - if self.entity_id: + if self.entity_id is not None: Boolean.send(True, packet_buffer) VarInt.send(self.entity_id, packet_buffer) else: @@ -56,5 +64,6 @@ class FacePlayerPacket(Packet): Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer) - # FacePlayerPacket.OriginPoint is an alias for OriginPoint - OriginPoint = OriginPoint + # These aliases declare the Enum type corresponding to each field: + Origin = OriginPoint + EntityOrigin = OriginPoint diff --git a/tests/test_packets.py b/tests/test_packets.py index 045cda8..3db2fb2 100644 --- a/tests/test_packets.py +++ b/tests/test_packets.py @@ -9,7 +9,7 @@ from random import choice from minecraft import SUPPORTED_PROTOCOL_VERSIONS, RELEASE_PROTOCOL_VERSIONS from minecraft.networking.connection import ConnectionContext from minecraft.networking.types import ( - VarInt, Enum, Vector, PositionAndLook + VarInt, Enum, Vector, PositionAndLook, OriginPoint, ) from minecraft.networking.packets import ( Packet, PacketBuffer, PacketListener, KeepAlivePacket, serverbound, @@ -199,8 +199,8 @@ class TestReadWritePackets(unittest.TestCase): 'type_id', context) pos_look = PositionAndLook( - position=(Vector(68.0, 38.0, 76.0) if context.protocol_version - >= 100 else Vector(68, 38, 76)), + position=(Vector(68.0, 38.0, 76.0) if protocol_version >= 100 + else Vector(68, 38, 76)), yaw=263.494, pitch=180) velocity = Vector(21, 55, 41) entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG @@ -212,7 +212,7 @@ class TestReadWritePackets(unittest.TestCase): velocity_x=velocity.x, velocity_y=velocity.y, velocity_z=velocity.z, entity_id=entity_id, type_id=type_id, data=1) - if context.protocol_version >= 49: + if protocol_version >= 49: object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa' packet.object_uuid = object_uuid self.assertEqual(packet.objectUUID, object_uuid) @@ -225,7 +225,7 @@ class TestReadWritePackets(unittest.TestCase): context=context, position_and_look=pos_look, velocity=velocity, type=type_name, entity_id=entity_id, data=1) - if context.protocol_version >= 49: + if protocol_version >= 49: packet2.object_uuid = object_uuid self.assertEqual(packet.__dict__, packet2.__dict__) @@ -233,7 +233,7 @@ class TestReadWritePackets(unittest.TestCase): self.assertEqual(packet.position, packet2.position) packet2.data = 0 - if context.protocol_version < 49: + if protocol_version < 49: del packet2.velocity self._test_read_write_packet(packet, context, yaw=360/256, pitch=360/256) @@ -247,7 +247,7 @@ class TestReadWritePackets(unittest.TestCase): packet = clientbound.play.SoundEffectPacket( sound_id=545, effect_position=Vector(0.125, 300.0, 50.5), volume=0.75) - if context.protocol_version >= 201: + if protocol_version >= 201: packet.pitch = struct.unpack('f', struct.pack('f', 1.5))[0] else: packet.pitch = int(1.5 / 63.5) * 63.5 @@ -256,6 +256,24 @@ class TestReadWritePackets(unittest.TestCase): clientbound.play.SoundEffectPacket.SoundCategory.NEUTRAL self._test_read_write_packet(packet, context) + def test_face_player_packet(self): + for protocol_version in TEST_VERSIONS: + context = ConnectionContext(protocol_version=protocol_version) + + packet = clientbound.play.FacePlayerPacket() + packet.target = 1.0, -2.0, 3.5 + packet.entity_id = None + if protocol_version >= 353: + packet.origin = OriginPoint.EYES + self._test_read_write_packet(packet, context) + + packet.entity_id = 123 + if protocol_version >= 353: + packet.entity_origin = OriginPoint.FEET + else: + del packet.target + self._test_read_write_packet(packet, context) + def _test_read_write_packet(self, packet_in, context=None, **kwargs): """ If kwargs are specified, the key will be tested against the diff --git a/tests/test_serialization.py b/tests/test_serialization.py index b991913..6c1ae70 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -1,9 +1,12 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +from __future__ import division + import unittest + from minecraft.networking.types import ( - Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, - Integer, FixedPointInteger, Angle, VarInt, Long, Float, Double, + Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Integer, + FixedPointInteger, Angle, VarInt, Long, Float, Double, Angle, ShortPrefixedByteArray, VarIntPrefixedByteArray, UUID, String as StringType, Position, TrailingByteArray, UnsignedLong, ) @@ -36,6 +39,8 @@ TEST_DATA = { UUID: ["12345678-1234-5678-1234-567812345678"], StringType: ["hello world"], Position: [(758, 0, 691), (-500, -12, -684)], + Angle: {0.0:0.0, 1/256:1/256, 255/256:255/256, 360.0:0.0, -90.0:270.0, + -1890.0:270.0, 1890.0:90.0}, } @@ -47,26 +52,30 @@ class SerializationTest(unittest.TestCase): for data_type in Type.__subclasses__(): if data_type in TEST_DATA: test_cases = TEST_DATA[data_type] + test_cases = test_cases.items() \ + if isinstance(test_cases, dict) else \ + map(lambda x: (x, x), test_cases) - for test_data in test_cases: + for write_data, expected_read_data in test_cases: packet_buffer = PacketBuffer() data_type.send_with_context( - test_data, packet_buffer, context) + write_data, packet_buffer, context) packet_buffer.reset_cursor() deserialized = data_type.read_with_context( packet_buffer, context) if data_type is FixedPointInteger: self.assertAlmostEqual( - test_data, deserialized, delta=1.0/32.0) + expected_read_data, deserialized, delta=1/32) elif data_type is Angle: - self.assertAlmostEqual(test_data % 360, + self.assertAlmostEqual(expected_read_data % 360, deserialized, delta=360/256) elif data_type is Float or data_type is Double: - self.assertAlmostEqual(test_data, deserialized, 3) + self.assertAlmostEqual( + expected_read_data, deserialized, 3) else: - self.assertEqual(test_data, deserialized) + self.assertEqual(expected_read_data, deserialized) def test_exceptions(self): base_type = Type() From 1012ee86405c15d63e87f463393eab8cfd24fa90 Mon Sep 17 00:00:00 2001 From: joodicator Date: Sat, 18 May 2019 02:54:21 +0200 Subject: [PATCH 13/13] Revert: Add tests for Angle (were already present) --- tests/test_serialization.py | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 6c1ae70..b991913 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -1,12 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -from __future__ import division - import unittest - from minecraft.networking.types import ( - Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Integer, - FixedPointInteger, Angle, VarInt, Long, Float, Double, Angle, + Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, + Integer, FixedPointInteger, Angle, VarInt, Long, Float, Double, ShortPrefixedByteArray, VarIntPrefixedByteArray, UUID, String as StringType, Position, TrailingByteArray, UnsignedLong, ) @@ -39,8 +36,6 @@ TEST_DATA = { UUID: ["12345678-1234-5678-1234-567812345678"], StringType: ["hello world"], Position: [(758, 0, 691), (-500, -12, -684)], - Angle: {0.0:0.0, 1/256:1/256, 255/256:255/256, 360.0:0.0, -90.0:270.0, - -1890.0:270.0, 1890.0:90.0}, } @@ -52,30 +47,26 @@ class SerializationTest(unittest.TestCase): for data_type in Type.__subclasses__(): if data_type in TEST_DATA: test_cases = TEST_DATA[data_type] - test_cases = test_cases.items() \ - if isinstance(test_cases, dict) else \ - map(lambda x: (x, x), test_cases) - for write_data, expected_read_data in test_cases: + for test_data in test_cases: packet_buffer = PacketBuffer() data_type.send_with_context( - write_data, packet_buffer, context) + test_data, packet_buffer, context) packet_buffer.reset_cursor() deserialized = data_type.read_with_context( packet_buffer, context) if data_type is FixedPointInteger: self.assertAlmostEqual( - expected_read_data, deserialized, delta=1/32) + test_data, deserialized, delta=1.0/32.0) elif data_type is Angle: - self.assertAlmostEqual(expected_read_data % 360, + self.assertAlmostEqual(test_data % 360, deserialized, delta=360/256) elif data_type is Float or data_type is Double: - self.assertAlmostEqual( - expected_read_data, deserialized, 3) + self.assertAlmostEqual(test_data, deserialized, 3) else: - self.assertEqual(expected_read_data, deserialized) + self.assertEqual(test_data, deserialized) def test_exceptions(self): base_type = Type()