Remove 'UUIDIntegerArray' type, as 'UUID' already exists

This commit is contained in:
joo 2020-08-12 12:46:08 +02:00
parent 8a098b399b
commit c6afe25429
2 changed files with 3 additions and 39 deletions

View File

@ -1,8 +1,7 @@
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
VarInt, String, VarIntPrefixedByteArray, TrailingByteArray,
UUIDIntegerArray
VarInt, String, VarIntPrefixedByteArray, TrailingByteArray, UUID,
)
@ -56,8 +55,7 @@ class LoginSuccessPacket(Packet):
packet_name = "login success"
get_definition = staticmethod(lambda context: [
{'UUID': UUIDIntegerArray} if context.protocol_version >= 707
else {'UUID': String},
{'UUID': UUID if context.protocol_version >= 707 else String},
{'Username': String}
])

View File

@ -14,7 +14,7 @@ __all__ = (
'Integer', 'FixedPointInteger', 'Angle', 'VarInt', 'Long',
'UnsignedLong', 'Float', 'Double', 'ShortPrefixedByteArray',
'VarIntPrefixedByteArray', 'TrailingByteArray', 'String', 'UUID',
'Position', 'UUIDIntegerArray'
'Position',
)
@ -253,40 +253,6 @@ class VarIntPrefixedByteArray(Type):
socket.send(struct.pack(str(len(value)) + "s", value))
# https://stackoverflow.com/questions/1604464/twos-complement-in-python
def twos_comp(val, bits):
"""compute the 2's complement of int value val"""
if (val & (1 << (bits - 1))) != 0: # if sign bit is set
val = val - (1 << bits) # compute negative value
return val # return positive value as is
class UUIDIntegerArray(Type):
""" Minecraft sends an array of 4 integers to represent the most
significant and least significant bits (as longs) of a UUID
because that is how UUIDs are constructed in Java. We need to
unpack this array and repack it with the right endianness to be
used as a python UUID. """
@staticmethod
def read(file_object):
ints = struct.unpack("4i", file_object.read(4 * 4))
packed = struct.pack("<qq", ints[1] << 32 | ints[0] & 0xffffffff,
ints[3] << 32 | ints[2] & 0xffffffff)
packed_uuid = uuid.UUID(bytes=packed)
return str(packed_uuid)
@staticmethod
def send(value, socket):
player_uuid = uuid.UUID(value)
msb, lsb = struct.unpack(">qq", player_uuid.bytes)
socket.send(struct.pack(">4i", msb >> 32,
twos_comp(msb & 0xffffffff, 32),
lsb >> 32,
twos_comp(lsb & 0xffffffff, 32)
)
)
class TrailingByteArray(Type):
""" A byte array consisting of all remaining data. If present in a packet
definition, this should only be the type of the last field. """