--- a/net/minecraft/world/level/ServerExplosion.java +++ b/net/minecraft/world/level/ServerExplosion.java @@ -33,6 +_,17 @@ import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.Vec3; +// CraftBukkit start +import net.minecraft.world.entity.boss.EnderDragonPart; +import net.minecraft.world.entity.boss.enderdragon.EnderDragon; +import net.minecraft.world.level.block.Blocks; +import org.bukkit.craftbukkit.event.CraftEventFactory; +import org.bukkit.craftbukkit.util.CraftLocation; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.Location; +import org.bukkit.event.block.BlockExplodeEvent; +// CraftBukkit end + public class ServerExplosion implements Explosion { private static final ExplosionDamageCalculator EXPLOSION_DAMAGE_CALCULATOR = new ExplosionDamageCalculator(); private static final int MAX_DROPS_PER_COMBINED_STACK = 16; @@ -47,6 +_,11 @@ private final DamageSource damageSource; private final ExplosionDamageCalculator damageCalculator; private final Map hitPlayers = new HashMap<>(); + // CraftBukkit - add field + public boolean wasCanceled = false; + public float yield; + // CraftBukkit end + public boolean excludeSourceFromDamage = true; // Paper - Allow explosions to damage source public ServerExplosion( ServerLevel level, @@ -60,12 +_,13 @@ ) { this.level = level; this.source = source; - this.radius = radius; + this.radius = (float) Math.max(radius, 0.0); // CraftBukkit - clamp bad values this.center = center; this.fire = fire; this.blockInteraction = blockInteraction; this.damageSource = damageSource == null ? level.damageSources().explosion(this) : damageSource; this.damageCalculator = damageCalculator == null ? this.makeDamageCalculator(source) : damageCalculator; + this.yield = this.blockInteraction == Explosion.BlockInteraction.DESTROY_WITH_DECAY ? 1.0F / this.radius : 1.0F; // CraftBukkit } private ExplosionDamageCalculator makeDamageCalculator(@Nullable Entity entity) { @@ -139,7 +_,8 @@ for (float f1 = 0.3F; f > 0.0F; f -= 0.22500001F) { BlockPos blockPos = BlockPos.containing(d3, d4, d5); BlockState blockState = this.level.getBlockState(blockPos); - FluidState fluidState = this.level.getFluidState(blockPos); + if (!blockState.isDestroyable()) continue; // Paper - Protect Bedrock and End Portal/Frames from being destroyed + FluidState fluidState = blockState.getFluidState(); // Paper - Perf: Optimize call to getFluid for explosions if (!this.level.isInWorldBounds(blockPos)) { break; } @@ -152,6 +_,15 @@ if (f > 0.0F && this.damageCalculator.shouldBlockExplode(this, this.level, blockPos, blockState, f)) { set.add(blockPos); + // Paper start - prevent headless pistons from forming + if (!io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.allowHeadlessPistons && blockState.is(Blocks.MOVING_PISTON)) { + net.minecraft.world.level.block.entity.BlockEntity extension = this.level.getBlockEntity(blockPos); + if (extension instanceof net.minecraft.world.level.block.piston.PistonMovingBlockEntity blockEntity && blockEntity.isSourcePiston()) { + net.minecraft.core.Direction direction = blockState.getValue(net.minecraft.world.level.block.piston.PistonHeadBlock.FACING); + set.add(blockPos.relative(direction.getOpposite())); + } + } + // Paper end - prevent headless pistons from forming } d3 += d * 0.3F; @@ -174,8 +_,8 @@ int floor3 = Mth.floor(this.center.y + f + 1.0); int floor4 = Mth.floor(this.center.z - f - 1.0); int floor5 = Mth.floor(this.center.z + f + 1.0); - - for (Entity entity : this.level.getEntities(this.source, new AABB(floor, floor2, floor4, floor1, floor3, floor5))) { + List list = this.level.getEntities(excludeSourceFromDamage ? this.source : null, new AABB(floor, floor2, floor4, floor1, floor3, floor5), entity -> entity.isAlive() && !entity.isSpectator()); // Paper - Fix lag from explosions processing dead entities, Allow explosions to damage source + for (Entity entity : list) { // Paper - used in loop if (!entity.ignoreExplosion(this)) { double d = Math.sqrt(entity.distanceToSqr(this.center)) / f; if (d <= 1.0) { @@ -189,15 +_,43 @@ d3 /= squareRoot; boolean shouldDamageEntity = this.damageCalculator.shouldDamageEntity(this, entity); float knockbackMultiplier = this.damageCalculator.getKnockbackMultiplier(entity); - float f1 = !shouldDamageEntity && knockbackMultiplier == 0.0F ? 0.0F : getSeenPercent(this.center, entity); + float f1 = !shouldDamageEntity && knockbackMultiplier == 0.0F ? 0.0F : this.getBlockDensity(this.center, entity); // Paper - Optimize explosions if (shouldDamageEntity) { - entity.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity, f1)); + // CraftBukkit start + + // Special case ender dragon only give knockback if no damage is cancelled + // Thinks to note: + // - Setting a velocity to a EnderDragonPart is ignored (and therefore not needed) + // - Damaging EnderDragonPart while forward the damage to EnderDragon + // - Damaging EntityEnderDragon does nothing + // - EnderDragon hitbock always covers the other parts and is therefore always present + if (entity instanceof EnderDragonPart) { + continue; + } + + entity.lastDamageCancelled = false; + + if (entity instanceof EnderDragon) { + for (EnderDragonPart dragonPart : ((EnderDragon) entity).getSubEntities()) { + // Calculate damage separately for each EntityComplexPart + if (list.contains(dragonPart)) { + dragonPart.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity, f1)); + } + } + } else { + entity.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity, f1)); + } + + if (entity.lastDamageCancelled) { // SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Skip entity if damage event was cancelled + continue; + } + // CraftBukkit end } double d4 = (1.0 - d) * f1 * knockbackMultiplier; double d5; if (entity instanceof LivingEntity livingEntity) { - d5 = d4 * (1.0 - livingEntity.getAttributeValue(Attributes.EXPLOSION_KNOCKBACK_RESISTANCE)); + d5 = entity instanceof Player && this.level.paperConfig().environment.disableExplosionKnockback ? 0 : d4 * (1.0 - livingEntity.getAttributeValue(Attributes.EXPLOSION_KNOCKBACK_RESISTANCE)); // Paper } else { d5 = d4; } @@ -206,10 +_,18 @@ d2 *= d5; d3 *= d5; Vec3 vec3 = new Vec3(d1, d2, d3); + // CraftBukkit start - Call EntityKnockbackEvent + if (entity instanceof LivingEntity) { + // Paper start - knockback events + io.papermc.paper.event.entity.EntityKnockbackEvent event = CraftEventFactory.callEntityKnockbackEvent((org.bukkit.craftbukkit.entity.CraftLivingEntity) entity.getBukkitEntity(), this.source, this.damageSource.getEntity() != null ? this.damageSource.getEntity() : this.source, io.papermc.paper.event.entity.EntityKnockbackEvent.Cause.EXPLOSION, d5, vec3); + vec3 = event.isCancelled() ? Vec3.ZERO : org.bukkit.craftbukkit.util.CraftVector.toNMS(event.getKnockback()); + // Paper end - knockback events + } + // CraftBukkit end entity.push(vec3); if (entity instanceof Player) { Player player = (Player)entity; - if (!player.isSpectator() && (!player.isCreative() || !player.getAbilities().flying)) { + if (!player.isSpectator() && (!player.isCreative() || !player.getAbilities().flying) && !level.paperConfig().environment.disableExplosionKnockback) { // Paper - Option to disable explosion knockback this.hitPlayers.put(player, vec3); } } @@ -225,7 +_,61 @@ List list = new ArrayList<>(); Util.shuffle(blocks, this.level.random); + // CraftBukkit start + org.bukkit.World bworld = this.level.getWorld(); + Location location = CraftLocation.toBukkit(this.center, bworld); + + List blockList = new ObjectArrayList<>(); + for (int i1 = blocks.size() - 1; i1 >= 0; i1--) { + BlockPos cpos = blocks.get(i1); + org.bukkit.block.Block bblock = bworld.getBlockAt(cpos.getX(), cpos.getY(), cpos.getZ()); + if (!bblock.getType().isAir()) { + blockList.add(bblock); + } + } + + List bukkitBlocks; + + if (this.source != null) { + EntityExplodeEvent event = CraftEventFactory.callEntityExplodeEvent(this.source, blockList, this.yield, this.getBlockInteraction()); + this.wasCanceled = event.isCancelled(); + bukkitBlocks = event.blockList(); + this.yield = event.getYield(); + } else { + org.bukkit.block.Block block = location.getBlock(); + org.bukkit.block.BlockState blockState = (this.damageSource.getDirectBlockState() != null) ? this.damageSource.getDirectBlockState() : block.getState(); + BlockExplodeEvent event = CraftEventFactory.callBlockExplodeEvent(block, blockState, blockList, this.yield, this.getBlockInteraction()); + this.wasCanceled = event.isCancelled(); + bukkitBlocks = event.blockList(); + this.yield = event.getYield(); + } + + blocks.clear(); + + for (org.bukkit.block.Block bblock : bukkitBlocks) { + BlockPos coords = new BlockPos(bblock.getX(), bblock.getY(), bblock.getZ()); + blocks.add(coords); + } + + if (this.wasCanceled) { + return; + } + // CraftBukkit end + for (BlockPos blockPos : blocks) { + // CraftBukkit start - TNTPrimeEvent + BlockState iblockdata = this.level.getBlockState(blockPos); + Block block = iblockdata.getBlock(); + if (block instanceof net.minecraft.world.level.block.TntBlock) { + Entity sourceEntity = this.source == null ? null : this.source; + BlockPos sourceBlock = sourceEntity == null ? BlockPos.containing(this.center) : null; + if (!CraftEventFactory.callTNTPrimeEvent(this.level, blockPos, org.bukkit.event.block.TNTPrimeEvent.PrimeCause.EXPLOSION, sourceEntity, sourceBlock)) { + this.level.sendBlockUpdated(blockPos, Blocks.AIR.defaultBlockState(), iblockdata, 3); // Update the block on the client + continue; + } + } + // CraftBukkit end + this.level .getBlockState(blockPos) .onExplosionHit(this.level, blockPos, this, (itemStack, blockPos1) -> addOrAppendStack(list, itemStack, blockPos1)); @@ -239,12 +_,21 @@ private void createFire(List blocks) { for (BlockPos blockPos : blocks) { if (this.level.random.nextInt(3) == 0 && this.level.getBlockState(blockPos).isAir() && this.level.getBlockState(blockPos.below()).isSolidRender()) { - this.level.setBlockAndUpdate(blockPos, BaseFireBlock.getState(this.level, blockPos)); + // CraftBukkit start - Ignition by explosion + if (!org.bukkit.craftbukkit.event.CraftEventFactory.callBlockIgniteEvent(this.level, blockPos, this).isCancelled()) { + this.level.setBlockAndUpdate(blockPos, BaseFireBlock.getState(this.level, blockPos)); + } + // CraftBukkit end } } } public void explode() { + // CraftBukkit start + if (this.radius < 0.1F) { + return; + } + // CraftBukkit end this.level.gameEvent(this.source, GameEvent.EXPLODE, this.center); List list = this.calculateExplodedPositions(); this.hurtEntities(); @@ -261,6 +_,7 @@ } private static void addOrAppendStack(List stackCollectors, ItemStack stack, BlockPos pos) { + if (stack.isEmpty()) return; // CraftBukkit - SPIGOT-5425 for (ServerExplosion.StackCollector stackCollector : stackCollectors) { stackCollector.tryMerge(stack); if (stack.isEmpty()) { @@ -342,4 +_,86 @@ } } } + + + // Paper start - Optimize explosions + private float getBlockDensity(Vec3 vec3d, Entity entity) { + if (!this.level.paperConfig().environment.optimizeExplosions) { + return getSeenPercent(vec3d, entity); + } + CacheKey key = new CacheKey(this, entity.getBoundingBox()); + Float blockDensity = this.level.explosionDensityCache.get(key); + if (blockDensity == null) { + blockDensity = getSeenPercent(vec3d, entity); + this.level.explosionDensityCache.put(key, blockDensity); + } + + return blockDensity; + } + + static class CacheKey { + private final Level world; + private final double posX, posY, posZ; + private final double minX, minY, minZ; + private final double maxX, maxY, maxZ; + + public CacheKey(Explosion explosion, AABB aabb) { + this.world = explosion.level(); + this.posX = explosion.center().x; + this.posY = explosion.center().y; + this.posZ = explosion.center().z; + this.minX = aabb.minX; + this.minY = aabb.minY; + this.minZ = aabb.minZ; + this.maxX = aabb.maxX; + this.maxY = aabb.maxY; + this.maxZ = aabb.maxZ; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + CacheKey cacheKey = (CacheKey) o; + + if (Double.compare(cacheKey.posX, posX) != 0) return false; + if (Double.compare(cacheKey.posY, posY) != 0) return false; + if (Double.compare(cacheKey.posZ, posZ) != 0) return false; + if (Double.compare(cacheKey.minX, minX) != 0) return false; + if (Double.compare(cacheKey.minY, minY) != 0) return false; + if (Double.compare(cacheKey.minZ, minZ) != 0) return false; + if (Double.compare(cacheKey.maxX, maxX) != 0) return false; + if (Double.compare(cacheKey.maxY, maxY) != 0) return false; + if (Double.compare(cacheKey.maxZ, maxZ) != 0) return false; + return world.equals(cacheKey.world); + } + + @Override + public int hashCode() { + int result; + long temp; + result = world.hashCode(); + temp = Double.doubleToLongBits(posX); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(posY); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(posZ); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(minX); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(minY); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(minZ); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(maxX); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(maxY); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(maxZ); + result = 31 * result + (int) (temp ^ (temp >>> 32)); + return result; + } + } + // Paper end }