Solved Pathfinding issue when player leaves close range

Discussion in 'Plugin Development' started by ski23, May 14, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    ski23

    I am having an issue that, when a player is withing close range of my custom mob, the mob moves correctly. However, when the player moves back further away, the mob stops/ slows down. I think this might be due to a chunk loading issue. I tried loading the chunk that the mob is in every time the navigation is set if the chunk is not already loaded. That did not seem to have any effect. Here is a video demonstrating the issue:

    If anyone has any ideas on how to fix this, it would be very much appreciated. Thanks in advance!
     
  2. Offline

    ChipDev

    Are you using NMS to create the mobs? Because I believe there is an NBT tag which controls how far a mob will walk until it gives up.
    EDIT; Of course you are using NMS. Hmm, have you changed some options with that? I can do some reasearch and find it.
     
  3. Offline

    ski23

    This is the code that I am using. This method is inside of my custom zombie and it gets called every tick:
    Code:
    public void setPath()
        {
            Entity entity = this.getBukkitEntity();
            Location currentLoc = entity.getLocation();
            if (!currentLoc.getChunk().isLoaded())
            {
                currentLoc.getChunk().load();
                MyLogger.info("loading chunk");
            }
            if (pathToken + 1 <= path.length)
            {
                if (this.getBukkitEntity().getLocation().distance(path[pathToken]) <= .5)
                {
                    pathToken ++;
                }
                Location locationToNavigate = path[pathToken];
                if (currentLoc.distance(locationToNavigate) >= 20)
                {
                    double xDiff = locationToNavigate.getX() - currentLoc.getX();
                    double zDiff = locationToNavigate.getZ() - currentLoc.getZ();
                    double absX = Math.abs(xDiff);
                    double absZ = Math.abs(zDiff);
                    if (absX == Math.max(absX,absZ))
                    {
                        if (xDiff >= 0)
                        {
                            locationToNavigate = currentLoc.add(20, 0, 0);
                        }
                        else
                        {
                            locationToNavigate = currentLoc.add(-20, 0, 0);
                        }
                    }
                    else
                    {
                        if (zDiff >= 0)
                        {
                            locationToNavigate = currentLoc.add(0, 0, 20);
                        }
                        else
                        {
                            locationToNavigate = currentLoc.add(0, 0, -20);
                        }
                    }
                }
                this.getNavigation().a(locationToNavigate.getX(), locationToNavigate.getY(), locationToNavigate.getZ(), speed);
                //MyLogger.info("Navigating to " + path[pathToken]);
            }
            else
            {
                MyLogger.info("BOOM");
            }
        }
     
  4. Offline

    ski23

    bump
     
  5. Offline

    ski23

    @Zombie_Striker Do you have any ideas of why this may be happening or any suggestions on how to fix it.
    I'm not asking for it coded for me nor do I want that. I want to understand why this is happening and then fix it for myself.
    @ChipDev Do you have any ideas either?
     
    Last edited: May 16, 2016
  6. Offline

    ski23

    bump
     
  7. Offline

    Irantwomiles

    I read somewhere that if you're 32 blocks away from the mob they will stop moving.
     
  8. Offline

    ski23

    @Irantwomiles A mob has a pathfinding range of 32. Aka if a mob is set to path somewhere further than 32 blocks, it wont work. In this case, the mob isnt set to pathfind towards the player so, I dont believe that is the issue. I read somewhere that spigot ticks entities slower if players are further away. I think that may be the issue. However, I don't have any clue if it is for sure or if so how to fix it.
     
    Irantwomiles likes this.
  9. Offline

    Irantwomiles

    Oh ok. Just trying help.
     
  10. Offline

    ski23

  11. Offline

    ski23

    I have it solved! For any whom it may concern, I overrode the m() method in my custom entity and added this line:
    Code:
    activatedTick = MinecraftServer.currentTick;
    Make sure to also call the super m() method as well though.
     
    A5H73Y likes this.
Thread Status:
Not open for further replies.

Share This Page