1.8.8 NMS Custom Ender Dragon

Discussion in 'Spigot Plugin Development' started by Delected_, Oct 9, 2020.

  1. You gave us the code with 0 context on how to use it. Technically that's spoonfeeding.
    But since I already know how to use it, and as its not working, we here are asking for help to resolve the issue.

    You do not just give us a code and leave us there to figure it out.
    This is a help forum for christs sake.
     
  2. This is a help forum exactly, not a java school. I told you that setting the target to an entity would work. Did you try that???
    I gave no code btw.
     
  3. I did in fact try it, and as I said, it doesn't work at all. All I am asking is for a quick explanation on how that function works because giving a 1 line code doesn't help because I could misplace it somewhere.

    You saying you spoonfed us does include you in this.
     
  4. You can legit set the target anywhere in your code once the ender dragon is spawned
     
  5. ((CraftEnderDragon) dragon).getHandle().getNavigation().a(entity);
    ((CraftEnderDragon) dragon).getHandle().getNavigation().a(blockPosition);
    ((CraftEnderDragon) dragon).getHandle().getNavigation().a(double, double, double);
     
  6. Using the 3rd method, and it doesn't work at all. So I doubt the other 2 won't work.

    Code (Java):

    public static void randomizeMovement(CustomEnderDragon enderDragon) {

        LivingEntity eDragon = (LivingEntity) enderDragon.getBukkitEntity();
        Location loc = eDragon.getLocation();

        Location loc1 = loc.add(-20, 0, -20);
        Location loc2 = loc.add(20, 0, 20);

        double minX = Math.min(loc1.getX(), loc2.getX());
        double minY = Math.min(5, 40);
        double minZ = Math.min(loc1.getZ(), loc2.getZ());

        double maxX = Math.max(loc1.getX(), loc2.getX());
        double maxY = Math.max(5, 40);
        double maxZ = Math.max(loc1.getZ(), loc2.getZ());

        Location target = new Location(eDragon.getWorld(), randomDouble(minX, maxX), randomDouble(minY, maxY), randomDouble(minZ, maxZ));

        ((CraftEnderDragon) enderDragon.getBukkitEntity()).getHandle().getNavigation().a(target.getX(), target.getY(), target.getZ());
        Utils.broadCast("[DEBUG] " + target.getX() + ", " + target.getY() + ", " + target.getZ());
    }
    the function above is in a bukkit runnable
     
  7. Why .getBukkitEntity()?
    How do you register your bukkit runnable?
     
  8. enderDragon is casted to CustomEnderDragon which is different than CraftEnderDragon
    Wont allow me to cast it
    upload_2020-10-19_23-32-20.png

    Runnable is just simple code.
    Code (Java):

    task = new BukkitRunnable() {
        @Override
        public void run() {
            CustomEnderDragon.randomizeMovement(eDragon);
        }
    }.runTaskTimer(plugin, 0L, 200L);
    BUT I think I need to clear the dragon's pathfindergoal before doing my own.
    I found some tutorials but I had to tweak it since there are missing variables.
    Not sure if its correct but will you review it?
    Code (Java):

    package xyz.apollo30.skyblockremastered.customMobs;

    import net.minecraft.server.v1_8_R3.EntityEnderDragon;
    import net.minecraft.server.v1_8_R3.PathfinderGoalSelector;
    import net.minecraft.server.v1_8_R3.World;
    import org.bukkit.Location;
    import org.bukkit.craftbukkit.v1_8_R3.CraftWorld;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftEnderDragon;
    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftLivingEntity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import xyz.apollo30.skyblockremastered.utils.Utils;

    import java.lang.reflect.Field;
    import java.util.List;
    import java.util.concurrent.ThreadLocalRandom;

    public class CustomEnderDragon extends EntityEnderDragon {

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

            try {
                List goalB = (List)getPrivateField("b", goalSelector); goalB.clear();
                List goalC = (List)getPrivateField("c",  goalSelector); goalC.clear();
                List targetB = (List)getPrivateField("b", targetSelector); targetB.clear();
                List targetC = (List)getPrivateField("c", targetSelector); targetC.clear();
            } catch (Exception ignored) {

            }

        }

        public static CustomEnderDragon spawn(Location loc, String name) {
            World mcWorld = ((CraftWorld) loc.getWorld()).getHandle();
            final CustomEnderDragon enderDragon = new CustomEnderDragon(mcWorld);

            enderDragon.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
            ((CraftLivingEntity) enderDragon.getBukkitEntity()).setRemoveWhenFarAway(true);
            mcWorld.addEntity(enderDragon, CreatureSpawnEvent.SpawnReason.CUSTOM);
            enderDragon.setCustomName(Utils.chat(name));
            enderDragon.setCustomNameVisible(false);
            return enderDragon;
        }

        private static Object getPrivateField(String fieldName, Object object) {
            Field field;
            Object o = null;

            try {
                field = PathfinderGoalSelector.class.getDeclaredField(fieldName);

                field.setAccessible(true);

                o = field.get(object);
            } catch (NoSuchFieldException | IllegalAccessException e) {
                e.printStackTrace();
            }

            return o;
        }

        public static void randomizeMovement(CustomEnderDragon enderDragon) {

            LivingEntity eDragon = (LivingEntity) enderDragon.getBukkitEntity();
            Location loc = eDragon.getLocation();

            Location loc1 = loc.add(-20, 0, -20);
            Location loc2 = loc.add(20, 0, 20);

            double minX = Math.min(loc1.getX(), loc2.getX());
            double minY = Math.min(5, 40);
            double minZ = Math.min(loc1.getZ(), loc2.getZ());

            double maxX = Math.max(loc1.getX(), loc2.getX());
            double maxY = Math.max(5, 40);
            double maxZ = Math.max(loc1.getZ(), loc2.getZ());

            Location target = new Location(eDragon.getWorld(), randomDouble(minX, maxX), randomDouble(minY, maxY), randomDouble(minZ, maxZ));

            enderDragon.getNavigation().a(target.getX(), target.getY(), target.getZ());
            ((CraftEnderDragon) enderDragon.getBukkitEntity()).getHandle().getNavigation().a(target.getX(), target.getY(), target.getZ());
            Utils.broadCast("[DEBUG] " + target.getX() + ", " + target.getY() + ", " + target.getZ());
        }

        private static double randomDouble(double min, double max) {
            return min + ThreadLocalRandom.current().nextDouble(Math.abs(max - min + 1));
        }

    }
     

    Attached Files:

    #28 Apollyon365, Oct 20, 2020 at 7:33 AM
    Last edited: Oct 20, 2020 at 7:50 AM
  9. double bump in 4 hours... noice
     
  10. Technically bumped the thread for us lol

    Anyways it's been like 16 hours so *bump*

    Also funny you live in Laval, I was just visiting there like 2 days ago. I went to Brossard and Laval... anyways off-topic
     
  11. upload_2020-10-22_17-35-49.png

    something is wrong with this thread :D
     
    • Funny Funny x 1
  12. :sweatBlob:
     
    • Funny Funny x 1
  13. Well, I found a solution in a different thread, not using pathfindergoals but a different function.

    Once it all works out I will send it here for others to look at if they are truely stuck with enderdragon pathfinders.
    Then we can finally resolve this thread.
     
  14. Wow ahah where are you from?
    What function? The set target one?
     
  15. Toronto

    Anyways I'm gonna close this thread once @Apollyon365 posts his answer.
     
  16. Code (Java):

    setPositionRotation(x, y, z, yaw, pitch);
     
    Should be inside your entity.
    Make sure you have a bukkitrunnable that always moves the dragon, otherwise its just going to override.