Solved Pathfinding (no documentation anywhere)

Discussion in 'Plugin Development' started by Rixterz, Oct 16, 2016.

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

    Rixterz

    Hi

    There's no sign of documentation anywhere on earth for up-to-date minecraft plugin development.

    I want to make my CustomSkeleton target ArmorStands and do nothing else whatsoever. It should spawn, go to an armor stand, kill it, then go idle until another is placed.

    My code, giving a NullPointerException at PathfinderGoalFloat.<init>:
    Code:
    public class CustomSkeleton extends EntitySkeleton
    {
        public CustomSkeleton(World world)
        {
            super(world);
        }
       
        protected void initAttributes()
        {
            super.initAttributes();
            this.fireProof = true;
            this.setCustomName(this.at());
            this.setCustomNameVisible(true);
           
            this.getAttributeInstance(GenericAttributes.FOLLOW_RANGE).setValue(35.0D);
          
            this.goalSelector.a(1, new PathfinderGoalFloat(this));
            this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget<EntityArmorStand>(this, EntityArmorStand.class, true));
        }
       
        public Skeleton spawn(Location loc)
        {
            World mcWorld = (World) ((CraftWorld) loc.getWorld()).getHandle();
            final CustomSkeleton c = new CustomSkeleton(mcWorld);
           
            c.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            ((CraftLivingEntity) c.getBukkitEntity()).setRemoveWhenFarAway(false);
            mcWorld.addEntity(c, SpawnReason.CUSTOM);
           
            Skeleton sk = (Skeleton) c.getBukkitEntity();
           
            sk.getEquipment().clear();
            sk.getEquipment().setItemInMainHand(Utils.s(Material.BOW));
           
            return sk;
        }
    }
    
     
  2. Offline

    Zombie_Striker

    Bukkit has not changed since I joined back in 2013. All tutorials and docs should still work.

    We need the full error log. What line is throwing this error?
     
  3. Offline

    Rixterz

    Pathfinding has changed a lot

    I tried moving the code to the spawn() function instead, because "this" was being used in initAttributes() which may have caused the NullPointerException as "this" wouldn't yet exist at that point.

    Now there are no errors but the entity's behaviour is completely normal.

    Code:
    try
    {
        LinkedHashSet<?> goalB = (LinkedHashSet<?>) getPrivateField("b", PathfinderGoalSelector.class, goalSelector); goalB.clear();
        LinkedHashSet<?> goalC = (LinkedHashSet<?>) getPrivateField("c", PathfinderGoalSelector.class, goalSelector); goalC.clear();
        LinkedHashSet<?> targetB = (LinkedHashSet<?>) getPrivateField("b", PathfinderGoalSelector.class, targetSelector); targetB.clear();
        LinkedHashSet<?> targetC = (LinkedHashSet<?>) getPrivateField("c", PathfinderGoalSelector.class, targetSelector);       targetC.clear();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
          
    this.goalSelector.a(0, new PathfinderGoalFloat(this));
    this.targetSelector.a(2, new PathfinderGoalNearestAttackableTarget<EntitySheep>(this, EntitySheep.class, false));
    this.targetSelector.a(1, new PathfinderGoalHurtByTarget(this, true));
    
    public static Object getPrivateField(String fieldName, Class<PathfinderGoalSelector> clazz, Object object)
    {
        try
        {
            Field field = clazz.getDeclaredField(fieldName);
            field.setAccessible(true);
     
            return field.get(object);
         }
         catch(Exception e)
         {
             e.printStackTrace();
         }
         
         return null;
    }
    
    EDIT: oh wow. Turns out the only place that the code works is right after the super() statement. I have no idea why, but it works.
     
    Last edited: Oct 17, 2016
Thread Status:
Not open for further replies.

Share This Page