From e434497dc78f365d7c59d6130f9a44247cf7d9ef Mon Sep 17 00:00:00 2001 From: joo Date: Tue, 1 Sep 2020 00:37:48 +0200 Subject: [PATCH] Add type for general fixed-point numbers --- .../packets/clientbound/play/__init__.py | 10 +++---- minecraft/networking/types/basic.py | 26 ++++++++++++------- tests/test_backward_compatible.py | 6 +++++ 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/minecraft/networking/packets/clientbound/play/__init__.py b/minecraft/networking/packets/clientbound/play/__init__.py index 6dfe471..58e1596 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 ( - FixedPointInteger, Angle, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, - Double, Float, String, Enum, Difficulty, Long, Vector, Direction, + FixedPoint, Integer, Angle, UnsignedByte, Byte, Boolean, UUID, Short, + VarInt, Double, Float, String, Enum, Difficulty, Long, Vector, Direction, PositionAndLook, multi_attribute_alias, ) @@ -159,11 +159,11 @@ class SpawnPlayerPacket(Packet): {'entity_id': VarInt}, {'player_UUID': UUID}, {'x': Double} if context.protocol_version >= 100 - else {'x': FixedPointInteger}, + else {'x': FixedPoint(Integer)}, {'y': Double} if context.protocol_version >= 100 - else {'y': FixedPointInteger}, + else {'y': FixedPoint(Integer)}, {'z': Double} if context.protocol_version >= 100 - else {'z': FixedPointInteger}, + else {'z': FixedPoint(Integer)}, {'yaw': Angle}, {'pitch': Angle}, {'current_item': Short} if context.protocol_version <= 49 else {}, diff --git a/minecraft/networking/types/basic.py b/minecraft/networking/types/basic.py index f463c24..0a02ecd 100644 --- a/minecraft/networking/types/basic.py +++ b/minecraft/networking/types/basic.py @@ -13,8 +13,8 @@ from .utility import Vector, class_and_instancemethod __all__ = ( 'Type', 'Boolean', 'UnsignedByte', 'Byte', 'Short', 'UnsignedShort', - 'Integer', 'FixedPointInteger', 'Angle', 'VarInt', 'VarLong', 'Long', - 'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray', + 'Integer', 'FixedPoint', 'FixedPointInteger', 'Angle', 'VarInt', 'VarLong', + 'Long', 'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray', 'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID', 'Position', 'NBT', 'PrefixedArray', ) @@ -111,14 +111,22 @@ class Integer(Type): socket.send(struct.pack('>i', value)) -class FixedPointInteger(Type): - @staticmethod - def read(file_object): - return Integer.read(file_object) / 32 +class FixedPoint(Type): + __slots__ = 'integer_type', 'denominator' - @staticmethod - def send(value, socket): - Integer.send(int(value * 32), socket) + def __init__(self, integer_type, fractional_bits=5): + self.integer_type = integer_type + self.denominator = 2**fractional_bits + + def read(self, file_object): + return self.integer_type.read(file_object) / self.denominator + + def send(self, value, socket): + self.integer_type.send(int(value * self.denominator)) + + +# This named instance is retained for backward compatibility: +FixedPointInteger = FixedPoint(Integer) class Angle(Type): diff --git a/tests/test_backward_compatible.py b/tests/test_backward_compatible.py index e9b321f..db9ef3b 100644 --- a/tests/test_backward_compatible.py +++ b/tests/test_backward_compatible.py @@ -82,6 +82,12 @@ class LegacyPacketNamesTest(unittest.TestCase): serverbound.play.get_packets) +class LegacyTypesTest(unittest.TestCase): + def test_legacy_types(self): + self.assertIsInstance(types.FixedPointInteger, types.FixedPoint) + self.assertEqual(types.FixedPointInteger.denominator, 32) + + class ClassMemberAliasesTest(unittest.TestCase): def test_alias_values(self): self.assertEqual(serverbound.play.AnimationPacket.HAND_MAIN,