Fix DamageSource API

Uses the correct entity in the EntityDamageByEntity event
Returns the correct entity for API's DamageSource#getCausingEntity
This commit is contained in:
Jake Potrebic
2024-03-09 14:13:04 -08:00
parent a87a5e10d9
commit d42ef390ea
12 changed files with 36 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
--- a/net/minecraft/world/damagesource/DamageSource.java
+++ b/net/minecraft/world/damagesource/DamageSource.java
@@ -21,7 +21,120 @@
@@ -21,7 +21,106 @@
private final Entity directEntity;
@Nullable
private final Vec3 damageSourcePosition;
@@ -12,8 +12,8 @@
+ private boolean sweep = false;
+ private boolean melting = false;
+ private boolean poison = false;
+ private Entity customEntityDamager = null; // This field is a helper for when direct entity damage is not set by vanilla
+ private Entity customCausingEntityDamager = null; // This field is a helper for when causing entity damage is not set by vanilla
+ @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() {
+ this.sweep = true;
@@ -42,33 +42,19 @@
+ return this.poison;
+ }
+
+ public Entity getDamager() {
+ return (this.customEntityDamager != null) ? this.customEntityDamager : this.directEntity;
+ // Paper start - fix DamageSource API
+ @Nullable
+ public Entity getCustomEventDamager() {
+ return (this.customEventDamager != null) ? this.customEventDamager : this.directEntity;
+ }
+
+ public Entity getCausingDamager() {
+ return (this.customCausingEntityDamager != null) ? this.customCausingEntityDamager : this.causingEntity;
+ }
+
+ public DamageSource customEntityDamager(Entity entity) {
+ // 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
+ if (this.customEntityDamager != null || this.directEntity == entity || this.causingEntity == entity) {
+ return this;
+ public DamageSource customEventDamager(Entity entity) {
+ if (this.directEntity != null) {
+ throw new IllegalStateException("Cannot set custom event damager when direct entity is already set (report a bug to Paper)");
+ }
+ DamageSource damageSource = this.cloneInstance();
+ damageSource.customEntityDamager = entity;
+ return damageSource;
+ }
+
+ public DamageSource customCausingEntityDamager(Entity entity) {
+ // 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
+ if (this.customCausingEntityDamager != null || this.directEntity == entity || this.causingEntity == entity) {
+ return this;
+ }
+ DamageSource damageSource = this.cloneInstance();
+ damageSource.customCausingEntityDamager = entity;
+ damageSource.customEventDamager = entity;
+ // Paper end - fix DamageSource API
+ return damageSource;
+ }
+
@@ -121,7 +107,7 @@
public String toString() {
return "DamageSource (" + this.type().msgId() + ")";
}
@@ -163,4 +276,18 @@
@@ -163,4 +262,18 @@
public Holder<DamageType> typeHolder() {
return this.type;
}

View File

@@ -16,10 +16,11 @@
this.inFire = this.source(DamageTypes.IN_FIRE);
this.campfire = this.source(DamageTypes.CAMPFIRE);
this.lightningBolt = this.source(DamageTypes.LIGHTNING_BOLT);
@@ -84,6 +90,16 @@
@@ -83,7 +89,17 @@
private DamageSource source(ResourceKey<DamageType> key, @Nullable Entity source, @Nullable Entity attacker) {
return new DamageSource(this.damageTypes.getOrThrow(key), source, attacker);
}
+ }
+
+ // CraftBukkit start
+ public DamageSource melting() {
@@ -28,27 +29,21 @@
+
+ public DamageSource poison() {
+ return this.poison;
+ }
}
+ // CraftBukkit end
public DamageSource inFire() {
return this.inFire;
@@ -254,17 +270,29 @@
@@ -254,7 +270,7 @@
}
public DamageSource explosion(@Nullable Entity source, @Nullable Entity attacker) {
- return this.source(attacker != null && source != null ? DamageTypes.PLAYER_EXPLOSION : DamageTypes.EXPLOSION, source, attacker);
+ // CraftBukkit start
+ return this.explosion(source, attacker, attacker != null && source != null ? DamageTypes.PLAYER_EXPLOSION : DamageTypes.EXPLOSION);
+ return this.source(attacker != null && source != null ? DamageTypes.PLAYER_EXPLOSION : DamageTypes.EXPLOSION, source, attacker); // Paper - revert to vanilla
}
+ public DamageSource explosion(@Nullable Entity entity, @Nullable Entity entity1, ResourceKey<DamageType> resourceKey) {
+ return this.source(resourceKey, entity, entity1);
+ // CraftBukkit end
+ }
+
public DamageSource sonicBoom(Entity attacker) {
return this.source(DamageTypes.SONIC_BOOM, attacker);
@@ -262,9 +278,15 @@
}
public DamageSource badRespawnPointExplosion(Vec3 position) {