Add type for general fixed-point numbers

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

No files matched your search

@@ -3,8 +3,8 @@ from minecraft.networking.packets import (
) )
from minecraft.networking.types import ( from minecraft.networking.types import (
FixedPointInteger, Angle, UnsignedByte, Byte, Boolean, UUID, Short, VarInt, FixedPoint, Integer, Angle, UnsignedByte, Byte, Boolean, UUID, Short,
Double, Float, String, Enum, Difficulty, Long, Vector, Direction, VarInt, Double, Float, String, Enum, Difficulty, Long, Vector, Direction,
PositionAndLook, multi_attribute_alias, PositionAndLook, multi_attribute_alias,
) )
@@ -159,11 +159,11 @@ class SpawnPlayerPacket(Packet):
{'entity_id': VarInt}, {'entity_id': VarInt},
{'player_UUID': UUID}, {'player_UUID': UUID},
{'x': Double} if context.protocol_version >= 100 {'x': Double} if context.protocol_version >= 100
else {'x': FixedPointInteger}, else {'x': FixedPoint(Integer)},
{'y': Double} if context.protocol_version >= 100 {'y': Double} if context.protocol_version >= 100
else {'y': FixedPointInteger}, else {'y': FixedPoint(Integer)},
{'z': Double} if context.protocol_version >= 100 {'z': Double} if context.protocol_version >= 100
else {'z': FixedPointInteger}, else {'z': FixedPoint(Integer)},
{'yaw': Angle}, {'yaw': Angle},
{'pitch': Angle}, {'pitch': Angle},
{'current_item': Short} if context.protocol_version <= 49 else {}, {'current_item': Short} if context.protocol_version <= 49 else {},
+17 -9
View File
@@ -13,8 +13,8 @@ from .utility import Vector, class_and_instancemethod
__all__ = ( __all__ = (
'Type', 'Boolean', 'UnsignedByte', 'Byte', 'Short', 'UnsignedShort', 'Type', 'Boolean', 'UnsignedByte', 'Byte', 'Short', 'UnsignedShort',
'Integer', 'FixedPointInteger', 'Angle', 'VarInt', 'VarLong', 'Long', 'Integer', 'FixedPoint', 'FixedPointInteger', 'Angle', 'VarInt', 'VarLong',
'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray', 'Long', 'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray',
'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID', 'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID',
'Position', 'NBT', 'PrefixedArray', 'Position', 'NBT', 'PrefixedArray',
) )
@@ -111,14 +111,22 @@ class Integer(Type):
socket.send(struct.pack('>i', value)) socket.send(struct.pack('>i', value))
class FixedPointInteger(Type): class FixedPoint(Type):
@staticmethod __slots__ = 'integer_type', 'denominator'
def read(file_object):
return Integer.read(file_object) / 32
@staticmethod def __init__(self, integer_type, fractional_bits=5):
def send(value, socket): self.integer_type = integer_type
Integer.send(int(value * 32), socket) 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): class Angle(Type):
+6
View File
@@ -82,6 +82,12 @@ class LegacyPacketNamesTest(unittest.TestCase):
serverbound.play.get_packets) 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): class ClassMemberAliasesTest(unittest.TestCase):
def test_alias_values(self): def test_alias_values(self):
self.assertEqual(serverbound.play.AnimationPacket.HAND_MAIN, self.assertEqual(serverbound.play.AnimationPacket.HAND_MAIN,