PaperMC/paper-server/patches/sources/net/minecraft/world/level/ServerExplosion.java.patch

332 lines
18 KiB
Diff
Raw Normal View History

--- a/net/minecraft/world/level/ServerExplosion.java
+++ b/net/minecraft/world/level/ServerExplosion.java
2024-12-18 19:09:46 +01:00
@@ -33,6 +_,17 @@
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
2024-12-15 00:42:27 +01:00
+// 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
2024-12-15 00:42:27 +01:00
+
public class ServerExplosion implements Explosion {
2024-12-15 00:42:27 +01:00
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;
2024-12-15 00:42:27 +01:00
private final Map<Player, Vec3> 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
2024-12-15 00:42:27 +01:00
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) {
2024-12-15 00:42:27 +01:00
@@ -139,7 +_,8 @@
for (float f1 = 0.3F; f > 0.0F; f -= 0.22500001F) {
2024-12-15 00:42:27 +01:00
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;
2024-12-15 00:42:27 +01:00
}
@@ -152,6 +_,15 @@
2024-12-15 00:42:27 +01:00
if (f > 0.0F && this.damageCalculator.shouldBlockExplode(this, this.level, blockPos, blockState, f)) {
set.add(blockPos);
+ // Paper start - prevent headless pistons from forming
2024-12-18 19:09:46 +01:00
+ if (!io.papermc.paper.configuration.GlobalConfiguration.get().unsupportedSettings.allowHeadlessPistons && blockState.is(Blocks.MOVING_PISTON)) {
2024-12-15 00:42:27 +01:00
+ 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()) {
2024-12-15 00:42:27 +01:00
+ 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
}
2024-12-15 00:42:27 +01:00
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))) {
2024-12-18 19:09:46 +01:00
+ List <Entity> 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
2024-12-15 00:42:27 +01:00
+ 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:
2024-12-18 19:09:46 +01:00
+ // - Setting a velocity to a EnderDragonPart is ignored (and therefore not needed)
+ // - Damaging EnderDragonPart while forward the damage to EnderDragon
+ // - Damaging EntityEnderDragon does nothing
2024-12-18 19:09:46 +01:00
+ // - EnderDragon hitbock always covers the other parts and is therefore always present
+ if (entity instanceof EnderDragonPart) {
+ continue;
+ }
+
+ entity.lastDamageCancelled = false;
+
+ if (entity instanceof EnderDragon) {
2024-12-18 19:09:46 +01:00
+ for (EnderDragonPart dragonPart : ((EnderDragon) entity).getSubEntities()) {
+ // Calculate damage separately for each EntityComplexPart
2024-12-18 19:09:46 +01:00
+ if (list.contains(dragonPart)) {
+ dragonPart.hurtServer(this.level, this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity, f1));
+ }
+ }
+ } else {
2024-12-15 00:42:27 +01:00
+ 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
}
2024-12-15 00:42:27 +01:00
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
2016-03-02 21:48:03 +01:00
} else {
2024-12-15 00:42:27 +01:00
d5 = d4;
2016-03-02 21:48:03 +01:00
}
2024-12-15 00:42:27 +01:00
@@ -206,10 +_,18 @@
d2 *= d5;
d3 *= d5;
Vec3 vec3 = new Vec3(d1, d2, d3);
+ // CraftBukkit start - Call EntityKnockbackEvent
+ if (entity instanceof LivingEntity) {
2024-12-15 00:42:27 +01:00
+ // 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
2024-12-15 00:42:27 +01:00
entity.push(vec3);
if (entity instanceof Player) {
2024-12-15 00:42:27 +01:00
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);
2016-03-02 21:48:03 +01:00
}
}
2024-12-15 00:42:27 +01:00
@@ -225,7 +_,61 @@
List<ServerExplosion.StackCollector> 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<org.bukkit.block.Block> blockList = new ObjectArrayList<>();
2024-12-15 00:42:27 +01:00
+ 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<org.bukkit.block.Block> 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();
+ }
+
2024-12-15 00:42:27 +01:00
+ blocks.clear();
+
+ for (org.bukkit.block.Block bblock : bukkitBlocks) {
+ BlockPos coords = new BlockPos(bblock.getX(), bblock.getY(), bblock.getZ());
2024-12-15 00:42:27 +01:00
+ blocks.add(coords);
+ }
+
+ if (this.wasCanceled) {
+ return;
+ }
+ // CraftBukkit end
2024-12-15 00:42:27 +01:00
+
for (BlockPos blockPos : blocks) {
+ // CraftBukkit start - TNTPrimeEvent
2024-12-15 00:42:27 +01:00
+ 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;
2024-12-15 00:42:27 +01:00
+ 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
2024-12-15 00:42:27 +01:00
+
this.level
.getBlockState(blockPos)
.onExplosionHit(this.level, blockPos, this, (itemStack, blockPos1) -> addOrAppendStack(list, itemStack, blockPos1));
@@ -239,12 +_,21 @@
private void createFire(List<BlockPos> 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
2024-12-15 00:42:27 +01:00
+ 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
2024-12-15 00:42:27 +01:00
this.level.gameEvent(this.source, GameEvent.EXPLODE, this.center);
List<BlockPos> list = this.calculateExplodedPositions();
2024-12-15 00:42:27 +01:00
this.hurtEntities();
@@ -261,6 +_,7 @@
}
2024-12-15 00:42:27 +01:00
private static void addOrAppendStack(List<ServerExplosion.StackCollector> 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 @@
}
}
}
+
2024-12-15 00:42:27 +01:00
+
+ // 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
}