Help please

Discussion in 'Plugin Development' started by HelloThereItsMe, Mar 28, 2015.

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

    HelloThereItsMe

    Can you please give me tips on how to make a Mob follow a player? Thanks
     
  2. Offline

    HelloThereItsMe

    Huh?
     
  3. Offline

    WladHD

    I dont know if it works... but i think this could be right:
    Code:
    Entity e = yourEntity;
    if(e instanceof Creature) {
         Creature c = (Creature) e;
         c.setTarget(LivingEntity);
    }
    
    *Update*

    Ohh sorry... thats not what you want... but i have something for you... it is in one of my old plugins. You have to use Pathfindergoals.
    Code:
    package de.wladhd.npc;
    
    import java.lang.reflect.Field;
    import java.util.List;
    
    import net.minecraft.server.v1_7_R4.EntityHuman;
    import net.minecraft.server.v1_7_R4.EntityIronGolem;
    import net.minecraft.server.v1_7_R4.EntitySpider;
    import net.minecraft.server.v1_7_R4.EntityVillager;
    import net.minecraft.server.v1_7_R4.PathfinderGoalFloat;
    import net.minecraft.server.v1_7_R4.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_7_R4.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_7_R4.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_7_R4.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_7_R4.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_7_R4.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_7_R4.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_7_R4.PathfinderGoalRandomStroll;
    import net.minecraft.server.v1_7_R4.PathfinderGoalSelector;
    
    import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
    import org.bukkit.entity.Player;
    
    import de.wladhd.npc.pathfindergoals.PathfinderGoalFollowPlayer;
    
    public class CustomVillager extends EntityVillager {
    
        @SuppressWarnings("rawtypes")
        public CustomVillager(org.bukkit.World world, Player p) {
            super(((CraftWorld)world).getHandle());
            List goalB = (List)getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
            List goalC = (List)getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
            List targetB = (List)getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
            List targetC = (List)getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();
          
    
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
    
            //This is what you want
            this.goalSelector.a(0, new PathfinderGoalFollowPlayer(this, p, 1));
        }
      
        @SuppressWarnings("rawtypes")
        public static Object getPrivateField(String fieldName, Class clazz, Object object)
        {
            Field field;
            Object o = null;
    
            try
            {
                field = clazz.getDeclaredField(fieldName);
    
                field.setAccessible(true);
    
                o = field.get(object);
            }
            catch(NoSuchFieldException e)
            {
                e.printStackTrace();
            }
            catch(IllegalAccessException e)
            {
                e.printStackTrace();
            }
    
            return o;
        }
    
    }
    
    PathfinderGoalFollowPlayer Class:
    Code:
    package de.wladhd.npc.pathfindergoals;
    
    import net.minecraft.server.v1_7_R4.EntityInsentient;
    import net.minecraft.server.v1_7_R4.Navigation;
    import net.minecraft.server.v1_7_R4.PathEntity;
    import net.minecraft.server.v1_7_R4.PathfinderGoal;
    
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class PathfinderGoalFollowPlayer extends PathfinderGoal
    {
       private double speed;
    
       private EntityInsentient entity;
    
       private Player p;
    
       private Navigation navigation;
    
       public PathfinderGoalFollowPlayer(EntityInsentient entity, Player p, double speed)
       {
         this.entity = entity;
         this.p = p;
         this.navigation = this.entity.getNavigation();
         this.speed = speed;
       }
    
       public boolean a()
       {
          c();
          return true;
       }
      
       public void c()
       {
          if(p == null){
              return;
          }
          Location loc = p.getLocation();
           PathEntity pathEntity = this.navigation.a(loc.getX(), loc.getY(), loc.getZ());
           this.navigation.a(pathEntity, speed);
       }
    }
    
    This is the managing class:
    Code:
    package de.wladhd.npc;
    
    import java.lang.reflect.Field;
    import java.util.Map;
    
    import net.minecraft.server.v1_7_R4.Entity;
    
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
    import org.bukkit.entity.EntityType;
    
    
    @SuppressWarnings("deprecation")
    public enum CustomEntityTypes {
      
            VILLAGER("Villager", EntityType.VILLAGER.getTypeId(), CustomVillager.class)
      
      
        ; //You can add as many as you want.
    
        private CustomEntityTypes(String name, int id, Class<? extends Entity> custom)
        {
            addToMaps(custom, name, id);
        }
    
        public static void spawnEntity(Entity entity, Location loc) {
         entity.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
         ((CraftWorld)loc.getWorld()).getHandle().addEntity(entity);
       }
    
        @SuppressWarnings({ "unchecked", "rawtypes" })
        private static void addToMaps(Class clazz, String name, int id)
        {
            //getPrivateField is the method from above.
            //Remove the lines with // in front of them if you want to override default entities (You'd have to remove the default entity from the map first though).
            //((Map)getPrivateField("c", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name, clazz);
            ((Map)getPrivateField("d", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(clazz, name);
            //((Map)getPrivateField("e", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(Integer.valueOf(id), clazz);
            ((Map)getPrivateField("f", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(clazz, Integer.valueOf(id));
            //((Map)getPrivateField("g", net.minecraft.server.v1_7_R4.EntityTypes.class, null)).put(name, Integer.valueOf(id));
        }
      
        @SuppressWarnings("rawtypes")
        public static Object getPrivateField(String fieldName, Class clazz, Object object)
        {
            Field field;
            Object o = null;
    
            try
            {
                field = clazz.getDeclaredField(fieldName);
    
                field.setAccessible(true);
    
                o = field.get(object);
            }
            catch(NoSuchFieldException e)
            {
                e.printStackTrace();
            }
            catch(IllegalAccessException e)
            {
                e.printStackTrace();
            }
    
            return o;
        }
    }
    
    And this is how you can spawn the custom entity:
    Code:
    CustomEntityTypes.spawnEntity(new CustomVillager(p.getWorld(), p), spawningLocation);
    
     
    Last edited: Mar 28, 2015
  4. Offline

    ferrago

    @HelloThereItsMe You have to use Net Minecraft Server classes to modify the AI of the mob to make it follow the player. He is essentially saying its a bit complex to do. There is no set in stone friendly API to use for it. I would recommend starting a google search of Bukkit Mob AI, there should be tons of hits.
     
  5. Offline

    HelloThereItsMe

    Thanks but I got it!
     
Thread Status:
Not open for further replies.

Share This Page