Solved 1.13 WASD mount controls help

Discussion in 'Spigot Plugin Development' started by FalconSeeker, May 17, 2019.

Thread Status:
Not open for further replies.
  1. Can someone point me in the right direction with this ^
     
  2. Currently trying: I don't know the right packet name though :O
    Code (Text):
        @EventHandler
        public void onMove(PlayerMoveEvent e) {
            e.getPlayer().sendMessage(String.valueOf(getForward(e.getPlayer())));
            e.getPlayer().sendMessage(String.valueOf(getStrafe(e.getPlayer())));

        }
        private float getForward(Player player) {
            try {
                return ((EntityPlayer) ((CraftPlayer) player).getHandle()).getClass().getSuperclass().getSuperclass().getDeclaredField("bg").getFloat(((CraftPlayer) player).getHandle());
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }

        private float getStrafe(Player player) {
            try {
                return ((EntityPlayer) ((CraftPlayer) player).getHandle()).getClass().getSuperclass().getSuperclass().getDeclaredField("be").getFloat(((CraftPlayer) player).getHandle());
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
        }
     
  3. I think the PlayerMoveEvent only gets fired when he actually moves and not based on what key he presses, so I don't think your code will work. But tbh, I have no clue how to do it correctly
     
  4. Take a look at how @Keyle does it in MyPet: EntityMyPet class; lines: 721, 1127, and 1178
     
    • Like Like x 1
  5. Thanks, I'm going to try this soon, it looks promising. Going to mark as solved until I try a few things out
     
  6. This is what I have now, but I don't know how to get it to fly
    Code (Text):
    package me.falconseeker.mounts.mounts;

    import java.lang.reflect.Field;
    import java.math.BigDecimal;
    import java.util.Collection;
    import java.util.Set;
    import net.minecraft.server.v1_13_R2.BlockPosition;
    import net.minecraft.server.v1_13_R2.Entity;
    import net.minecraft.server.v1_13_R2.EntityHorseAbstract;
    import net.minecraft.server.v1_13_R2.EntityHuman;
    import net.minecraft.server.v1_13_R2.EntityLiving;
    import net.minecraft.server.v1_13_R2.EntityPig;
    import net.minecraft.server.v1_13_R2.EnumMoveType;
    import net.minecraft.server.v1_13_R2.GenericAttributes;
    import net.minecraft.server.v1_13_R2.IJumpable;
    import net.minecraft.server.v1_13_R2.MathHelper;
    import net.minecraft.server.v1_13_R2.NBTTagCompound;
    import net.minecraft.server.v1_13_R2.PathfinderGoalFloat;
    import net.minecraft.server.v1_13_R2.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_13_R2.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_13_R2.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_13_R2.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_13_R2.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_13_R2.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_13_R2.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_13_R2.PathfinderGoalSelector;
    import net.minecraft.server.v1_13_R2.TagsFluid;
    import net.minecraft.server.v1_13_R2.World;

    public class CustomPig extends EntityPig {

        private final double MOUNT_SPEED = 0.0d;
        private final double MOUNT_FOLLOW = 4.0d;
        protected boolean canFly = true;
        protected boolean isFlying = false;
        private static Field jump = ReflectionUtil.getField(EntityLiving.class, "bg");
        protected int flyCheckCounter = 0;
        protected float jumpPower = 0;
        protected float limitCounter = 0;
     
        public CustomPig(World world) {
          super(world);
       
          Set<?>  goalB = (Set<?> )getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
          Set<?>  goalC = (Set<?> )getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
          Set<?>  targetB = (Set<?> )getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
          Set<?>  targetC = (Set<?> )getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
           
          this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget<EntityHuman>(this, EntityHuman.class, 1, true, false, null));
          this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(MOUNT_SPEED);
          this.getAttributeInstance(GenericAttributes.FOLLOW_RANGE).setValue(MOUNT_FOLLOW);
       
          this.goalSelector.a(0, new PathfinderGoalFloat(this));
          this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
          this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 0.2D));
          this.goalSelector.a(4, new PathfinderGoalMeleeAttack(this, 1.0, true));
          this.goalSelector.a(6, new PathfinderGoalMoveThroughVillage(this, 0.2D, false));
          this.goalSelector.a(8, new PathfinderGoalRandomLookaround(this));
          this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, false));
       
         }
        private void ride(float motionSideways, float f, float motionForward, float speedModifier) {
            double locY;
            float f2;
            float speed;
            float swimmSpeed;

            if (this.isInWater()) {
                locY = this.locY;
                speed = 0.8F;
                swimmSpeed = 0.02F;

                this.a(motionSideways, motionForward, f, swimmSpeed);
                this.move(EnumMoveType.SELF, this.motX, this.motY, this.motZ);
                this.motX *= (double) speed;
                this.motY *= 0.800000011920929D;
                this.motZ *= (double) speed;
                this.motY -= 0.02D;
                if (this.positionChanged && this.c(this.motX, this.motY + 0.6000000238418579D - this.locY + locY, this.motZ)) {
                    this.motY = 0.30000001192092896D;
                }
            } else if (this.ax()) { // in lava
                locY = this.locY;
                this.a(motionSideways, motionForward, f, 0.02F);
                this.move(EnumMoveType.SELF, this.motX, this.motY, this.motZ);
                this.motX *= 0.5D;
                this.motY *= 0.5D;
                this.motZ *= 0.5D;
                this.motY -= 0.02D;
                if (this.positionChanged && this.c(this.motX, this.motY + 0.6000000238418579D - this.locY + locY, this.motZ)) {
                    this.motY = 0.30000001192092896D;
                }
            } else {
                double minY = this.getBoundingBox().minY;      

                float friction = 0.91F;
                if (this.onGround) {
                    friction = this.world.getType(new BlockPosition(MathHelper.floor(this.locX), MathHelper.floor(minY) - 1, MathHelper.floor(this.locZ))).getBlock().n() * 0.91F;
                }

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

                this.a(motionSideways, motionForward, f, speed);
                friction = 0.91F;
                if (this.onGround) {
                    friction = this.world.getType(new BlockPosition(MathHelper.floor(this.locX), MathHelper.floor(minY) - 1, MathHelper.floor(this.locZ))).getBlock().n() * 0.91F;
                }

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

                this.move(EnumMoveType.SELF, this.motX, this.motY, this.motZ);
                if (this.positionChanged && this.z_()) {
                    this.motY = 0.2D;
                }

                this.motY -= 0.08D;

                this.motY *= 0.9800000190734863D;
                this.motX *= (double) friction;
                this.motZ *= (double) friction;
            }

            this.aI = this.aJ;
            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.aJ += (f2 - this.aJ) * 0.4F;
            this.aK += this.aJ;
        }
        public void a(float motionSideways, float motionForward, float f) {
            super.a(motionSideways, motionForward, f);

            if (this.onGround && this.isFlying) {
                isFlying = false;
                this.fallDistance = 0;
            }

            EntityLiving passenger = null;
         
            for (Entity pass : this.getAllPassengers()) {
                passenger = (EntityLiving) pass;
                break;
            }

            if (passenger == null) return;
            if (this.a(TagsFluid.WATER)) {
                this.motY += 0.4;
            }

            //apply pitch & yaw
            this.lastYaw = (this.yaw = passenger.yaw);
            this.pitch = passenger.pitch * 0.5F;
            setYawPitch(this.yaw, this.pitch);
            this.aS = (this.aQ = this.yaw);

            // get motion from passenger (player)
            motionSideways = passenger.bh * 0.5F / 10F;
            motionForward = passenger.bj / 10F;

            // backwards is slower
            if (motionForward <= 0.0F) {
                motionForward *= 0.25F;
            }
            // sideways is slower too but not as slow as backwards
            motionSideways *= 0.85F;

            float speed = 0.22222F * (1F + (111111 / 1000F));  //rideSkill.getSpeedIncrease().getValue()
            double jumpHeight = 1.5;
            float ascendSpeed = 0.2f;

            ride(motionSideways, motionForward, f, 1); // apply motion

            if (jump != null && this.isVehicle()) {
                boolean doJump = false;
                if (this instanceof IJumpable) {
                    if (this.jumpPower > 0.0F) {
                        doJump = true;
                        this.jumpPower = 0.0F;
                    } else if (!this.onGround && jump != null) {
                        try {
                            doJump = jump.getBoolean(passenger);
                        } catch (IllegalAccessException ignored) {
                        }
                    }
                } else {
                    if (jump != null) {
                        try {
                            doJump = jump.getBoolean(passenger);
                        } catch (IllegalAccessException ignored) {
                        }
                    }
                }

                if (doJump) {
                    if (onGround) {
                        jumpHeight = new BigDecimal(jumpHeight).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                        String jumpHeightString = JumpHelper.JUMP_FORMAT.format(jumpHeight);
                        Double jumpVelocity = JumpHelper.JUMP_MAP.get(jumpHeightString);
                        jumpVelocity = jumpVelocity == null ? 0.44161199999510264 : jumpVelocity;
                        if (this instanceof IJumpable) {
                            getAttributeInstance(EntityHorseAbstract.attributeJumpStrength).setValue(jumpVelocity);
                        }
                        this.motY = jumpVelocity;
                    } else {
                        if (limitCounter <= 0) {
                            canFly = false;
                        } else if (flyCheckCounter-- <= 0) {
                            canFly = true;
                            if (canFly) {
                                canFly = false;
                            }
                            flyCheckCounter = 5;
                        }
                        if (canFly) {
                            if (this.motY < ascendSpeed) {
                                this.motY = ascendSpeed;
                                this.fallDistance = 0;
                                this.isFlying = true;
                            }
                        }
                    }
                } else {
                    flyCheckCounter = 0;
                }
            }
        }

        public static Object getPrivateField(String fieldName, Class<PathfinderGoalSelector> clazz, Object object) {
            Field field;
            Object o = null;
            try {
                field = clazz.getDeclaredField(fieldName);
                field.setAccessible(true);
                o = field.get(object);
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return o;
        }
    }
    It moves on the floor correctly, but just won't fly
     
    #6 FalconSeeker, May 18, 2019
    Last edited: May 18, 2019
  7. Essentially you just want to grab the players "jump" variable to check if the player wants to jump, then go from there. See the conditional on line 1203, specifically line 1234 and below. If you don't understand it don't hesitate to reply!
     
    • Like Like x 1
  8. Yeah, I don't understand. This is what I am trying
    Code (Text):
                if (doJump) {
                    if (onGround) {
                        jumpHeight = new BigDecimal(jumpHeight).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
                        String jumpHeightString = JumpHelper.JUMP_FORMAT.format(jumpHeight);
                        Double jumpVelocity = JumpHelper.JUMP_MAP.get(jumpHeightString);
                        jumpVelocity = jumpVelocity == null ? 0.44161199999510264 : jumpVelocity;
                        if (this instanceof IJumpable) {
                            getAttributeInstance(EntityHorseAbstract.attributeJumpStrength).setValue(jumpVelocity);
                        }
                        this.motY = jumpVelocity;
                    } else {
                            if (canFly) {
                                canFly = false;
                            }
                            else {
                                canFly = true;
                            }
                        }
                        if (canFly) {
                            if (this.motY < ascendSpeed) {
                                this.motY = ascendSpeed;
                                this.fallDistance = 0;
                                this.isFlying = true;
                            }
                        }
                    }
                } else {
                    flyCheckCounter = 0;
                }
    it works If I hold down space, but it doesn't toggle flight by double tapping
     
    #8 FalconSeeker, May 19, 2019
    Last edited: May 19, 2019
  9. Can you explain a little bit more what you mean by this? What is your desired output?

    I'm not for sure, but I think PlayerToggleFlightEvent doesn't get called when the player double taps while riding an entity
     
    • Like Like x 1
  10. Alright, thanks so much for the help, I kind of like the holding space effect instead of toggling it
     
    • Friendly Friendly x 1
Thread Status:
Not open for further replies.