Add type for general fixed-point numbers

This commit is contained in:
joo 2020-09-01 00:37:48 +02:00
parent 3c84c2a429
commit e434497dc7
3 changed files with 28 additions and 14 deletions

View File

@ -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 {},

View File

@ -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):

View File

@ -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,