Making a mob follow the player

Discussion in 'Plugin Development' started by JPG2000, Aug 3, 2013.

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

    JPG2000

    I was wondeirng, is there a simple way to make a mob follow a player? Like a method? Because I cant find anything. Thanks!
     
  2. Offline

    tommycake50

    Yes it easy!
    mob.setTarget(player);
    passive will follow hostile will try to kill lol.
     
    KingFaris11 likes this.
  3. Offline

    JPG2000

    tommycake50 Lol. Thanks!

    tommycake50 it deosnt work!! The method doesnt exsist.
    Code:
    Entity mob = player.getWorld().spawnEntity(player.getLocation(), (EntityType)Menu.CHOICES.get(player.getName()));
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  4. Offline

    xTrollxDudex

    JPG2000
    setTarget is broken for most mobs, use Pathfinding(super accurate) or Navigation(just follows)
     
  5. Offline

    DarkBladee12

    JPG2000 Try this method:

    Code:java
    1. /**
    2.   * Forces a living entity to walk to a location with a given velocity
    3.   */
    4. private void navigate(LivingEntity le, Location loc, double velocity) {
    5. try {
    6. Object entityLiving = ReflectionUtil.getMethod("getHandle", le.getClass(), 0).invoke(le);
    7. Object navigation = ReflectionUtil.getMethod("getNavigation", entityLiving.getClass(), 0).invoke(entityLiving);
    8. ReflectionUtil.getMethod("a", navigation.getClass(), 4).invoke(navigation, loc.getX(), loc.getY(), loc.getZ(), velocity);
    9. } catch (Exception e) {
    10. e.printStackTrace();
    11. }
    12. }


    ReflectionUtil.class:

    Code:java
    1. import java.lang.reflect.Constructor;
    2. import java.lang.reflect.Field;
    3. import java.lang.reflect.Method;
    4.  
    5. import org.bukkit.Bukkit;
    6.  
    7. public class ReflectionUtil {
    8. public static Object getClass(String name, Object... args) throws Exception {
    9. Class<?> c = Class.forName(ReflectionUtil.getPackageName() + "." + name);
    10. int params = 0;
    11. if (args != null) {
    12. params = args.length;
    13. }
    14. for (Constructor<?> co : c.getConstructors()) {
    15. if (co.getParameterTypes().length == params) {
    16. return co.newInstance(args);
    17. }
    18. }
    19. return null;
    20. }
    21.  
    22. public static Method getMethod(String name, Class<?> c, int params) {
    23. for (Method m : c.getMethods()) {
    24. if (m.getName().equals(name) && m.getParameterTypes().length == params) {
    25. return m;
    26. }
    27. }
    28. return null;
    29. }
    30.  
    31. public static void setValue(Object instance, String fieldName, Object value) throws Exception {
    32. Field field = instance.getClass().getDeclaredField(fieldName);
    33. field.setAccessible(true);
    34. field.set(instance, value);
    35. }
    36.  
    37. public static String getPackageName() {
    38. return "net.minecraft.server." + Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];
    39. }
    40. }
     
  6. Offline

    JPG2000

  7. Offline

    mattrick

    JPG2000
    DarkBladde's should work.
     
  8. Offline

    xTrollxDudex

    JPG2000
    Since I'm not too good with NMS I'll leave pathfinding
    Use navigation:
    PHP:
    //lets have a field for the entity location called l
    Navigation nav = ((EntityInsentient)((CraftEntitye).getHandle()).getNavigation();
    nav.a(l.getX(), l.getY(), l.getZ(), 0.3f);
     
  9. Offline

    JPG2000

    xTrollxDudex It doesnt work, I get loads of erorrs. So are there any other quick and easy ways? Im desperate D:
     
Thread Status:
Not open for further replies.

Share This Page