Add serialisation and tests for SpawnObjectPacket.

This commit is contained in:
joo committed 2018-05-27 15:36:13 +01:00
1 parent 709b80b539
commit 8578326c2f
7 files changed
+145 -72

No files matched your search

@@ -1,7 +1,8 @@
from minecraft.networking.packets import Packet
from minecraft.networking.types import (
VarInt, UUID, Byte, Double, Integer, UnsignedByte, Short, Enum
VarInt, UUID, Byte, Double, Integer, UnsignedByte, Short, Enum, Vector,
PositionAndLook
)
@@ -44,33 +45,65 @@ class SpawnObjectPacket(Packet):
def read(self, file_object):
self.entity_id = VarInt.read(file_object)
if self._context.protocol_version >= 49:
self.objectUUID = UUID.read(file_object)
type_id = Byte.read(file_object)
self.type = SpawnObjectPacket.EntityType.name_from_value(type_id)
if self.context.protocol_version >= 49:
self.object_uuid = UUID.read(file_object)
self.type_id = Byte.read(file_object)
if self._context.protocol_version >= 100:
self.x = Double.read(file_object)
self.y = Double.read(file_object)
self.z = Double.read(file_object)
else:
self.x = Integer.read(file_object)
self.y = Integer.read(file_object)
self.z = Integer.read(file_object)
xyz_type = Double if self.context.protocol_version >= 100 else Integer
for attr in 'x', 'y', 'z':
setattr(self, attr, xyz_type.read(file_object))
for attr in 'pitch', 'yaw':
setattr(self, attr, UnsignedByte.read(file_object))
self.pitch = UnsignedByte.read(file_object)
self.yaw = UnsignedByte.read(file_object)
self.data = Integer.read(file_object)
if self._context.protocol_version < 49:
if self.data > 0:
self.velocity_x = Short.read(file_object)
self.velocity_y = Short.read(file_object)
self.velocity_z = Short.read(file_object)
else:
self.velocity_x = Short.read(file_object)
self.velocity_y = Short.read(file_object)
self.velocity_z = Short.read(file_object)
if self.context.protocol_version >= 49 or self.data > 0:
for attr in 'velocity_x', 'velocity_y', 'velocity_z':
setattr(self, attr, Short.read(file_object))
def write_fields(self, packet_buffer):
raise NotImplementedError
VarInt.send(self.entity_id, packet_buffer)
if self.context.protocol_version >= 49:
UUID.send(self.object_uuid, packet_buffer)
Byte.send(self.type_id, packet_buffer)
xyz_type = Double if self.context.protocol_version >= 100 else Integer
for coord in self.x, self.y, self.z:
xyz_type.send(coord, packet_buffer)
for coord in self.pitch, self.yaw:
UnsignedByte.send(coord, packet_buffer)
Integer.send(self.data, packet_buffer)
if self.context.protocol_version >= 49 or self.data > 0:
for coord in self.velocity_x, self.velocity_y, self.velocity_z:
Short.send(coord, packet_buffer)
# Access the entity type as a string, according to the EntityType enum.
def type(self, type_name):
self.type_id = getattr(self.EntityType, type_name)
type = property(lambda p: p.EntityType.name_from_value(p.type_id), type)
# Access the fields 'x', 'y', 'z' as a Vector.
def position(self, position):
self.x, self.y, self.z = position
position = property(lambda p: Vector(p.x, p.y, p.z), position)
# Access the fields 'x', 'y', 'z', 'yaw', 'pitch' as a PositionAndLook.
# NOTE: modifying the object retrieved from this property will not change
# the packet; it can only be changed by attribute or property assignment.
def position_and_look(self, position_and_look):
self.x, self.y, self.z = position_and_look.position
self.yaw, self.pitch = position_and_look.look
position_and_look = property(lambda p: PositionAndLook(
x=p.x, y=p.y, z=p.z, yaw=p.yaw, pitch=p.pitch),
position_and_look)
# Access the fields 'velocity_x', 'velocity_y', 'velocity_z' as a Vector.
def velocity(self, velocity):
self.velocity_x, self.velocity_y, self.velocity_z = velocity
velocity = property(lambda p: Vector(p.velocity_x, p.velocity_y,
p.velocity_z), velocity)
# This alias is retained for backward compatibility.
def objectUUID(self, object_uuid):
self.object_uuid = object_uuid
objectUUID = property(lambda self: self.object_uuid, objectUUID)