1.14.4 About ride entity, how to hover at a certain height?

Discussion in 'Spigot Plugin Development' started by inrh, Feb 8, 2020.

  1. I recently developed a free pet plugin.

    I know how to solve the problem through MyPet source code.

    I got the source code for riding from MyPet, but after researching it all afternoon, it always had to land on the ground instead of hovering at a certain height.

    I want to know what code can be modified to hover at a certain height?
    Code (Java):

    @Override
    public void e(Vec3D vec3d) {
        if (this.passengers != null && !this.passengers.isEmpty()) {
            EntityLiving passenger = (EntityLiving) this.passengers.get(0);

            if (this.a(TagsFluid.WATER)) {
                this.setMot(this.getMot().add(0, 0.4, 0));
            }

            this.lastYaw = (this.yaw = passenger.yaw);
            this.pitch = passenger.pitch * 0.5F;
            setYawPitch(this.yaw, this.pitch);
            this.aM = (this.aK = this.yaw);

            double motionSideways = passenger.bb * 0.5F;
            double motionForward = passenger.bd;

            if (motionForward <= 0.0F) {
                motionForward *= 0.25F;
            }

            motionSideways *= 0.85F;

            float speed = 0.2F;

            ride(motionSideways, motionForward, vec3d.y, speed);

            if (jump != null && this.isVehicle()) {
                boolean doJump = false;
                if (jump != null) {
                    try {
                        doJump = jump.getBoolean(passenger);
                    } catch (IllegalAccessException ignored) {
                    }
                }

                double jumpHeight = 0.3F;
                float ascendSpeed = 0.2f;

                if (doJump) {
                    if (this.getMot().y < ascendSpeed) {
                        this.setMot(this.getMot().x, ascendSpeed, this.getMot().z);
                    }
                }
            }
        }
        super.a(vec3d);
    }

    private void ride(double motionSideways, double motionForward, double motionUpwards, float speedModifier) {
        double locY;
        float f2;
        float speed;
        float swimSpeed;

        if (this.b(TagsFluid.WATER)) {
            locY = this.locY;
            speed = 0.8F;
            swimSpeed = 0.02F;

            this.a(swimSpeed, new Vec3D(motionSideways, motionUpwards, motionForward));
            this.move(EnumMoveType.SELF, this.getMot());
            double motX = this.getMot().x * (double) speed;
            double motY = this.getMot().y * 0.800000011920929D;
            double motZ = this.getMot().z * (double) speed;
            motY -= 0.02D;
            if (this.positionChanged && this.d(this.getMot().x, this.getMot().y + 0.6000000238418579D - this.locY + locY, this.getMot().z)) {
                motY = 0.30000001192092896D;
            }
            this.setMot(motX, motY, motZ);
        } else if (this.b(TagsFluid.LAVA)) {
            locY = this.locY;
            this.a(0.02F, new Vec3D(motionSideways, motionUpwards, motionForward));
            this.move(EnumMoveType.SELF, this.getMot());
            double motX = this.getMot().x * 0.5D;
            double motY = this.getMot().y * 0.5D;
            double motZ = this.getMot().z * 0.5D;
            motY -= 0.02D;
            if (this.positionChanged && this.d(this.getMot().x, this.getMot().y + 0.6000000238418579D - this.locY + locY, this.getMot().z)) {
                motY = 0.30000001192092896D;
            }
            this.setMot(motX, motY, motZ);
        } else {
            float friction = 0.91F;

            speed = speedModifier * (0.16277136F / (friction * friction * friction));

            this.a(speed, new Vec3D(motionSideways, motionUpwards, motionForward));
            friction = 0.91F;

            double motX = this.getMot().x;
            double motY = this.getMot().y;
            double motZ = this.getMot().z;

            if (this.isClimbing()) {
                swimSpeed = 0.15F;
                motX = MathHelper.a(motX, -swimSpeed, swimSpeed);
                motZ = MathHelper.a(motZ, -swimSpeed, swimSpeed);
                this.fallDistance = 0.0F;
                if (motY < -0.15D) {
                    motY = -0.15D;
                }
            }

            Vec3D mot = new Vec3D(motX, motY, motZ);

            this.move(EnumMoveType.SELF, mot);
            if (this.positionChanged && this.isClimbing()) {
                motY = 0.2D;
            }

            motY -= 0.08D;

            motY *= 0.9800000190734863D;
            motX *= friction;
            motZ *= friction;

            this.setMot(motX, motY, motZ);
        }

        this.aE = this.aF;
        locY = this.locX - this.lastX;
        double d1 = this.locZ - this.lastZ;
        f2 = MathHelper.sqrt(locY * locY + d1 * d1) * 4.0F;
        if (f2 > 1.0F) {
            f2 = 1.0F;
        }

        this.aF += (f2 - this.aF) * 0.4F;
        this.aG += this.aF;
    }
     

    Thank you.
     
    #1 inrh, Feb 8, 2020
    Last edited: Feb 9, 2020
  2. Hi, I have modified the content of the problem and hope to resolve it.
     
  3. If I got your question right, you need multiple entities to properly balance the y axis offset when riding an entity. So you would do something like: Pet -> Invisible Slime (Slimes are good owing that they can scale) -> Player. Correct me if I am wrong, but you cannot server sidedly define the client sided height of an entity, slimes being a special exception.