--- a/net/minecraft/world/entity/boss/enderdragon/EntityEnderDragon.java +++ b/net/minecraft/world/entity/boss/enderdragon/EntityEnderDragon.java @@ -53,6 +53,19 @@ import org.joml.Vector3f; import org.slf4j.Logger; +// CraftBukkit start +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Explosion; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.entity.TileEntity; +import net.minecraft.world.level.storage.loot.LootParams; +import net.minecraft.world.level.storage.loot.LootTableInfo; +import net.minecraft.world.level.storage.loot.parameters.LootContextParameters; +import org.bukkit.craftbukkit.block.CraftBlock; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.EntityRegainHealthEvent; +// CraftBukkit end + public class EntityEnderDragon extends EntityInsentient implements IMonster { private static final Logger LOGGER = LogUtils.getLogger(); @@ -90,6 +103,7 @@ private final PathPoint[] nodes; private final int[] nodeAdjacency; private final Path openSet; + private final Explosion explosionSource; // CraftBukkit - reusable source for CraftTNTPrimed.getSource() public EntityEnderDragon(EntityTypes entitytypes, World world) { super(EntityTypes.ENDER_DRAGON, world); @@ -111,6 +125,7 @@ this.noPhysics = true; this.noCulling = true; this.phaseManager = new DragonControllerManager(this); + this.explosionSource = new Explosion(world, this, null, null, Double.NaN, Double.NaN, Double.NaN, Float.NaN, true, Explosion.Effect.DESTROY); // CraftBukkit } public void setDragonFight(EnderDragonBattle enderdragonbattle) { @@ -258,7 +273,7 @@ Vec3D vec3d1 = idragoncontroller.getFlyTargetLocation(); - if (vec3d1 != null) { + if (vec3d1 != null && idragoncontroller.getPhase() != DragonControllerPhase.HOVERING) { // CraftBukkit - Don't move when hovering double d0 = vec3d1.x - this.getX(); double d1 = vec3d1.y - this.getY(); double d2 = vec3d1.z - this.getZ(); @@ -399,7 +414,14 @@ if (this.nearestCrystal.isRemoved()) { this.nearestCrystal = null; } else if (this.tickCount % 10 == 0 && this.getHealth() < this.getMaxHealth()) { - this.setHealth(this.getHealth() + 1.0F); + // CraftBukkit start + EntityRegainHealthEvent event = new EntityRegainHealthEvent(this.getBukkitEntity(), 1.0F, EntityRegainHealthEvent.RegainReason.ENDER_CRYSTAL); + this.level().getCraftServer().getPluginManager().callEvent(event); + + if (!event.isCancelled()) { + this.setHealth((float) (this.getHealth() + event.getAmount())); + } + // CraftBukkit end } } @@ -474,6 +496,9 @@ int j1 = MathHelper.floor(axisalignedbb.maxZ); boolean flag = false; boolean flag1 = false; + // CraftBukkit start - Create a list to hold all the destroyed blocks + List destroyedBlocks = new java.util.ArrayList(); + // CraftBukkit end for (int k1 = i; k1 <= l; ++k1) { for (int l1 = j; l1 <= i1; ++l1) { @@ -483,7 +508,11 @@ if (!iblockdata.isAir() && !iblockdata.is(TagsBlock.DRAGON_TRANSPARENT)) { if (this.level().getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING) && !iblockdata.is(TagsBlock.DRAGON_IMMUNE)) { - flag1 = this.level().removeBlock(blockposition, false) || flag1; + // CraftBukkit start - Add blocks to list rather than destroying them + // flag1 = this.level().removeBlock(blockposition, false) || flag1; + flag1 = true; + destroyedBlocks.add(CraftBlock.at(this.level(), blockposition)); + // CraftBukkit end } else { flag = true; } @@ -492,6 +521,51 @@ } } + // CraftBukkit start - Set off an EntityExplodeEvent for the dragon exploding all these blocks + // SPIGOT-4882: don't fire event if nothing hit + if (!flag1) { + return flag; + } + + org.bukkit.entity.Entity bukkitEntity = this.getBukkitEntity(); + EntityExplodeEvent event = new EntityExplodeEvent(bukkitEntity, bukkitEntity.getLocation(), destroyedBlocks, 0F); + bukkitEntity.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + // This flag literally means 'Dragon hit something hard' (Obsidian, White Stone or Bedrock) and will cause the dragon to slow down. + // We should consider adding an event extension for it, or perhaps returning true if the event is cancelled. + return flag; + } else if (event.getYield() == 0F) { + // Yield zero ==> no drops + for (org.bukkit.block.Block block : event.blockList()) { + this.level().removeBlock(new BlockPosition(block.getX(), block.getY(), block.getZ()), false); + } + } else { + for (org.bukkit.block.Block block : event.blockList()) { + org.bukkit.Material blockId = block.getType(); + if (blockId.isAir()) { + continue; + } + + CraftBlock craftBlock = ((CraftBlock) block); + BlockPosition blockposition = craftBlock.getPosition(); + + Block nmsBlock = craftBlock.getNMS().getBlock(); + if (nmsBlock.dropFromExplosion(explosionSource)) { + TileEntity tileentity = craftBlock.getNMS().hasBlockEntity() ? this.level().getBlockEntity(blockposition) : null; + LootParams.a loottableinfo_builder = (new LootParams.a((WorldServer) this.level())).withParameter(LootContextParameters.ORIGIN, Vec3D.atCenterOf(blockposition)).withParameter(LootContextParameters.TOOL, ItemStack.EMPTY).withParameter(LootContextParameters.EXPLOSION_RADIUS, 1.0F / event.getYield()).withOptionalParameter(LootContextParameters.BLOCK_ENTITY, tileentity); + + craftBlock.getNMS().getDrops(loottableinfo_builder).forEach((itemstack) -> { + Block.popResource(this.level(), blockposition, itemstack); + }); + craftBlock.getNMS().spawnAfterBreak((WorldServer) this.level(), blockposition, ItemStack.EMPTY, false); + } + nmsBlock.wasExploded(this.level(), blockposition, explosionSource); + + this.level().removeBlock(blockposition, false); + } + } + // CraftBukkit end + if (flag1) { BlockPosition blockposition1 = new BlockPosition(i + this.random.nextInt(l - i + 1), j + this.random.nextInt(i1 - j + 1), k + this.random.nextInt(j1 - k + 1)); @@ -556,6 +630,21 @@ } + // CraftBukkit start - SPIGOT-2420: Special case, the ender dragon drops 12000 xp for the first kill and 500 xp for every other kill and this over time. + @Override + public int getExpReward() { + // CraftBukkit - Moved from #tickDeath method + boolean flag = this.level().getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT); + short short0 = 500; + + if (this.dragonFight != null && !this.dragonFight.hasPreviouslyKilledDragon()) { + short0 = 12000; + } + + return flag ? short0 : 0; + } + // CraftBukkit end + @Override protected void tickDeath() { if (this.dragonFight != null) { @@ -571,15 +660,20 @@ this.level().addParticle(Particles.EXPLOSION_EMITTER, this.getX() + (double) f, this.getY() + 2.0D + (double) f1, this.getZ() + (double) f2, 0.0D, 0.0D, 0.0D); } + // CraftBukkit start - SPIGOT-2420: Moved up to #getExpReward method + /* boolean flag = this.level().getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT); short short0 = 500; if (this.dragonFight != null && !this.dragonFight.hasPreviouslyKilledDragon()) { short0 = 12000; } + */ + int short0 = expToDrop; + // CraftBukkit end if (this.level() instanceof WorldServer) { - if (this.dragonDeathTime > 150 && this.dragonDeathTime % 5 == 0 && flag) { + if (this.dragonDeathTime > 150 && this.dragonDeathTime % 5 == 0 && true) { // CraftBukkit - SPIGOT-2420: Already checked for the game rule when calculating the xp EntityExperienceOrb.award((WorldServer) this.level(), this.position(), MathHelper.floor((float) short0 * 0.08F)); } @@ -590,7 +684,7 @@ this.move(EnumMoveType.SELF, new Vec3D(0.0D, 0.10000000149011612D, 0.0D)); if (this.dragonDeathTime == 200 && this.level() instanceof WorldServer) { - if (flag) { + if (true) { // CraftBukkit - SPIGOT-2420: Already checked for the game rule when calculating the xp EntityExperienceOrb.award((WorldServer) this.level(), this.position(), MathHelper.floor((float) short0 * 0.2F)); } @@ -811,6 +905,7 @@ super.addAdditionalSaveData(nbttagcompound); nbttagcompound.putInt("DragonPhase", this.phaseManager.getCurrentPhase().getPhase().getId()); nbttagcompound.putInt("DragonDeathTime", this.dragonDeathTime); + nbttagcompound.putInt("Bukkit.expToDrop", expToDrop); // CraftBukkit - SPIGOT-2420: The ender dragon drops xp over time which can also happen between server starts } @Override @@ -824,6 +919,11 @@ this.dragonDeathTime = nbttagcompound.getInt("DragonDeathTime"); } + // CraftBukkit start - SPIGOT-2420: The ender dragon drops xp over time which can also happen between server starts + if (nbttagcompound.contains("Bukkit.expToDrop")) { + this.expToDrop = nbttagcompound.getInt("Bukkit.expToDrop"); + } + // CraftBukkit end } @Override