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: