Solved Spawning in Custom Wither

Discussion in 'Plugin Development' started by Rexe0, Apr 29, 2021 at 12:25 AM.

  1. Offline

    Rexe0

    Hello,

    I've been trying to make a custom boss fight with a custom wither entity.

    Basically, when you press a button and the conditions are right you are teleported to the boss room and the wither is meant to spawn. However, when the button is pressed, you are teleported and everything else works except the wither just isn't there when I test it.

    Help would be greatly appreciated.
    Thanks.

    Code:
    Code:
    if (e.getClickedBlock().getType() == Material.POLISHED_BLACKSTONE_BUTTON) {
                            if (e.getPlayer().getWorld().getName().equals("abyssaldepths")) {
                                Block block = e.getClickedBlock();
    
                                if (xeracRoomCounter >= 9) {
                                    Player player = e.getPlayer();
                                    player.teleport(new Location(Bukkit.getWorld("abyssaldepths"), -230, 77, 255, 90f, 0f));
                                    if (!xeracBossRoomStarted) {
                                        Main.scheduleSyncDelayedTask(new Runnable() {
                                            @Override
                                            public void run() {
                                          
                                                Location finalLoc = new Location(Bukkit.getWorld("abyssaldepths"), -321, 81, 255);
                                                Xerac voidsidian = new Xerac(finalLoc, ChatColor.DARK_RED + "☠" + ChatColor.RED + " Xerac");
    
    
                                                WorldServer world = ((CraftWorld) finalLoc.getWorld()).getHandle();
                                                world.addEntity(voidsidian);
                                                Wither wither = (Wither) voidsidian.getBukkitEntity();
    
                                                wither.addScoreboardTag("supremeHealthBar");
                                                wither.getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(10000);
                                                wither.getAttribute(Attribute.GENERIC_ARMOR).setBaseValue(10);
                                                wither.getAttribute(Attribute.GENERIC_KNOCKBACK_RESISTANCE).setBaseValue(100);
                                                wither.setHealth(10000);
                                                wither.setPersistent(true);
    
    
                                                xeracBossRoomStarted = true;
                                                BossBar bossbar = wither.getBossBar();
                                                bossbar.setTitle(ChatColor.DARK_RED + "☠" + ChatColor.RED + " Xerac");
                                                bossbar.setStyle(BarStyle.SOLID);
                                                bossbar.setColor(BarColor.RED);
                                                bossbar.setProgress(1);
                                                bossbar.setVisible(true);
    
                                            }
                                        }, 1);
                                    }
                                }
    Xerac Class: (I made this to make the wither not float above the player during the first 'stage' of a wither)
    Code:
    public class Xerac extends EntityWither {
        private final float[] bq = new float[2];
        private final float[] br = new float[2];
        private final float[] bs = new float[2];
        private final float[] bt = new float[2];
    
    
    
        public Xerac(Location loc, String name) {
            super(EntityTypes.WITHER, ((CraftWorld) loc.getWorld()).getHandle());
            this.setPosition(loc.getX(), loc.getY(), loc.getZ());
    
    
            this.setCustomName(new ChatComponentText(name));
            this.setCustomNameVisible(true);
    
    
        }
    
    
        @Override
        public boolean S_() {
            return false;
        }
    
        @Override
        public void movementTick() {
    
            if (true) {
                Player pl = Main.findTankLOS(this.getBukkitEntity());
                if (pl != null) {
                    this.setHeadTarget(0, pl.getEntityId());
                    this.setHeadTarget(1, pl.getEntityId());
                    this.setHeadTarget(2, pl.getEntityId());
                }
                Vec3D vec3d = this.getMot().d(1.0D, 0.6D, 1.0D);
                if (!this.world.isClientSide && this.getHeadTarget(0) > 0) {
                    Entity entity = this.world.getEntity(this.getHeadTarget(0));
                    if (entity != null) {
                        double d0 = vec3d.y;
                        if (this.locY() < entity.locY() || !this.S_() && this.locY() < entity.locY() + 0.0D) {
                            d0 = Math.max(0.0D, d0);
                            d0 += 0.3D - d0 * 0.6000000238418579D;
                        }
    
                        vec3d = new Vec3D(vec3d.x, d0, vec3d.z);
                        Vec3D vec3d1 = new Vec3D(entity.locX() - this.locX(), 0.0D, entity.locZ() - this.locZ());
                        if (c((Vec3D) vec3d1) > 9.0D) {
                            Vec3D vec3d2 = vec3d1.d();
                            vec3d = vec3d.add(vec3d2.x * 0.3D - vec3d.x * 0.6D, 0.0D, vec3d2.z * 0.3D - vec3d.z * 0.6D);
                        }
                    }
                }
    
                this.setMot(vec3d);
                if (c((Vec3D) vec3d) > 0.05D) {
                    this.yaw = (float) MathHelper.d(vec3d.z, vec3d.x) * 57.295776F - 90.0F;
                }
            }
    
            if (true) {
                this.dA();
                this.eQ();
    
                if (this.cs()) {
                    this.aU = 0;
                    this.c(this.locX(), this.locY(), this.locZ());
                }
    
                if (this.aU > 0) {
                    double d0 = this.locX() + (this.aV - this.locX()) / (double)this.aU;
                    double d1 = this.locY() + (this.aW - this.locY()) / (double)this.aU;
                    double d2 = this.locZ() + (this.aX - this.locZ()) / (double)this.aU;
                    double d3 = MathHelper.g(this.aY - (double)this.yaw);
                    this.yaw = (float)((double)this.yaw + d3 / (double)this.aU);
                    this.pitch = (float)((double)this.pitch + (this.aZ - (double)this.pitch) / (double)this.aU);
                    --this.aU;
                    this.setPosition(d0, d1, d2);
                    this.setYawPitch(this.yaw, this.pitch);
                } else if (!this.doAITick()) {
                    this.setMot(this.getMot().a(0.98D));
                }
    
                if (this.bb > 0) {
                    this.aC = (float)((double)this.aC + MathHelper.g(this.ba - (double)this.aC) / (double)this.bb);
                    --this.bb;
                }
    
                Vec3D vec3d = this.getMot();
                double d4 = vec3d.x;
                double d5 = vec3d.y;
                double d6 = vec3d.z;
                if (Math.abs(vec3d.x) < 0.003D) {
                    d4 = 0.0D;
                }
    
                if (Math.abs(vec3d.y) < 0.003D) {
                    d5 = 0.0D;
                }
    
                if (Math.abs(vec3d.z) < 0.003D) {
                    d6 = 0.0D;
                }
    
                this.setMot(d4, d5, d6);
                this.world.getMethodProfiler().enter("ai");
                SpigotTimings.timerEntityAI.startTiming();
                if (this.isFrozen()) {
                    this.jumping = false;
                    this.aR = 0.0F;
                    this.aT = 0.0F;
                } else if (this.doAITick()) {
                    this.world.getMethodProfiler().enter("newAi");
                    this.doTick();
                    this.world.getMethodProfiler().exit();
                }
    
                SpigotTimings.timerEntityAI.stopTiming();
    
    
                this.world.getMethodProfiler().exit();
                this.world.getMethodProfiler().enter("travel");
                this.aR *= 0.98F;
                this.aT *= 0.98F;
                this.r();
                AxisAlignedBB axisalignedbb = this.getBoundingBox();
                SpigotTimings.timerEntityAIMove.startTiming();
                this.g(new Vec3D((double)this.aR, (double)this.aS, (double)this.aT));
                SpigotTimings.timerEntityAIMove.stopTiming();
                this.world.getMethodProfiler().exit();
                this.world.getMethodProfiler().enter("push");
                if (this.bf > 0) {
                    --this.bf;
                    this.a(axisalignedbb, this.getBoundingBox());
                }
    
                SpigotTimings.timerEntityAICollision.startTiming();
                this.collideNearby();
                SpigotTimings.timerEntityAICollision.stopTiming();
                this.world.getMethodProfiler().exit();
                if (!this.world.isClientSide && this.dO() && this.aG()) {
                    this.damageEntity(DamageSource.DROWN, 1.0F);
                }
    
    
                this.world.getMethodProfiler().enter("looting");
                if (!this.world.isClientSide && this.canPickupLoot() && this.isAlive() && !this.killed && this.world.getGameRules().getBoolean(GameRules.MOB_GRIEFING)) {
                    List<EntityItem> list = this.world.a(EntityItem.class, this.getBoundingBox().grow(1.0D, 0.0D, 1.0D));
                    Iterator iterator = list.iterator();
    
                    while(iterator.hasNext()) {
                        EntityItem entityitem = (EntityItem)iterator.next();
                        if (!entityitem.dead && !entityitem.getItemStack().isEmpty() && !entityitem.p() && this.i(entityitem.getItemStack())) {
                            this.b(entityitem);
                        }
                    }
                }
    
                this.world.getMethodProfiler().exit();
            }
    
            int i;
            for(i = 0; i < 2; ++i) {
                this.bt[i] = this.br[i];
                this.bs[i] = this.bq[i];
            }
    
            double d9;
            double d10;
            int j;
            double d8;
            for(i = 0; i < 2; ++i) {
                j = this.getHeadTarget(i + 1);
                Entity entity1 = null;
                if (j > 0) {
                    entity1 = this.world.getEntity(j);
                }
    
                if (entity1 != null) {
                    d8 = this.u(i + 1);
                    d9 = this.v(i + 1);
                    d10 = this.w(i + 1);
                    double d4 = entity1.locX() - d8;
                    double d5 = entity1.getHeadY() - d9;
                    double d6 = entity1.locZ() - d10;
                    double d7 = (double)MathHelper.sqrt(d4 * d4 + d6 * d6);
                    float f = (float)(MathHelper.d(d6, d4) * 57.2957763671875D) - 90.0F;
                    float f1 = (float)(-(MathHelper.d(d5, d7) * 57.2957763671875D));
                    this.bq[i] = this.a(this.bq[i], f1, 40.0F);
                    this.br[i] = this.a(this.br[i], f, 10.0F);
                } else {
                    this.br[i] = this.a(this.br[i], this.aA, 10.0F);
                }
            }
    
            boolean flag = this.S_();
    
            for(j = 0; j < 3; ++j) {
                d8 = this.u(j);
                d9 = this.v(j);
                d10 = this.w(j);
                this.world.addParticle(Particles.SMOKE, d8 + this.random.nextGaussian() * 0.30000001192092896D, d9 + this.random.nextGaussian() * 0.30000001192092896D, d10 + this.random.nextGaussian() * 0.30000001192092896D, 0.0D, 0.0D, 0.0D);
                if (flag && this.world.random.nextInt(4) == 0) {
                    this.world.addParticle(Particles.ENTITY_EFFECT, d8 + this.random.nextGaussian() * 0.30000001192092896D, d9 + this.random.nextGaussian() * 0.30000001192092896D, d10 + this.random.nextGaussian() * 0.30000001192092896D, 0.699999988079071D, 0.699999988079071D, 0.5D);
                }
            }
    
            if (this.getInvul() > 0) {
                for(j = 0; j < 3; ++j) {
                    this.world.addParticle(Particles.ENTITY_EFFECT, this.locX() + this.random.nextGaussian(), this.locY() + (double)(this.random.nextFloat() * 3.3F), this.locZ() + this.random.nextGaussian(), 0.699999988079071D, 0.699999988079071D, 0.8999999761581421D);
                }
            }
    
        }
    
    
        private float a(float f, float f1, float f2) {
            float f3 = MathHelper.g(f1 - f);
            if (f3 > f2) {
                f3 = f2;
            }
    
            if (f3 < -f2) {
                f3 = -f2;
            }
    
            return f + f3;
        }
    
        private double u(int i) {
            if (i <= 0) {
                return this.locX();
            } else {
                float f = (this.aA + (float)(180 * (i - 1))) * 0.017453292F;
                float f1 = MathHelper.cos(f);
                return this.locX() + (double)f1 * 1.3D;
            }
        }
    
        private double v(int i) {
            return i <= 0 ? this.locY() + 3.0D : this.locY() + 2.2D;
        }
    
        private double w(int i) {
            if (i <= 0) {
                return this.locZ();
            } else {
                float f = (this.aA + (float)(180 * (i - 1))) * 0.017453292F;
                float f1 = MathHelper.sin(f);
                return this.locZ() + (double)f1 * 1.3D;
            }
        }
    }
    
     
  2. Offline

    Kars

    Have you tried World#spawnEntity?

    It would help if you gave an indication of where in your code things go wrong.
     
  3. Offline

    Rexe0

    1. There was a tutorial I followed some time ago that gave me this code, I tried it before and it worked completely fine. (It said to use CraftWorld#addEntity, but I'll try using that)
    2. I'm not getting any errors in console. I've tried testing the variables, etc.

    I'm confused where something has gone wrong, as the wither itself isn't spawning but I don't see anything wrong.

    I'll go try out the World#spawnEntity and report back later.

    EDIT:
    World#spawnEntity accepts an EntityType which means I would not be able to spawn the wither with the custom moving AI thing. (or have to use some workaround that I don't know)
     
    Last edited: Apr 29, 2021 at 2:30 PM
  4. Offline

    davidclue

    Just do
    Code:
    Wither myWither = (Wither) world.spawnEntity(location, EntityType.WITHER);
    And then you can use your instance to do what you need.
     
  5. Offline

    Rexe0

    I think your confused. I can't spawn it using that way, otherwise I won't be able modify the hover movement from a regular wither. (If you ever spawn a wither and it is targeting something while it isn't below half health, it floats above the target). If there is a way I can still modify that mechanic while spawning it the regular Bukkit way then please let me know.
     
  6. Offline

    davidclue

    Code:
    World world = ((CraftWorld) p.getWorld()).getHandle();
    entity = new EntityWither(EntityTypes.WITHER, world);
    entity.setPosition(location.getX(), location.getY(), location.getZ());
    ((CraftLivingEntity) entity.getBukkitEntity()).setRemoveWhenFarAway(false);
    world.addEntity(entity);
    Try this.
     
  7. Offline

    Rexe0

    Nevermind, I just hooked up the spawn on a long delay and it started working. Thanks everyone for trying to help.
     

Share This Page