Solved Making mobs follow a player.

Discussion in 'Plugin Development' started by mollekake, Nov 10, 2012.

Thread Status:
Not open for further replies.
  1. In a plugin i'm working on, i want specified mobs to follow specified players.
    How would i go on and do this? I want to make the mobs follow the player when the player is x nummer of blocks away.
     
  2. nope they are dead xd

    I can't find anything about that sorry
     
  3. Offline

    fireblast709

    why not a constant stalk? (like make them follow at a few blocks distance, and teleport if the player teleports)
     
  4. how could i do that?
     
  5. Offline

    fireblast709

    Updating their target location to the location of the player, and if the player is too far teleport. You kinda could try making them act like wolves that cannot sit
     
  6. Offline

    libraryaddict

    By using some nms calls you can extend the range of the mobs.

    But without the calls. To make a mob target a living entity.

    mob.setTarget(player)
     
  7. Wont that make them attack?
     
  8. Offline

    libraryaddict

    YES

    I think you may have to do some nms stuff..
     
  9. anyone else know how this can be done?
    is there a way to check if the mob is too far away from the player?
     
  10. Offline

    fireblast709

    yes, it is called the distance between 2 Location objects
    Code:java
    1. public double getDistance(Location from, Location to)
    2. {
    3. from.distanceSquared(to); // gets the distance squared, a bit faster than the distance() function
    4. }
     
  11. yeah that gets the distance, but how would i know when to call it?
    on playermove? that would be very resource heavy
     
  12. Offline

    fireblast709

    If you do not want that, use a scheduler
     
  13. still stuck on this one. anyone know how to make a mob follow?
     
  14. Offline

    Zidkon

    Fireblast709 means you can use both events like PlayerMoveEvent or a Scheduler so inside it you calculate the distance between player and mob and then make the mob move if the distance is greater than a specified number.

    libraryaddict means that you can modify the mob itself so it targets a player constatly and so it will follow him to attack the player, and you can modify the mob too so the mob will have no attack behavior, so it will "follow".

    This both methods will just change the way the mob will follow the player, the first one will be you decide how the mob will follow, the second one will be the server algorithm decides how the mob will follow

    I think with this you can do pretty good research about Player movement check, distance between player and mob check, making a mob to move.

    When you have some of this and still have troubles come again to post ;)
     
    fireblast709 likes this.
  15. yeah i understand what they mean, but it's the targeting i don't like. i would love to do this without setting the player as the target. also, wouldn't this make the mob hug the player? would be annoying and if not againts it's purpose if the mob can push the player.
     
  16. Offline

    Zidkon

    So you can always use the events, move the mob like you want, if you don't do it for every mob on the world, it shouldn't cause lag, but U must try it anyways.
     
  17. i've figured out that the setTarget function don't work, so i've got to come up with something else i guess. maybe some navigation editing
     
  18. Solved this by using a scheduler, which is i think the better way:
    Code:java
    1. public static int Task;
    2. public void followTask(){
    3. Task = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    4.  
    5. public void run() {
    6. if(plugin.getServer().getOnlinePlayers().length < 1) plugin.getServer().getScheduler().cancelAllTasks();
    7. walkTo();
    8. }
    9. }, 20L, 20L);
    10. }
    11.  
    12. public void walkTo(){
    13. System.out.print("checking");
    14. for(Player player : plugin.getServer().getOnlinePlayers()){
    15. for(LivingEntity entity : plugin.getServer().getWorld(plugin.getConfig().getString("World")).getLivingEntities()){
    16. if((EntityListener.mobowners.get(entity.getUniqueId())) == player.getName()){
    17. if(getDistance(player.getLocation(), entity.getLocation()) > 5){
    18. float speed = 0.23F;
    19. EntityListener.walkTo(entity, player.getLocation(), speed);
    20. }
    21. }
    22. }
    23. }
    24. }
    25.  
    26. public double getDistance(Location from, Location to)
    27. {
    28. return from.distanceSquared(to);
    29. }
     
  19. Offline

    Retherz_

  20. Yes.... Because a zombie will follow and not attack if you have wheat....
     
  21. Offline

    Retherz_

    You never said anything about that ;)
     
  22. I never saidi only want cows and sheeps to follow either ;) anyway. Problem solved.
     
  23. Offline

    Zidkon

    Haha thats nice, we never realize wheat, great one!
     
Thread Status:
Not open for further replies.

Share This Page