1.8.8 Custom entites help

Discussion in 'Spigot Plugin Development' started by ProIEZRush, Mar 20, 2020.

  1. Hello, my name is ProIEZRush, I'm plugin developer.

    Actually I'm developing my first custom entity plugin, but I have 1 issue.

    Basically I want to make a custom entity that attacks other custom entities and walks to a custom location.

    I have already coded this:

    I have a class for all entities
    Code (Java):
    public class CreeperEnemie extends EntityCreeper {

        public CreeperEnemie(World world) {
            super (world);

            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 e) {
                e.printStackTrace();
            }

            this.goalSelector.a(0, new PathfinderGoalFloat(this));
            this.goalSelector.a(5, new PathfinderGoalMoveTowardsRestriction(this, 0.6D));
            this.goalSelector.a(9, new PathfinderGoalRandomStroll(this, 0.6D));
        }

        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 | IllegalAccessException e) {
                e.printStackTrace();
            }
            return o;
        }

        @Override
        public void move(double d0, double d1, double d2) {

        }

        @Override
        public void collide(Entity entity) {
        }

        @Override
        public boolean damageEntity(DamageSource damagesource, float f) {
            return false;
        }


        @Override
        public void g(double d0, double d1, double d2) {
        }

    }

    NOTE: I have two classes per entity type (1 enemie and other trop)
    This class is for trop spawning
    Code (Java):
    public static CreeperTrop spawnCreeperTrop(Location spawnLoc){
            World nmsWorld = ((CraftWorld)spawnLoc.getWorld()).getHandle();

            CreeperTrop cz = new CreeperTrop(nmsWorld);
            cz.setPosition(spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ());
            x(cz);
            nmsWorld.addEntity(cz);

            return cz;
        }

    private static void x(EntityCreature cz) {
            //Attack enemies
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, ZombieEnemie.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, SkeletonEnemie.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, CreeperEnemie.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, SpiderEnemie.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, EndermanEnemie.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, IronGolemEnemie.class, 0, true, false, null));
        }
    This class is for enemie spawning
    Code (Java):
    public static CreeperEnemie spawnCreeperEnemie(Location spawnLoc, Location walkTo){
            World nmsWorld = ((CraftWorld)spawnLoc.getWorld()).getHandle();

            CreeperEnemie cz = new CreeperEnemie(nmsWorld);
            cz.setPosition(spawnLoc.getX(), spawnLoc.getY(), spawnLoc.getZ());
            z(cz, walkTo);
            nmsWorld.addEntity(cz);

            return cz;
        }

    private static void z(EntityCreature cz, Location walkTo) {
            //Make entity walk to the castle
            cz.goalSelector.a(2, new PathfinderGoalWalkToLoc(cz, walkTo, 0.28F));
            //Attack trops
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, ZombieTrop.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, SkeletonTrop.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, CreeperTrop.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, SpiderTrop.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, EndermanTrop.class, 0, true, false, null));
            cz.targetSelector.a(3, new PathfinderGoalNearestAttackableTarget<>(cz, IronGolemTrop.class, 0, true, false, null));
        }

    Code (Java):
    public class PathfinderGoalWalkToLoc extends PathfinderGoal {

        float speed;
        Location location;
        private EntityCreature entitycreature;

        public PathfinderGoalWalkToLoc(EntityCreature entitycreature, Location location, float speed) {
            this.location = location;
            this.speed = speed;
            this.entitycreature = entitycreature;
        }


        @Override
        public boolean a() {

            if (this.entitycreature.aI >= 100) {
                return false;
            } else {
                return true;
            }
        }

        @Override
        public void c() {
            this.entitycreature.getNavigation().a(location.getX(), location.getY(), location.getZ(), speed);
        }
    }

    NOTE: I want to fix all PathfinderGoalNearestAttackableTarget (don't work), I want to know whats the first parameter on targetSelector.a, I want to fix the PathFinderWalkToLoc class, when mobs are spawned, some are not visible and some appear on other location (upper) I want to fix that.

    THE PLUGIN IS FOR 1.8

    Thanks!
     
  2. use Bukkit Javadocs please