SPIGOT-7300, #1180: Add new DamageSource API providing enhanced information about entity damage

By: Doc <nachito94@msn.com>
This commit is contained in:
CraftBukkit/Spigot
2024-02-11 09:54:25 +11:00
parent db4af65c2e
commit 49b5ee78bb
29 changed files with 640 additions and 310 deletions

View File

@@ -28,7 +28,7 @@
public static DamageSource getDefaultDamageSource(World world, @Nullable Entity entity) {
return world.damageSources().explosion(entity, getIndirectSourceEntityInternal(entity));
@@ -85,7 +99,7 @@
@@ -85,17 +99,18 @@
this.hitPlayers = Maps.newHashMap();
this.level = world;
this.source = entity;
@@ -37,7 +37,11 @@
this.x = d0;
this.y = d1;
this.z = d2;
@@ -96,6 +110,7 @@
this.fire = flag;
this.blockInteraction = explosion_effect;
- this.damageSource = damagesource == null ? world.damageSources().explosion(this) : damagesource;
+ this.damageSource = damagesource == null ? world.damageSources().explosion(this).customCausingEntity(entity) : damagesource.customCausingEntity(entity); // CraftBukkit - handle source entity
this.damageCalculator = explosiondamagecalculator == null ? this.makeDamageCalculator(entity) : explosiondamagecalculator;
this.smallExplosionParticles = particleparam;
this.largeExplosionParticles = particleparam1;
this.explosionSound = soundeffect;
@@ -57,7 +61,7 @@
this.level.gameEvent(this.source, GameEvent.EXPLODE, new Vec3D(this.x, this.y, this.z));
Set<BlockPosition> set = Sets.newHashSet();
boolean flag = true;
@@ -228,7 +248,37 @@
@@ -228,7 +248,35 @@
d9 /= d11;
d10 /= d11;
if (this.damageCalculator.shouldDamageEntity(this, entity)) {
@@ -74,7 +78,6 @@
+ continue;
+ }
+
+ CraftEventFactory.entityDamage = source;
+ entity.lastDamageCancelled = false;
+
+ if (entity instanceof EntityEnderDragon) {
@@ -88,7 +91,6 @@
+ entity.hurt(this.damageSource, this.damageCalculator.getEntityDamageAmount(this, entity));
+ }
+
+ CraftEventFactory.entityDamage = null;
+ if (entity.lastDamageCancelled) { // SPIGOT-5339, SPIGOT-6252, SPIGOT-6777: Skip entity if damage event was cancelled
+ continue;
+ }
@@ -96,7 +98,7 @@
}
double d12 = (1.0D - d7) * (double) getSeenPercent(vec3d, entity);
@@ -247,6 +297,14 @@
@@ -247,6 +295,14 @@
d10 *= d13;
Vec3D vec3d1 = new Vec3D(d8, d9, d10);
@@ -111,7 +113,7 @@
entity.setDeltaMovement(entity.getDeltaMovement().add(vec3d1));
if (entity instanceof EntityHuman) {
EntityHuman entityhuman = (EntityHuman) entity;
@@ -287,9 +345,63 @@
@@ -287,9 +343,63 @@
SystemUtils.shuffle(this.toBlow, this.level.random);
ObjectListIterator objectlistiterator = this.toBlow.iterator();
@@ -175,7 +177,7 @@
this.level.getBlockState(blockposition).onExplosionHit(this.level, blockposition, this, (itemstack, blockposition1) -> {
addOrAppendStack(list, itemstack, blockposition1);
@@ -314,7 +426,11 @@
@@ -314,7 +424,11 @@
BlockPosition blockposition1 = (BlockPosition) objectlistiterator1.next();
if (this.random.nextInt(3) == 0 && this.level.getBlockState(blockposition1).isAir() && this.level.getBlockState(blockposition1.below()).isSolidRender(this.level, blockposition1.below())) {
@@ -188,7 +190,7 @@
}
}
}
@@ -322,6 +438,7 @@
@@ -322,6 +436,7 @@
}
private static void addOrAppendStack(List<Pair<ItemStack, BlockPosition>> list, ItemStack itemstack, BlockPosition blockposition) {

View File

@@ -18,13 +18,12 @@
IBlockData iblockdata1 = (IBlockData) iblockdata.setValue(BlockCactus.AGE, 0);
worldserver.setBlock(blockposition, iblockdata1, 4);
@@ -119,7 +121,9 @@
@@ -119,7 +121,7 @@
@Override
public void entityInside(IBlockData iblockdata, World world, BlockPosition blockposition, Entity entity) {
+ CraftEventFactory.blockDamage = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()); // CraftBukkit
entity.hurt(world.damageSources().cactus(), 1.0F);
+ CraftEventFactory.blockDamage = null; // CraftBukkit
- entity.hurt(world.damageSources().cactus(), 1.0F);
+ entity.hurt(world.damageSources().cactus().directBlock(world, blockposition), 1.0F); // CraftBukkit
}
@Override

