Example #1
0
    def test_properties(self):
        pos_look_1 = PositionAndLook(position=(1, 2, 3), look=(4, 5))
        pos_look_2 = PositionAndLook(x=1, y=2, z=3, yaw=4, pitch=5)
        string_repr = 'PositionAndLook(x=1, y=2, z=3, yaw=4, pitch=5)'

        self.assertEqual(pos_look_1, pos_look_2)
        self.assertEqual(pos_look_1.position, pos_look_1.position)
        self.assertEqual(pos_look_1.look, pos_look_2.look)
        self.assertEqual(hash(pos_look_1), hash(pos_look_2))
        self.assertEqual(str(pos_look_1), string_repr)

        self.assertFalse(pos_look_1 != pos_look_2)
        pos_look_1.position += Vector(1, 1, 1)
        self.assertTrue(pos_look_1 != pos_look_2)
Example #2
0
    def test_spawn_object_packet(self):
        EntityType = clientbound.play.SpawnObjectPacket.EntityType

        object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa'
        pos_look = PositionAndLook(x=68.0, y=38.0, z=76.0, yaw=16, pitch=23)
        velocity = Vector(21, 55, 41)
        entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG

        packet = clientbound.play.SpawnObjectPacket(
                    x=pos_look.x, y=pos_look.y, z=pos_look.z,
                    yaw=pos_look.yaw, pitch=pos_look.pitch,
                    velocity_x=velocity.x, velocity_y=velocity.y,
                    velocity_z=velocity.z, object_uuid=object_uuid,
                    entity_id=entity_id, type_id=type_id, data=1)
        self.assertEqual(packet.position_and_look, pos_look)
        self.assertEqual(packet.position, pos_look.position)
        self.assertEqual(packet.velocity, velocity)
        self.assertEqual(packet.objectUUID, object_uuid)
        self.assertEqual(packet.type, type_name)

        packet2 = clientbound.play.SpawnObjectPacket(
                     position_and_look=pos_look, velocity=velocity,
                     type=type_name, object_uuid=object_uuid,
                     entity_id=entity_id, data=1)
        self.assertEqual(packet.__dict__, packet2.__dict__)

        packet2.position = pos_look.position
        self.assertEqual(packet.position, packet2.position)

        packet2.data = 0
        self._test_read_write_packet(packet)
        self._test_read_write_packet(packet2)
Example #3
0
    def test_spawn_object_packet(self):
        for protocol_version in TEST_VERSIONS:
            logging.debug('protocol_version = %r' % protocol_version)
            context = ConnectionContext(protocol_version=protocol_version)

            EntityType = clientbound.play.SpawnObjectPacket.field_enum(
                            'type_id', context)

            pos_look = PositionAndLook(
                position=(Vector(68.0, 38.0, 76.0) if protocol_version >= 100
                          else Vector(68, 38, 76)),
                yaw=263.494, pitch=180)
            velocity = Vector(21, 55, 41)
            entity_id, type_name, type_id = 49846, 'EGG', EntityType.EGG

            packet = clientbound.play.SpawnObjectPacket(
                        context=context,
                        x=pos_look.x, y=pos_look.y, z=pos_look.z,
                        yaw=pos_look.yaw, pitch=pos_look.pitch,
                        velocity_x=velocity.x, velocity_y=velocity.y,
                        velocity_z=velocity.z,
                        entity_id=entity_id, type_id=type_id, data=1)
            if protocol_version >= 49:
                object_uuid = 'd9568851-85bc-4a10-8d6a-261d130626fa'
                packet.object_uuid = object_uuid
                self.assertEqual(packet.objectUUID, object_uuid)
            self.assertEqual(packet.position_and_look, pos_look)
            self.assertEqual(packet.position, pos_look.position)
            self.assertEqual(packet.velocity, velocity)
            self.assertEqual(packet.type, type_name)

            packet2 = clientbound.play.SpawnObjectPacket(
                        context=context, position_and_look=pos_look,
                        velocity=velocity, type=type_name,
                        entity_id=entity_id, data=1)
            if protocol_version >= 49:
                packet2.object_uuid = object_uuid
            self.assertEqual(packet.__dict__, packet2.__dict__)

            packet2.position = pos_look.position
            self.assertEqual(packet.position, packet2.position)

            packet2.data = 0
            if protocol_version < 49:
                del packet2.velocity
            self._test_read_write_packet(packet, context,
                                         yaw=360/256, pitch=360/256)
            self._test_read_write_packet(packet2, context,
                                         yaw=360/256, pitch=360/256)
Example #4
0
class SpawnObjectPacket(Packet):
    @staticmethod
    def get_id(context):
        return 0x00 if context.protocol_version >= 67 else \
               0x0E

    packet_name = 'spawn object'

    class EntityType(Enum):
        BOAT = 1
        ITEM_STACK = 2
        AREA_EFFECT_CLOUD = 3
        MINECART = 10
        ACTIVATED_TNT = 50
        ENDERCRYSTAL = 51
        ARROW = 60
        SNOWBALL = 61
        EGG = 62
        FIREBALL = 63
        FIRECHARGE = 64
        ENDERPERL = 65
        WITHER_SKULL = 66
        SHULKER_BULLET = 67
        LLAMA_SPIT = 68
        FALLING_OBJECT = 70
        ITEM_FRAMES = 71
        EYE_OF_ENDER = 72
        POTION = 73
        EXP_BOTTLE = 75
        FIREWORK_ROCKET = 76
        LEASH_KNOT = 77
        ARMORSTAND = 78
        EVOCATION_FANGS = 79
        FISHING_HOOK = 90
        SPECTRAL_ARROW = 91
        DRAGON_FIREBALL = 93

    def read(self, file_object):
        self.entity_id = VarInt.read(file_object)
        if self.context.protocol_version >= 49:
            self.object_uuid = UUID.read(file_object)
        self.type_id = Byte.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.data = Integer.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):
        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)