Mob walking to a specific tile

Discussion in 'Plugin Development' started by ski23, Jan 21, 2016.

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

    ski23

    The title says it. I'm having trouble making a mob walk to a specific location. I have been looking through countless tutorials and posts but yet, no matter how many ways I try it, the mob never walks the direction. Before you ask, the distance is not more than 16 blocks away. Is there some obvious mistake that I don't see? Here is my code:
    Pathfinder:
    Pathfinder (open)

    Code:
    package CustomMobs;
    
    import java.util.ArrayList;
    import java.util.logging.Logger;
    
    import org.bukkit.Location;
    
    import net.minecraft.server.v1_8_R2.EntityCreature;
    import net.minecraft.server.v1_8_R2.EntityInsentient;
    import net.minecraft.server.v1_8_R2.EntityMonster;
    import net.minecraft.server.v1_8_R2.Navigation;
    import net.minecraft.server.v1_8_R2.NavigationAbstract;
    import net.minecraft.server.v1_8_R2.PathEntity;
    import net.minecraft.server.v1_8_R2.PathfinderGoal;
    
    
    public class PathfinderGoalWalkToLoc extends PathfinderGoal
    {
       private double speed;
    
       private EntityMonster entity;
    
       private ArrayList<Location> loc;
    
       private Navigation navigation;
     
       private int currentLoc;
       protected final Logger MyLogger = Logger.getLogger("Minecraft");
    
       public PathfinderGoalWalkToLoc(EntityMonster entity, ArrayList<Location> loc, double speed)
       {
         this.entity = entity;
         this.loc = loc;
         this.currentLoc = 1;
         this.navigation = (Navigation) this.entity.getNavigation();
         this.speed = speed;
       }
       public boolean a()
       {
           MyLogger.info("inside pathfinder a");
        if (this.entity.getGoalTarget() != null)
        {
            MyLogger.info("goal target not null");
            return false;
        }
        if (currentLoc + 1 < this.loc.size())
        {
               if (this.entity.getBukkitEntity().getLocation().distance(this.loc.get(currentLoc + 1)) <= 15){
                   currentLoc++;
                   MyLogger.info("true");
                   return true;
               }
            
             if (this.entity.getBukkitEntity().getLocation().distance(this.loc.get(currentLoc)) <= 15)
             {
                 MyLogger.info("true");
                   return true;
               }
           } else {
               MyLogger.info("currenctloc + 1 >= this.loc.size");
               if (this.entity.getBukkitEntity().getLocation().distance(this.loc.get(currentLoc)) > 15)
               {
                   MyLogger.info("false");
                   return false;
               }
           }
       [/SPOILER]
         return false;
       }
       public void c()
       {
           MyLogger.info("inside pathfinder c");
           PathEntity pathEntity = this.navigation.a(loc.get(currentLoc).getX(), loc.get(currentLoc).getY(), loc.get(currentLoc).getZ());
           this.navigation.a(pathEntity, speed);
       }
    }
    
    Zombie:
    custom zombie (open)

    Code:
    package CustomMobs;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R2.CraftWorld;
    import org.bukkit.craftbukkit.v1_8_R2.util.UnsafeList;
    
    import net.minecraft.server.v1_8_R2.Entity;
    import net.minecraft.server.v1_8_R2.EntityCreeper;
    import net.minecraft.server.v1_8_R2.EntityHuman;
    import net.minecraft.server.v1_8_R2.EntityIronGolem;
    import net.minecraft.server.v1_8_R2.EntitySkeleton;
    import net.minecraft.server.v1_8_R2.EntitySpider;
    import net.minecraft.server.v1_8_R2.EntityZombie;
    import net.minecraft.server.v1_8_R2.PathfinderGoalBreakDoor;
    import net.minecraft.server.v1_8_R2.PathfinderGoalFloat;
    import net.minecraft.server.v1_8_R2.PathfinderGoalHurtByTarget;
    import net.minecraft.server.v1_8_R2.PathfinderGoalLookAtPlayer;
    import net.minecraft.server.v1_8_R2.PathfinderGoalMeleeAttack;
    import net.minecraft.server.v1_8_R2.PathfinderGoalMoveThroughVillage;
    import net.minecraft.server.v1_8_R2.PathfinderGoalMoveTowardsRestriction;
    import net.minecraft.server.v1_8_R2.PathfinderGoalNearestAttackableTarget;
    import net.minecraft.server.v1_8_R2.PathfinderGoalRandomLookaround;
    import net.minecraft.server.v1_8_R2.PathfinderGoalRandomStroll;
    import net.minecraft.server.v1_8_R2.PathfinderGoalSelector;
    
    
    public class CustomZombie extends EntityZombie
    {
        public CustomZombie(org.bukkit.World world)
        {
            super(((CraftWorld)world).getHandle());
                /*List goalB = (List)MobUtils.getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
                List goalC = (List)MobUtils.getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
                List targetB = (List)MobUtils.getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
                List targetC = (List)MobUtils.getPrivateField("c", PathfinderGoalSelector.class, targetSelector); targetC.clear();*/
                ArrayList<Location> locations = new ArrayList<Location>();
                locations.add(new Location(Bukkit.getServer().getWorld("world"), 100,100,110));
                locations.add(new Location(Bukkit.getServer().getWorld("world"), 110,100,110));
                locations.add(new Location(Bukkit.getServer().getWorld("world"), 120,100,110));
                this.goalSelector.a(3, new PathfinderGoalWalkToLoc(this, locations, 1));//this.bw can be replaced by a custom speed
        }
       
        public CustomZombie()
        {
            super(((CraftWorld)Bukkit.getServer().getWorld("world")).getHandle());
        }
    }
    
    
     
  2. Offline

    stefvanschie

    @ski23 You probably have to do some thing with sin and cos to see which direction the block is from the zombie, then teleport it some block in that direction, but I have no idea how sin and cos work, so I can't help you with that, but there is a tutorial that might come in handy: https://bukkit.org/threads/tutorial-how-to-calculate-vectors.138849/ it's about vectors, but the cos and sin parts can be used for other things as well.
     
  3. Offline

    ShowbizLocket61

    @stefvanschie
    That would technically work, but it'll be really ugly. And choppy.
     
  4. Offline

    HenrySartori

    Don't use Minecraft logger! You didn't own that!
    Fortunately, I said this before Zombie_Striker. My first time.
     
  5. Offline

    ShowbizLocket61

    @HenrySartori @ski23
    Er... no one owns Bukkit either, and they can still use Bukkit methods ;P
    The reason why you shouldn't use Logger.getLogger() is because it's unofficial, therefore unsupported, and volatile. It's the vanilla Java way of getting CraftBukkit's global logger.

    Instead, use Bukkit.getServer().getLogger().info(message);
     
  6. Offline

    teej107

    He meant that the logger isn't meant to be used by plugins. Your suggested logger isn't yours either to use (I think that's MC's anyway). JavaPlugin has its very own logger that is meant for you to use. You log things under your plugin's logger and people know it's from your plugin.
     
  7. Offline

    ShowbizLocket61

    @teej107
    I'm not saying anything's mine to use, I'm just using it. What's this logger your talking about? I've only seen two, the Logger.getLogger and the getServer().getLogger
     
  8. Offline

    teej107

  9. Offline

    ski23

    Ok... So any suggestions for the question??
     
  10. Offline

    teej107

    Does anything happen?
     
  11. Offline

    ski23

    I have been trying it some more and now the mob will actually go to a location if I go in survival, let it hit me, and then go back in creative.
    I can see from debug messages that the c method only gets called once when this happens as well.
     
  12. Offline

    87pen

    @ski23 c only gets called after a returns true. Have you given the mob your custom path finding goal?
     
    Last edited: Jan 23, 2016
  13. Offline

    ski23

    @87pen I have. Also, that was my understanding however, many times, a returns true but c does not get called. Look:
    Code:
    [19:33:52 INFO]: inside pathfinder a
    [19:33:52 INFO]: true
    [19:33:52 INFO]: inside pathfinder a
    [19:33:52 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
    [19:33:53 INFO]: true
    [19:33:53 INFO]: inside pathfinder a
     
  14. Offline

    87pen

    @ski23 Hmmm, I'm not really sure. I can try some stuff when I get access to an ide. But that won't be for another 2 hours.
     
  15. Offline

    ski23

    @87pen Ok, Let me know what you figure out. In the meantime, I'll be trying to disassemble the existing pathfinders and see what makes them work compared to mine.
    Edit:
    So I have moved the line that changes the navigation into the a method and it works. Somehow, I feel that this is not a very good option long term though. Also, I seem to notice that the range that an entity can go to target my location is 35 blocks. I previously thought that it was only 16 or 20 blocks (heard different things different places).
     
    Last edited: Jan 23, 2016
  16. Offline

    ski23

    bump.
    Any advice on how to fix the original method?
     
  17. Offline

    ski23

Thread Status:
Not open for further replies.

Share This Page