View File

@@ -1,27 +1,15 @@
--- a/net/minecraft/world/level/block/BlockCampfire.java
+++ b/net/minecraft/world/level/block/BlockCampfire.java
@@ -50,6 +50,10 @@
import net.minecraft.world.phys.shapes.VoxelShapeCollision;
import net.minecraft.world.phys.shapes.VoxelShapes;
+// CraftBukkit start
+import org.bukkit.craftbukkit.block.CraftBlock;
+// CraftBukkit end
+
public class BlockCampfire extends BlockTileEntity implements IBlockWaterlogged {
public static final MapCodec<BlockCampfire> CODEC = RecordCodecBuilder.mapCodec((instance) -> {
@@ -106,7 +110,9 @@
@@ -106,7 +106,7 @@
@Override
public void entityInside(IBlockData iblockdata, World world, BlockPosition blockposition, Entity entity) {
if ((Boolean) iblockdata.getValue(BlockCampfire.LIT) && entity instanceof EntityLiving && !EnchantmentManager.hasFrostWalker((EntityLiving) entity)) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.blockDamage = CraftBlock.at(world, blockposition); // CraftBukkit
entity.hurt(world.damageSources().inFire(), (float) this.fireDamage);
+ org.bukkit.craftbukkit.event.CraftEventFactory.blockDamage = null; // CraftBukkit
- entity.hurt(world.damageSources().inFire(), (float) this.fireDamage);
+ entity.hurt(world.damageSources().inFire().directBlock(world, blockposition), (float) this.fireDamage); // CraftBukkit
}
super.entityInside(iblockdata, world, blockposition, entity);
@@ -216,6 +222,11 @@
@@ -216,6 +216,11 @@
BlockPosition blockposition = movingobjectpositionblock.getBlockPos();
if (!world.isClientSide && iprojectile.isOnFire() && iprojectile.mayInteract(world, blockposition) && !(Boolean) iblockdata.getValue(BlockCampfire.LIT) && !(Boolean) iblockdata.getValue(BlockCampfire.WATERLOGGED)) {

View File

@@ -1,12 +1,11 @@
--- a/net/minecraft/world/level/block/BlockMagma.java
+++ b/net/minecraft/world/level/block/BlockMagma.java
@@ -30,7 +30,9 @@
@@ -30,7 +30,7 @@
@Override
public void stepOn(World world, BlockPosition blockposition, IBlockData iblockdata, Entity entity) {
if (!entity.isSteppingCarefully() && entity instanceof EntityLiving && !EnchantmentManager.hasFrostWalker((EntityLiving) entity)) {
+ org.bukkit.craftbukkit.event.CraftEventFactory.blockDamage = world.getWorld().getBlockAt(blockposition.getX(), blockposition.getY(), blockposition.getZ()); // CraftBukkit
entity.hurt(world.damageSources().hotFloor(), 1.0F);
+ org.bukkit.craftbukkit.event.CraftEventFactory.blockDamage = null; // CraftBukkit
- entity.hurt(world.damageSources().hotFloor(), 1.0F);
+ entity.hurt(world.damageSources().hotFloor().directBlock(world, blockposition), 1.0F); // CraftBukkit
}
super.stepOn(world, blockposition, iblockdata, entity);

View File

@@ -1,12 +1,11 @@
--- a/net/minecraft/world/level/block/BlockSweetBerryBush.java
+++ b/net/minecraft/world/level/block/BlockSweetBerryBush.java
@@ -28,6 +28,14 @@
@@ -28,6 +28,13 @@
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.phys.shapes.VoxelShapeCollision;
+// CraftBukkit start
+import java.util.Collections;
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+import org.bukkit.craftbukkit.inventory.CraftItemStack;
+import org.bukkit.event.player.PlayerHarvestBlockEvent;
@@ -15,7 +14,7 @@
public class BlockSweetBerryBush extends BlockPlant implements IBlockFragilePlantElement {
public static final MapCodec<BlockSweetBerryBush> CODEC = simpleCodec(BlockSweetBerryBush::new);
@@ -69,7 +77,7 @@
@@ -69,7 +76,7 @@
if (i < 3 && randomsource.nextInt(5) == 0 && worldserver.getRawBrightness(blockposition.above(), 0) >= 9) {
IBlockData iblockdata1 = (IBlockData) iblockdata.setValue(BlockSweetBerryBush.AGE, i + 1);
@@ -24,17 +23,16 @@
worldserver.gameEvent(GameEvent.BLOCK_CHANGE, blockposition, GameEvent.a.of(iblockdata1));
}
@@ -84,7 +92,9 @@
@@ -84,7 +91,7 @@
double d1 = Math.abs(entity.getZ() - entity.zOld);
if (d0 >= 0.003000000026077032D || d1 >= 0.003000000026077032D) {
+ CraftEventFactory.blockDamage = CraftBlock.at(world, blockposition); // CraftBukkit
entity.hurt(world.damageSources().sweetBerryBush(), 1.0F);
+ CraftEventFactory.blockDamage = null; // CraftBukkit
- entity.hurt(world.damageSources().sweetBerryBush(), 1.0F);
+ entity.hurt(world.damageSources().sweetBerryBush().directBlock(world, blockposition), 1.0F); // CraftBukkit
}
}
@@ -101,7 +111,15 @@
@@ -101,7 +108,15 @@
} else if (i > 1) {
int j = 1 + world.random.nextInt(2);

View File

@@ -1,40 +1,27 @@
--- a/net/minecraft/world/level/block/PointedDripstoneBlock.java
+++ b/net/minecraft/world/level/block/PointedDripstoneBlock.java
@@ -43,6 +43,11 @@
import net.minecraft.world.phys.shapes.VoxelShapeCollision;
import net.minecraft.world.phys.shapes.VoxelShapes;
+// CraftBukkit start
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+// CraftBukkit end
+
public class PointedDripstoneBlock extends Block implements Fallable, IBlockWaterlogged {
public static final MapCodec<PointedDripstoneBlock> CODEC = simpleCodec(PointedDripstoneBlock::new);
@@ -132,6 +137,11 @@
@@ -132,6 +132,11 @@
BlockPosition blockposition = movingobjectpositionblock.getBlockPos();
if (iprojectile.mayInteract(world, blockposition) && iprojectile.mayBreak(world) && iprojectile instanceof EntityThrownTrident && iprojectile.getDeltaMovement().length() > 0.6D) {
+ // CraftBukkit start
+ if (!CraftEventFactory.callEntityChangeBlockEvent(iprojectile, blockposition, Blocks.AIR.defaultBlockState())) {
+ if (!org.bukkit.craftbukkit.event.CraftEventFactory.callEntityChangeBlockEvent(iprojectile, blockposition, Blocks.AIR.defaultBlockState())) {
+ return;
+ }
+ // CraftBukkit end
world.destroyBlock(blockposition, true);
}
@@ -141,7 +151,9 @@
@@ -141,7 +146,7 @@
@Override
public void fallOn(World world, IBlockData iblockdata, BlockPosition blockposition, Entity entity, float f) {
if (iblockdata.getValue(PointedDripstoneBlock.TIP_DIRECTION) == EnumDirection.UP && iblockdata.getValue(PointedDripstoneBlock.THICKNESS) == DripstoneThickness.TIP) {
+ CraftEventFactory.blockDamage = CraftBlock.at(world, blockposition); // CraftBukkit
entity.causeFallDamage(f + 2.0F, 2.0F, world.damageSources().stalagmite());
+ CraftEventFactory.blockDamage = null; // CraftBukkit
- entity.causeFallDamage(f + 2.0F, 2.0F, world.damageSources().stalagmite());
+ entity.causeFallDamage(f + 2.0F, 2.0F, world.damageSources().stalagmite().directBlock(world, blockposition)); // CraftBukkit
} else {
super.fallOn(world, iblockdata, blockposition, entity, f);
}
@@ -386,15 +398,15 @@
@@ -386,15 +391,15 @@
if (isUnmergedTipWithDirection(iblockdata, enumdirection.getOpposite())) {
createMergedTips(iblockdata, worldserver, blockposition1);
} else if (iblockdata.isAir() || iblockdata.is(Blocks.WATER)) {
@@ -53,7 +40,7 @@
}
private static void createMergedTips(IBlockData iblockdata, GeneratorAccess generatoraccess, BlockPosition blockposition) {
@@ -409,8 +421,8 @@
@@ -409,8 +414,8 @@
blockposition1 = blockposition.below();
}
@@ -64,7 +51,7 @@
}
public static void spawnDripParticle(World world, BlockPosition blockposition, IBlockData iblockdata) {
@@ -443,7 +455,7 @@
@@ -443,7 +448,7 @@
return (BlockPosition) findBlockVertical(generatoraccess, blockposition, enumdirection.getAxisDirection(), bipredicate, (iblockdata1) -> {
return isTip(iblockdata1, flag);
@@ -73,7 +60,7 @@
}
}
@@ -559,7 +571,7 @@
@@ -559,7 +564,7 @@
return canDripThrough(world, blockposition1, iblockdata);
};
@@ -82,7 +69,7 @@
}
@Nullable
@@ -568,7 +580,7 @@
@@ -568,7 +573,7 @@
return canDripThrough(world, blockposition1, iblockdata);
};

View File

@@ -1,18 +1,6 @@
--- a/net/minecraft/world/level/block/entity/TileEntityConduit.java
+++ b/net/minecraft/world/level/block/entity/TileEntityConduit.java
@@ -27,6 +27,11 @@
import net.minecraft.world.phys.AxisAlignedBB;
import net.minecraft.world.phys.Vec3D;
+// CraftBukkit start
+import org.bukkit.craftbukkit.block.CraftBlock;
+import org.bukkit.craftbukkit.event.CraftEventFactory;
+// CraftBukkit end
+
public class TileEntityConduit extends TileEntity {
private static final int BLOCK_REFRESH_RATE = 2;
@@ -201,7 +206,7 @@
@@ -201,7 +201,7 @@
EntityHuman entityhuman = (EntityHuman) iterator.next();
if (blockposition.closerThan(entityhuman.blockPosition(), (double) j) && entityhuman.isInWaterOrRain()) {
@@ -21,18 +9,16 @@
}
}
@@ -230,8 +235,13 @@
@@ -230,8 +230,11 @@
}
if (tileentityconduit.destroyTarget != null) {
- world.playSound((EntityHuman) null, tileentityconduit.destroyTarget.getX(), tileentityconduit.destroyTarget.getY(), tileentityconduit.destroyTarget.getZ(), SoundEffects.CONDUIT_ATTACK_TARGET, SoundCategory.BLOCKS, 1.0F, 1.0F);
- tileentityconduit.destroyTarget.hurt(world.damageSources().magic(), 4.0F);
+ // CraftBukkit start
+ CraftEventFactory.blockDamage = CraftBlock.at(world, blockposition);
+ if (tileentityconduit.destroyTarget.hurt(world.damageSources().magic(), 4.0F)) {
+ world.playSound((EntityHuman) null, tileentityconduit.destroyTarget.getX(), tileentityconduit.destroyTarget.getY(), tileentityconduit.destroyTarget.getZ(), SoundEffects.CONDUIT_ATTACK_TARGET, SoundCategory.BLOCKS, 1.0F, 1.0F);
+ if (tileentityconduit.destroyTarget.hurt(world.damageSources().magic().directBlock(world, blockposition), 4.0F)) { // CraftBukkit
+ world.playSound(null, tileentityconduit.destroyTarget.getX(), tileentityconduit.destroyTarget.getY(), tileentityconduit.destroyTarget.getZ(), SoundEffects.CONDUIT_ATTACK_TARGET, SoundCategory.BLOCKS, 1.0F, 1.0F);
+ }
+ CraftEventFactory.blockDamage = null;
+ // CraftBukkit end
}