[Solved] Let entities follow specific players over large distances

Discussion in 'Spigot Plugin Development' started by Dani432123, May 10, 2015.

  1. I want to let a entity target (just to go to them, not to hit them) the nearest player of the opposite team, when a entity is spawned.

    Therefore I have written a methode:
    Code (Text):
        public void EntityFollow(final Team team , final Entity entity , final double speed)
        {
            new BukkitRunnable()
            {
                public void run()
                {
                    if ((!entity.isValid()))
                    {
                        this.cancel();
                    }
                    Player player = null;
                    double distance = Double.MAX_VALUE;
                       for(Player p : Bukkit.getServer().getOnlinePlayers())
                      {
                          if(entity.getLocation().distance(p.getLocation()) < distance && (getTeam(p) != team))
                          {
                             distance = entity.getLocation().distance(p.getLocation());
                             player = p;
                          }
                      }
                    if (player != null)
                    {
                        net.minecraft.server.v1_8_R2.Entity entity2 = ((CraftEntity) entity).getHandle();
                        ((EntityInsentient) entity2).getNavigation().a(2);
                        Object entityObject = ((CraftEntity) entity).getHandle();
                        Location targetLocation = player.getLocation();
                        PathEntity path;
                        path = ((EntityInsentient) entityObject).getNavigation().a(targetLocation.getX() + 1, targetLocation.getY(), targetLocation.getZ() + 1);
                        if (path != null)
                        {
                            ((EntityInsentient) entityObject).getNavigation().a(path, 1.0D);
                            ((EntityInsentient) entityObject).getNavigation().a(2.0D);
                        }
                        AttributeInstance attributes = ((EntityInsentient)((CraftEntity)entity).getHandle()).getAttributeInstance(GenericAttributes.d);
                        attributes.setValue(speed);
                    }
                }
            }.runTaskTimer(plugin, 0L, 20L);
        }

    This works, if the distance between the player and the entity is closer then 10 blocks. But I want this to work with even higher distances.
    I have already tried to increase the "entity-tracking-range" in the spigot.yml, but it didn't help.
     
    • Agree Agree x 1
  2. All right, but how can I do that?
     
  3. Set the value for GenericAttributes.b
     
    • Winner Winner x 1
  4. I have a class that can make a mob follow the player. Do you want it?
     
  5. Yes, but I don't want to teleport the entity, if it is too far.
     
  6. OK, that works. Thank you.

    Code (Text):
    AttributeInstance attributes2 = ((EntityInsentient)((CraftEntity)entity).getHandle()).getAttributeInstance(GenericAttributes.b);
    attributes2.setValue(500);
    Is the value (in my example 500) the radius of blocks?

    I noticed, when I tried this e.g. with a sheep, that it often fall of the edge of a bridge or stuck and run in a circle. Can I optimize that? Or can I cancel the other (normal) pathfinding goals of a sheep?
     
  7. As far as I know, yes
    you could try that. If nothing works, write your own pathfinding algorithm (though this might be a tad hard)
     
    • Like Like x 1