2024-03-16 19:51:22 +01:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jake Potrebic <jake.m.potrebic@gmail.com>
Date: Sat, 9 Mar 2024 14:13:04 -0800
Subject: [PATCH] Fix DamageSource API
Uses the correct entity in the EntityDamageByEntity event
Returns the correct entity for API's DamageSource#getCausingEntity
diff --git a/src/main/java/net/minecraft/world/damagesource/DamageSource.java b/src/main/java/net/minecraft/world/damagesource/DamageSource.java
2024-05-11 23:48:37 +02:00
index 533ea6cbb813c8d1dc2bb3f65fd94d4fa5dd591e..dd9638bdb228a53e72820e0e7cf6fe6fcc08fe4b 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/damagesource/DamageSource.java
+++ b/src/main/java/net/minecraft/world/damagesource/DamageSource.java
2024-04-30 00:16:07 +02:00
@@ -29,7 +29,8 @@ public class DamageSource {
2024-05-11 23:48:37 +02:00
private boolean sweep = false;
2024-03-16 19:51:22 +01:00
private boolean melting = false;
private boolean poison = false;
2024-05-11 23:48:37 +02:00
- private Entity customEntityDamager = null; // This field is a helper for when causing entity damage is not set by vanilla
2024-03-16 19:51:22 +01:00
+ @Nullable
+ private Entity customEventDamager = null; // This field is a helper for when causing entity damage is not set by vanilla // Paper - fix DamageSource API
public DamageSource sweep() {
2024-05-11 23:48:37 +02:00
this.sweep = true;
@@ -58,18 +59,19 @@ public class DamageSource {
2024-03-16 19:51:22 +01:00
return this.poison;
}
2024-05-11 23:48:37 +02:00
- public Entity getDamager() {
- return (this.customEntityDamager != null) ? this.customEntityDamager : this.directEntity;
2024-03-16 19:51:22 +01:00
+ // Paper start - fix DamageSource API
2024-05-11 23:48:37 +02:00
+ @Nullable
+ public Entity getCustomEventDamager() {
2024-03-16 19:51:22 +01:00
+ return (this.customEventDamager != null) ? this.customEventDamager : this.directEntity;
}
2024-05-11 23:48:37 +02:00
- public DamageSource customEntityDamager(Entity entity) {
2024-04-25 12:27:57 +02:00
- // This method is not intended for change the causing entity if is already set
- // also is only necessary if the entity passed is not the direct entity or different from the current causingEntity
2024-05-11 23:48:37 +02:00
- if (this.customEntityDamager != null || this.directEntity == entity || this.causingEntity == entity) {
2024-04-25 12:27:57 +02:00
- return this;
2024-03-16 19:51:22 +01:00
+ public DamageSource customEventDamager(Entity entity) {
+ if (this.directEntity != null) {
2024-04-25 12:27:57 +02:00
+ throw new IllegalStateException("Cannot set custom event damager when direct entity is already set (report a bug to Paper)");
}
2024-03-16 19:51:22 +01:00
DamageSource damageSource = this.cloneInstance();
2024-05-11 23:48:37 +02:00
- damageSource.customEntityDamager = entity;
2024-03-16 19:51:22 +01:00
+ damageSource.customEventDamager = entity;
+ // Paper end - fix DamageSource API
return damageSource;
}
2024-05-11 23:48:37 +02:00
diff --git a/src/main/java/net/minecraft/world/damagesource/DamageSources.java b/src/main/java/net/minecraft/world/damagesource/DamageSources.java
index abda6094e02ebd2589ba0e4760e574fcf44dc8e4..5ec8cbd07a1830876f58e1fd33de6df4466d7e95 100644
--- a/src/main/java/net/minecraft/world/damagesource/DamageSources.java
+++ b/src/main/java/net/minecraft/world/damagesource/DamageSources.java
@@ -258,13 +258,7 @@ public class DamageSources {
}
public DamageSource explosion(@Nullable Entity source, @Nullable Entity attacker) {
- // CraftBukkit start
- return this.explosion(source, attacker, attacker != null && source != null ? DamageTypes.PLAYER_EXPLOSION : DamageTypes.EXPLOSION);
- }
-
- public DamageSource explosion(@Nullable Entity entity, @Nullable Entity entity1, ResourceKey<DamageType> resourceKey) {
- return this.source(resourceKey, entity, entity1);
- // CraftBukkit end
+ return this.source(attacker != null && source != null ? DamageTypes.PLAYER_EXPLOSION : DamageTypes.EXPLOSION, source, attacker); // Paper - revert to vanilla
}
public DamageSource sonicBoom(Entity attacker) {
2024-03-16 19:51:22 +01:00
diff --git a/src/main/java/net/minecraft/world/entity/Entity.java b/src/main/java/net/minecraft/world/entity/Entity.java
2024-05-11 23:48:37 +02:00
index 0782fd7bf29aa4d30ea5a9a303cc43620fe5b06e..3c1bcf8d1a07b35a8688160c9f05f792451338a3 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/Entity.java
+++ b/src/main/java/net/minecraft/world/entity/Entity.java
2024-04-25 12:27:57 +02:00
@@ -3195,7 +3195,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
2024-03-16 19:51:22 +01:00
return;
}
2024-05-11 23:48:37 +02:00
- if (!this.hurt(this.damageSources().lightningBolt().customEntityDamager(lightning), 5.0F)) {
2024-03-16 19:51:22 +01:00
+ if (!this.hurt(this.damageSources().lightningBolt().customEventDamager(lightning), 5.0F)) { // Paper - fix DamageSource API
return;
}
// CraftBukkit end
diff --git a/src/main/java/net/minecraft/world/entity/animal/Turtle.java b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
2024-05-11 23:48:37 +02:00
index b4d68a08ec1e4ec7bcc123bcb6c2dc90272430cf..ffffa53fcaa6ec8681b283fa20bb5a3320ad9c11 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/animal/Turtle.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Turtle.java
2024-04-25 12:27:57 +02:00
@@ -334,7 +334,7 @@ public class Turtle extends Animal {
2024-03-16 19:51:22 +01:00
@Override
public void thunderHit(ServerLevel world, LightningBolt lightning) {
2024-05-11 23:48:37 +02:00
- this.hurt(this.damageSources().lightningBolt().customEntityDamager(lightning), Float.MAX_VALUE); // CraftBukkit
2024-03-16 19:51:22 +01:00
+ this.hurt(this.damageSources().lightningBolt().customEventDamager(lightning), Float.MAX_VALUE); // CraftBukkit // Paper - fix DamageSource API
}
@Override
diff --git a/src/main/java/net/minecraft/world/entity/decoration/HangingEntity.java b/src/main/java/net/minecraft/world/entity/decoration/HangingEntity.java
2024-04-25 12:27:57 +02:00
index 0ffff5329fa2c7833f9ec71528cb7f951cf78109..bf2d91bbb4bf401696f5f5d14a67e3920a179084 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/decoration/HangingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/decoration/HangingEntity.java
2024-04-25 12:27:57 +02:00
@@ -203,7 +203,7 @@ public abstract class HangingEntity extends Entity {
2024-03-16 19:51:22 +01:00
} else {
if (!this.isRemoved() && !this.level().isClientSide) {
// CraftBukkit start - fire break events
- Entity damager = (source.isIndirect()) ? source.getEntity() : source.getDirectEntity();
+ Entity damager = (source.isIndirect() && source.getEntity() != null) ? source.getEntity() : source.getDirectEntity(); // Paper - fix DamageSource API
HangingBreakEvent event;
if (damager != null) {
event = new HangingBreakByEntityEvent((Hanging) this.getBukkitEntity(), damager.getBukkitEntity(), source.is(DamageTypeTags.IS_EXPLOSION) ? HangingBreakEvent.RemoveCause.EXPLOSION : HangingBreakEvent.RemoveCause.ENTITY);
2024-04-25 12:27:57 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/monster/Creeper.java b/src/main/java/net/minecraft/world/entity/monster/Creeper.java
2024-05-11 23:48:37 +02:00
index 7db5de71b0353d9807978c0351870fd99b76ee67..0ae4ba060b2ce2c79e1235c939f3c1926eb6e33e 100644
2024-04-25 12:27:57 +02:00
--- a/src/main/java/net/minecraft/world/entity/monster/Creeper.java
+++ b/src/main/java/net/minecraft/world/entity/monster/Creeper.java
@@ -271,7 +271,7 @@ public class Creeper extends Monster implements PowerableMob {
if (!event.isCancelled()) {
// CraftBukkit end
this.dead = true;
2024-05-11 23:48:37 +02:00
- this.level().explode(this, this.level().damageSources().explosion(this, this.entityIgniter, net.minecraft.world.damagesource.DamageTypes.EXPLOSION), null, this.getX(), this.getY(), this.getZ(), event.getRadius(), event.getFire(), Level.ExplosionInteraction.MOB); // CraftBukkit
+ this.level().explode(this, this.getX(), this.getY(), this.getZ(), event.getRadius(), event.getFire(), Level.ExplosionInteraction.MOB); // CraftBukkit // Paper - fix DamageSource API (revert to vanilla, no, just no, don't change this)
2024-04-25 12:27:57 +02:00
this.discard(EntityRemoveEvent.Cause.EXPLODE); // CraftBukkit - add Bukkit remove cause
this.spawnLingeringCloud();
// CraftBukkit start
2024-03-16 19:51:22 +01:00
diff --git a/src/main/java/net/minecraft/world/entity/projectile/EvokerFangs.java b/src/main/java/net/minecraft/world/entity/projectile/EvokerFangs.java
2024-05-11 23:48:37 +02:00
index edc5cfd3f3eab176df9080cf932dac3905f1b26d..f8aa723f18e28b02d4b7055404922ff8d7a73f8d 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/projectile/EvokerFangs.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/EvokerFangs.java
2024-04-25 12:27:57 +02:00
@@ -133,7 +133,7 @@ public class EvokerFangs extends Entity implements TraceableEntity {
2024-03-16 19:51:22 +01:00
if (target.isAlive() && !target.isInvulnerable() && target != entityliving1) {
if (entityliving1 == null) {
2024-05-11 23:48:37 +02:00
- target.hurt(this.damageSources().magic().customEntityDamager(this), 6.0F); // CraftBukkit
2024-03-16 19:51:22 +01:00
+ target.hurt(this.damageSources().magic().customEventDamager(this), 6.0F); // CraftBukkit // Paper - fix DamageSource API
} else {
if (entityliving1.isAlliedTo((Entity) target)) {
return;
diff --git a/src/main/java/net/minecraft/world/entity/projectile/ThrownEnderpearl.java b/src/main/java/net/minecraft/world/entity/projectile/ThrownEnderpearl.java
2024-05-11 23:48:37 +02:00
index ced6bf2fc4556c9b2b32ac382058506e931c1475..519755b7f8bc7e8bb9fab135fc5bf7de3a9419f9 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/projectile/ThrownEnderpearl.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/ThrownEnderpearl.java
2024-05-11 23:48:37 +02:00
@@ -85,7 +85,7 @@ public class ThrownEnderpearl extends ThrowableItemProjectile {
2024-03-16 19:51:22 +01:00
entityplayer.connection.teleport(teleEvent.getTo());
entity.resetFallDistance();
2024-05-11 23:48:37 +02:00
- entity.hurt(this.damageSources().fall().customEntityDamager(this), 5.0F); // CraftBukkit
2024-03-16 19:51:22 +01:00
+ entity.hurt(this.damageSources().fall().customEventDamager(this), 5.0F); // CraftBukkit // Paper - fix DamageSource API
}
// CraftBukkit end
this.level().playSound((Player) null, this.getX(), this.getY(), this.getZ(), SoundEvents.PLAYER_TELEPORT, SoundSource.PLAYERS);
diff --git a/src/main/java/net/minecraft/world/entity/projectile/WitherSkull.java b/src/main/java/net/minecraft/world/entity/projectile/WitherSkull.java
2024-04-25 12:27:57 +02:00
index 02931861de955352e71d6f5ffc720a17304815fe..55b4b5ad5f084c9a271a716d076672478d6486ba 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/net/minecraft/world/entity/projectile/WitherSkull.java
+++ b/src/main/java/net/minecraft/world/entity/projectile/WitherSkull.java
@@ -72,7 +72,7 @@ public class WitherSkull extends AbstractHurtingProjectile {
}
}
} else {
- flag = entity.hurt(this.damageSources().magic().customCausingEntity(this), 5.0F); // Paper - Fire EntityDamageByEntityEvent for unowned wither skulls
+ flag = entity.hurt(this.damageSources().magic().customEventDamager(this), 5.0F); // Paper - Fire EntityDamageByEntityEvent for unowned wither skulls // Paper - fix DamageSource API
}
if (flag && entity instanceof LivingEntity) {
diff --git a/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSource.java b/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSource.java
2024-05-11 23:48:37 +02:00
index ab67c5caaff6e8c7de293b528636f53254b805bd..6d92856b5089e946a85d519cadce080bc6c3574f 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSource.java
+++ b/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSource.java
2024-05-11 23:48:37 +02:00
@@ -47,7 +47,7 @@ public class CraftDamageSource implements DamageSource {
2024-03-16 19:51:22 +01:00
@Override
2024-05-11 23:48:37 +02:00
public org.bukkit.entity.Entity getDirectEntity() {
- net.minecraft.world.entity.Entity entity = this.getHandle().getDamager();
+ net.minecraft.world.entity.Entity entity = this.getHandle().getDirectEntity(); // Paper - fix DamageSource API
2024-03-16 19:51:22 +01:00
return (entity != null) ? entity.getBukkitEntity() : null;
}
@@ -65,7 +65,7 @@ public class CraftDamageSource implements DamageSource {
@Override
public boolean isIndirect() {
2024-05-11 23:48:37 +02:00
- return this.getHandle().getEntity() != this.getHandle().getDamager();
2024-03-16 19:51:22 +01:00
+ return this.getHandle().isIndirect(); // Paper - fix DamageSource API
}
@Override
diff --git a/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSourceBuilder.java b/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSourceBuilder.java
index 4c6e15535fa40aad8cf1920f392589404f9ba79c..35eb95ef6fb6a0f7ea63351e90741c489fdd15f9 100644
--- a/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSourceBuilder.java
+++ b/src/main/java/org/bukkit/craftbukkit/damage/CraftDamageSourceBuilder.java
@@ -41,6 +41,11 @@ public class CraftDamageSourceBuilder implements DamageSource.Builder {
@Override
public DamageSource build() {
+ // Paper start - fix DamageCause API
+ if (this.causingEntity != null && this.directEntity == null) {
+ throw new IllegalArgumentException("Direct entity must be set if causing entity is set");
+ }
+ // Paper end - fix DamageCause API
return CraftDamageSource.buildFromBukkit(this.damageType, this.causingEntity, this.directEntity, this.damageLocation);
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-05-11 23:48:37 +02:00
index 1d526e76eca94cf2dce473cff2b56ea9f7f0ec18..e075770772b84a79f2e05f273f31a4aeb479ff98 100644
2024-03-16 19:51:22 +01:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-05-11 23:48:37 +02:00
@@ -1084,7 +1084,7 @@ public class CraftEventFactory {
2024-03-16 19:51:22 +01:00
private static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, Map<DamageModifier, Double> modifiers, Map<DamageModifier, Function<? super Double, Double>> modifierFunctions, boolean cancelled) {
CraftDamageSource bukkitDamageSource = new CraftDamageSource(source);
2024-05-11 23:48:37 +02:00
- Entity damager = (source.getDamager() != null) ? source.getDamager() : source.getEntity();
2024-04-25 23:28:56 +02:00
+ final Entity damager = source.getCustomEventDamager(); // Paper - fix DamageSource API
2024-03-16 19:51:22 +01:00
if (source.is(DamageTypeTags.IS_EXPLOSION)) {
if (damager == null) {
2024-05-11 23:48:37 +02:00
return CraftEventFactory.callEntityDamageEvent(source.getDirectBlock(), source.getDirectBlockState(), entity, DamageCause.BLOCK_EXPLOSION, bukkitDamageSource, modifiers, modifierFunctions, cancelled);