Solved Entity Pathfinding / Navigation needs to fall down, how?

Discussion in 'Spigot Plugin Development' started by jusjus112, Oct 8, 2019.

  1. Hey guys,

    Is there a way to force entities to fall down 20 or more blocks even if fall damage is cancelled with an event?
    I currently got the pathfinding goal to only just the player, and I even tried this:

    Code (Java):
    Player closest = null;
                for(Entity entity : getBukkitEntity().getNearbyEntities(200, 50, 200)){
                    if (entity instanceof Player){
                        Player player = (Player) entity;
                        closest = player;
                        break;
                    }
                }
                if (closest != null){
                    Location loc = closest.getLocation();
                    this.getNavigation().a(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 3 * 2.2 / getBukkitEntity().getLocation().distance(closest.getLocation()));
                }
    But even that doesn't let the entity fall down a lot of blocks.
    Maybe there is a way to do this, but even Google can't find the answer :(
     
  2. It's possible by modifying the entity's Navigation. This object manage entity's pathfinding
     
  3. Omg, yes I know how to edit / change the pathfinding for an entity. The problem is that I can't figure out why they don't jump down a 20 block height even if damage is cancelled with an event.
     
  4. I know. And to do that you need to change the entity's Navigation
     
  5. I'm just asking how I can let the entity fall down a bunch of blocks with either the navigation or pathfinding goals?
    There is just nothing to find on google, and yes I've searched like hours and even searched in NMS but as you know it's like pretty hard to understand.
     
  6. Oke. I don't know how. But I figured it out. I digged down the entire NMS classes to see how functions worked and what they might do.
    So my custom entity is now walking towards the closest player without being afraid to fall down a 30 block cliff.
    If any is interested in the code (For people in the future with the same problem) there ya go:

    Code (Java):
    import net.minecraft.server.v1_14_R1.*;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_14_R1.entity.CraftPlayer;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.entity.EntityTargetEvent;

    public class EnemyZombie extends EntityZombie {

        private static int count = 0;

        public EnemyZombie (EntityTypes<? extends EntityMonster> entitytypes, World world) {
            super(EntityTypes.ZOMBIE, world);

            // Setting the options
            this.setBaby(false);
            this.setAirTicks(0);
            this.setCanPickupLoot(false);
            this.setJumping(true);
            this.setPersistent();


            LivingEntity closest = null;
            for(Entity entity : getBukkitEntity().getNearbyEntities(100, 50, 100)){
                if (entity instanceof Player){
                    LivingEntity player = ((Player) entity);
                    closest = player;
                    break;
                }
            }
            if (closest != null){
                EntityLiving entityLiving = ((CraftPlayer) closest).getHandle();
                Location loc = closest.getLocation();

                this.getNavigation().a(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ(), 1D);
                this.setGoalTarget(entityLiving, EntityTargetEvent.TargetReason.CLOSEST_ENTITY, false);
            }

        }

        @Override
        protected void initAttributes() {
            super.initAttributes();

            this.getAttributeInstance(GenericAttributes.MAX_HEALTH).setValue(50D);
            this.getAttributeInstance(GenericAttributes.MOVEMENT_SPEED).setValue(0.296D);

            // Attack
            this.getAttributeInstance(GenericAttributes.ATTACK_KNOCKBACK).setValue(3);
            this.getAttributeInstance(GenericAttributes.ATTACK_DAMAGE).setValue(3);
        }

        @Override
        protected void initPathfinder() {
            // Disable the minecraft default pathfinding
    //        super.initPathfinder();

            this.goalSelector.a(8, new PathfinderGoalLookAtPlayer(this, EntityHuman.class, 8.0F));
            this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 400D));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget(this, EntityHuman.class, true));
        }

        @Override
        protected void l() {
            this.goalSelector.a(2, new PathfinderGoalZombieAttack(this, 1.0D, false));
        }
    }