[TUT] Making a LivingEntity walk to a specific tile.

Discussion in 'Resources' started by Ivan, Apr 3, 2013.

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

    CrystalxNeth

    That looks pretty wierd, I've tried it :p
     
  2. Offline

    nitrousspark

    through lots of trying out new things i cant get pig zombies to walk to tiles, also, when i remove all the pathfinding goals from a pig zombie, they still seem to wander around. but very slowly, unlike zombies that just sit facing the same direction and stay in a big bunch.
     
  3. Offline

    strubelcopter

    Try this in yuor custom Zombie class:
    Code:java
    1. @Override
    2. public boolean bk() {
    3. return false;
    4. }

    It works with bats for me.
     
  4. Offline

    bars96

  5. Offline

    ZeusAllMighty11

    For anyone that has an issue where their mob will attempt to move every couple moments as opposed to constantly move to the tile, on the tick boolean (I believe it's tick?) set it to true, but above that make sure to call the navigate method.
     
  6. Offline

    ToastHelmi

    is there i way i could use thes to check if a moch CAN walk to ta tile
     
  7. Offline

    Nixodas

    Okay I'm a little confused...
    I'm getting an error that UnsafeList isn't the correct type for the field.
    See here

    Code:java
    1. package de.apfel.npc;
    2.  
    3. import java.lang.reflect.Field;
    4.  
    5. import org.bukkit.craftbukkit.v1_7_R3.util.UnsafeList;
    6.  
    7. import net.minecraft.server.v1_7_R3.EntityInsentient;
    8. import net.minecraft.server.v1_7_R3.EntityVillager;
    9. import net.minecraft.server.v1_7_R3.PathfinderGoalLookAtPlayer;
    10. import net.minecraft.server.v1_7_R3.World;
    11.  
    12. public class CustomVillager extends EntityVillager {
    13.  
    14. public CustomVillager(World w, int job) {
    15. super(w,job);
    16.  
    17. try{
    18.  
    19. Field gsa = net.minecraft.server.v1_7_R3.PathfinderGoalSelector.class.getDeclaredField("a");
    20. gsa.setAccessible(true);
    21.  
    22. gsa.set(this.goalSelector, new UnsafeList());
    23.  
    24. } catch (SecurityException e) {
    25. e.printStackTrace();
    26. } catch (NoSuchFieldException e) {
    27. e.printStackTrace();
    28. e.printStackTrace();
    29. } catch (IllegalAccessException e) {
    30. e.printStackTrace();
    31. }
    32.  
    33. this.goalSelector.a(10, new PathfinderGoalLookAtPlayer(this, EntityInsentient.class, 8.0F));
    34. }
    35. }
    36.  
    37.  
    38.  


    This is the whole class.
    I'm just trying to remove everything except for the villager to look at the player.

    I hope someone knows why it doesn't accept UnsafeList.

    Greetings,
    Nixodas
     
  8. Offline

    GermanCoding

    Nixodas In your error log it says that net.minecraft.server.v1_7_R3.PathfinderGoalSelector.a is an (final) logger field and not an unsafe list. Maybe the obfuscation changed and the field you are looking for has another name?
     
  9. Offline

    Nixodas

    GermanCoding
    I also thought so but
    Code:java
    1. this.goalSelector.a(...)

    doesn't throw any errors.

    And it's the same field I'm editing, right?
    Or did I mix something up?
     
  10. Offline

    GermanCoding

    Nixodas Maybe. I do not know what "this.goalSelector" is. It could be another type then "net.minecraft.server.v1_7_R3.PathfinderGoalSelector". Do you checked that?

    Nixodas But wait... I think in your reflection you call the field(!) a and in your last line you call a method(!). Maybe that is the problem?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  11. Offline

    Nixodas

    GermanCoding
    It's the method called in the original EntityVillager class to add the diffrent pathfinders, which is inherited to my CustomVillager class and the NMS says it's of the type PathfingerGoalSelector

    GermanCoding
    sorry I saw the second post to late.
    And yes I just looked it up the obfuscation changed it.
    Stupid me just looked up if it still contains a field a, not if it is the correct type ^^

    Thank you very much

    Greetings,
    Nixodas

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  12. Offline

    GermanCoding

    Nixodas I am happy that I could help somehow :D
     
  13. How can I make the entity walk along a path?
     
  14. Offline

    InfamousSheep

    What tick boolean are you referencing? Can anyone else help me with them not constantly walking to the tile?
    Here's my code:

    Code:
    public class PathfinderGoalMoveToBlock extends PathfinderGoal {
     
    private float speed;
    private EntityCreature entitycreature;
    private PathEntity path;
     
    private World world;
    private double x;
    private double y;
    private double z;
     
    public PathfinderGoalMoveToBlock(EntityCreature entitycreature, float speed, Location location) {
    this.speed = speed;
    this.entitycreature = entitycreature;
     
    this.world = location.getWorld();
    this.x = location.getX();
    this.y = location.getY();
    this.z = location.getZ();
    }
     
    @Override
    public boolean a() {
    Location targetLocation = new Location(world, x, y, z);
    boolean flag = this.entitycreature.getNavigation().c();
     
    this.entitycreature.getNavigation().b(false);
    this.path = this.entitycreature.getNavigation().a(targetLocation.getX(), targetLocation.getY(), targetLocation.getZ());
    this.entitycreature.getNavigation().b(flag);
     
    return this.path != null;
    }
     
    @Override
    public void c() { 
    this.entitycreature.getNavigation().a(this.path, (double) this.speed);
    }
    }
    
     
  15. Offline

    spacemoehre

    Ivan
    Can you make an example. I dont know how i could make the EntityZombie move or even spawn now
     
  16. Offline

    Ivan

    If you're still interested, hit me up through PM and I'll help you out, and will aswell update the topic with the current NMS variables
     
  17. Offline

    Freelix2000

    Awesome tutorial, but I have just one question. What would a good speed be? What is the float value for the speed at which a zombie or a skeleton walks or runs?
     
  18. Offline

    xTrollxDudex

  19. Offline

    GrandpaMizery

    I have written both a custom entity and custom pathGoal class. I can get the custom entity to spawn and despawn correctly. However all it does is stand there saying moo. The PathGoal class is a modified version of PathGoalFollowOwner class. Here is the code for both classes:
    CustomEntityCow (open)

    Code:
    package me.grandpamizery;
    
    import java.lang.reflect.Field;
    
    import org.bukkit.craftbukkit.v1_8_R1.util.UnsafeList;
    import org.bukkit.entity.Player;
    
    import net.minecraft.server.v1_8_R1.EntityCow;
    import net.minecraft.server.v1_8_R1.GenericAttributes;
    import net.minecraft.server.v1_8_R1.Navigation;
    import net.minecraft.server.v1_8_R1.PathfinderGoalFloat;
    import net.minecraft.server.v1_8_R1.PathfinderGoalRandomStroll;
    import net.minecraft.server.v1_8_R1.PathfinderGoalSelector;
    import net.minecraft.server.v1_8_R1.World;
    
    public class CustomEntityCow extends EntityCow {
        private final Player owner;
    
        public CustomEntityCow(World world, Player player) {
            super(world);
            this.owner = player;
            this.ageLocked = true;
            this.setAgeRaw(-2400);
            try {
                Field bField = PathfinderGoalSelector.class.getDeclaredField("b");
                bField.setAccessible(true);
                Field cField = PathfinderGoalSelector.class.getDeclaredField("c");
                cField.setAccessible(true);
                bField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
                bField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(goalSelector, new UnsafeList<PathfinderGoalSelector>());
                cField.set(targetSelector, new UnsafeList<PathfinderGoalSelector>());
            } catch (Exception exc) {
                exc.printStackTrace();
            }
            a(0.9F, 1.3F);
            ((Navigation)getNavigation()).a(true);
            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(1, new PetGoalFollowOwner(this, 1.0D, 10.0F, 2.0F));
            this.goalSelector.a(2, new PathfinderGoalRandomStroll(this, 1.0D));
        }
       
        @Override
        protected void aW() {
            super.aW();
            this.getAttributeInstance(GenericAttributes.b).setValue(2.0D);
        }   
    
        public Player getOwner() {
            return owner;
        }
       
        @Override
        public boolean isBaby() {
            return true;
        }
    }

    PetGoalFollowOwner (open)

    Code:
    package me.grandpamizery;
    
    import net.minecraft.server.v1_8_R1.BlockPosition;
    import net.minecraft.server.v1_8_R1.EntityLiving;
    import net.minecraft.server.v1_8_R1.MathHelper;
    import net.minecraft.server.v1_8_R1.Navigation;
    import net.minecraft.server.v1_8_R1.NavigationAbstract;
    import net.minecraft.server.v1_8_R1.PathfinderGoal;
    import net.minecraft.server.v1_8_R1.World;
    
    import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
    
    public class PetGoalFollowOwner extends PathfinderGoal{
        private CustomEntityCow pet;                   //d
        private EntityLiving owner;                    //e
        World world;                                   //a
        private double start;                          //f
        private NavigationAbstract nav;                //g
        private int h;                                 //h
        float teleport;                                //b
        float stop;                                    //c
        @SuppressWarnings("unused")
        private boolean i;                             //i
       
        public PetGoalFollowOwner(CustomEntityCow cow, double startDistance, float stopDistance, float teleportDistance) {
            pet = cow;
            world = pet.world;
            start = startDistance;
            stop = stopDistance;
            teleport = teleportDistance;
            nav = pet.getNavigation();
            a(3);
        }
    
        @Override
        public boolean a() {
            EntityLiving localOwner = ((CraftPlayer) this.pet.getOwner()).getHandle();
            if(localOwner == null) return false;
            if(this.pet.h(localOwner) < this.stop * this.stop) return false;
            owner = localOwner;
            return true;
        }
       
        @Override
        public boolean b() {
            return(!this.nav.m()) && (this.pet.h(this.owner) > this.teleport * this.teleport);
        }
       
       
       
        @Override
        public void c() {
            this.h = 0;
            this.i = ((Navigation) this.pet.getNavigation()).e();
            ((Navigation) this.pet.getNavigation()).a(true);
        }
       
        @Override
        public void d() {
            this.owner = null;
            this.nav.n();
            ((Navigation)this.pet.getNavigation()).a(true);
        }
       
        @Override
        public void e() {
            this.pet.getControllerLook().a(owner, 10.0f, this.pet.bP());
           
            if(--this.h >0) return;
            this.h = 10;
           
            if(this.nav.a(owner, start)) return;
           
            if(this.pet.cb()) return;
           
            if(this.pet.h(this.owner) < 144.0D) return;
           
            int j = MathHelper.floor(this.pet.locX) - 2;
            int k = MathHelper.floor(this.pet.locZ) - 2;
            int m = MathHelper.floor(this.pet.getBoundingBox().b);
            for(int n = 0; n <= 4; n++)
                for(int i1 = 0; i1 <= 4; i1++)
                    if((n < 1) || (i1 < 1) || (n > 3) || (i1 > 3)) {
                        if((World.a(this.world, new BlockPosition(j + n, m - 1, k + i1)))
                                && (!this.world.getType(new BlockPosition(j + n, m, k + i1)).getBlock().d())
                                && (!this.world.getType(new BlockPosition(j + n, m + 1, k + i1)).getBlock().d())) {
                            this.pet.setPositionRotation(j + n + 0.5F, m, k + i1 + 0.5F, this.pet.yaw, this.pet.pitch);
                            this.nav.n();
                            return;
                        }
                    }
        }
    }


    The cow doesn't even randomly stroll. I am sure it is a stupid mistake, but any help would be appreciated.
     
Thread Status:
Not open for further replies.

Share This Page