Fix incorrect invulnerability damage reduction (#11599)

Fixes incorrect spigot handling of the invulnerability damage
reduction applied when an already invulnerable entity is damaged with a
larger damage amount than the initial damage.
Vanilla still damages entities even if invulnerable if the damage to be
applied is larger than the previous damage taken. In that case, vanilla
applies the difference between the previous damage taken and the
proposed damage.

Spigot's damage modifier API takes over the computation of damage
reducing effects, however spigot invokes this handling with the initial
damage before computing the difference to the previous damage amount.
This leads to the reduction values to generally be larger than expected,
as they are computed on the not-yet-reduced value.
Spigot applies these reductions after calling the EntityDamageEvent and
*then* subtracts the previous damage point, leading to the final damage
amount being smaller than expected.

This patch cannot simply call the EntityDamageEvent with the reduced
damage, as that would lead to EntityDamageEvent#getDamage() returning
the already reduced damage, which breaks its method contract.
Instead, this patch makes use of the DamageModifier API, implementing
the last-damage-reduction as a DamageModifier.
This commit is contained in:
Bjarne Koll 2024-11-19 11:54:58 +01:00 committed by GitHub
parent 57eab3e312
commit d0dcd7d251
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 169 additions and 14 deletions

View file

@ -43,7 +43,7 @@ ysl3000 <yannicklamprecht@live.de>
Machine_Maker <machine@machinemaker.me>
Ivan Pekov <ivan@mrivanplays.com>
Camotoy <20743703+Camotoy@users.noreply.github.com>
Bjarne Koll <lynxplay101@gmail.com>
Bjarne Koll <git@lynxplay.dev>
MeFisto94 <MeFisto94@users.noreply.github.com>
Owen1212055 <23108066+Owen1212055@users.noreply.github.com>
LemonCaramel <admin@caramel.moe>

View file

@ -6,7 +6,7 @@ Subject: [PATCH] Expand Explosions API
Add Entity as a Source capability, and add more API choices, and on Location.
Co-authored-by: Esoteric Enderman <90862990+EsotericEnderman@users.noreply.github.com>
Co-authored-by: Bjarne Koll <lynxplay101@gmail.com>
Co-authored-by: Bjarne Koll <git@lynxplay.dev>
diff --git a/src/main/java/org/bukkit/Location.java b/src/main/java/org/bukkit/Location.java
index df88bc77a3fa2506adf17eddc6300ac65774df6f..fe2e0939df61b1f59d12adf3f760f1d619bb3de3 100644

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Thu, 13 Jun 2024 22:35:05 +0200
Subject: [PATCH] Introduce registry entry and builders

View file

@ -0,0 +1,40 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <git@lynxplay.dev>
Date: Mon, 11 Nov 2024 21:35:22 +0100
Subject: [PATCH] Fix incorrect invulnerability damage reduction
Fixes incorrect spigot handling of the invulnerability damage
reduction applied when an already invulnerable entity is damaged with a
larger damage amount than the initial damage.
Vanilla still damages entities even if invulnerable if the damage to be
applied is larger than the previous damage taken. In that case, vanilla
applies the difference between the previous damage taken and the
proposed damage.
Spigot's damage modifier API takes over the computation of damage
reducing effects, however spigot invokes this handling with the initial
damage before computing the difference to the previous damage amount.
This leads to the reduction values to generally be larger than expected,
as they are computed on the not-yet-reduced value.
Spigot applies these reductions after calling the EntityDamageEvent and
*then* subtracts the previous damage point, leading to the final damage
amount being smaller than expected.
This patch cannot simply call the EntityDamageEvent with the reduced
damage, as that would lead to EntityDamageEvent#getDamage() returning
the already reduced damage, which breaks its method contract.
Instead, this patch makes use of the DamageModifier API, implementing
the last-damage-reduction as a DamageModifier.
diff --git a/src/main/java/org/bukkit/event/entity/EntityDamageEvent.java b/src/main/java/org/bukkit/event/entity/EntityDamageEvent.java
index ef5b2a0f18c1c126db0b0c4a4d2a57483680665a..73aa5dc079ecb1c38c55ae1916b12edf81b723f5 100644
--- a/src/main/java/org/bukkit/event/entity/EntityDamageEvent.java
+++ b/src/main/java/org/bukkit/event/entity/EntityDamageEvent.java
@@ -247,6 +247,7 @@ public class EntityDamageEvent extends EntityEvent implements Cancellable {
* raw {@link EntityDamageEvent#getDamage()}.
*/
BASE,
+ INVULNERABILITY_REDUCTION, // Paper - fix invulnerability reduction in EntityDamageEvent - needs to be right under BASE as its the first reduction all others are based on
/**
* This represents the damage increased by freezing status.
*/

View file

@ -6,7 +6,7 @@ Subject: [PATCH] Expand Explosions API
Add Entity as a Source capability, and add more API choices, and on Location.
Co-authored-by: Esoteric Enderman <90862990+EsotericEnderman@users.noreply.github.com>
Co-authored-by: Bjarne Koll <lynxplay101@gmail.com>
Co-authored-by: Bjarne Koll <git@lynxplay.dev>
diff --git a/src/main/java/net/minecraft/server/level/ServerLevel.java b/src/main/java/net/minecraft/server/level/ServerLevel.java
index 03161ef12960cab3b9c81c190dd808a9c5c476e1..f0fc08bcc2988277b6a5e3107e6fc5c89bb67b93 100644

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Sat, 6 Nov 2021 23:15:20 +0100
Subject: [PATCH] Fix setPatternColor on tropical fish bucket meta

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Tue, 10 Jan 2023 21:06:42 +0100
Subject: [PATCH] Correctly shrink items during EntityResurrectEvent

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Fri, 4 Aug 2023 15:53:36 +0200
Subject: [PATCH] Only erase allay memory on non-item targets

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Thu, 13 Jun 2024 11:02:36 +0200
Subject: [PATCH] Adopt MaterialRerouting

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Thu, 13 Jun 2024 17:16:01 +0200
Subject: [PATCH] Configurable damage tick when blocking with shield

View file

@ -5,7 +5,7 @@ Subject: [PATCH] Remove streams from hot code
Feature patch
Co-authored-by: Bjarne Koll <lynxplay101@gmail.com>
Co-authored-by: Bjarne Koll <git@lynxplay.dev>
Co-authored-by: Spottedleaf <Spottedleaf@users.noreply.github.com>
diff --git a/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java b/src/main/java/net/minecraft/world/entity/ai/behavior/GateBehavior.java

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Thu, 13 Jun 2024 23:45:32 +0200
Subject: [PATCH] Add registry entry and builders

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Mon, 8 Jul 2024 22:01:08 +0200
Subject: [PATCH] Only call EntityDamageEvents before actuallyHurt

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Mon, 16 Sep 2024 23:07:29 +0200
Subject: [PATCH] Remove wall-time / unused skip tick protection

View file

@ -1,5 +1,5 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <lynxplay101@gmail.com>
From: Bjarne Koll <git@lynxplay.dev>
Date: Fri, 1 Nov 2024 14:58:57 +0100
Subject: [PATCH] Correct update cursor

View file

@ -0,0 +1,115 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Bjarne Koll <git@lynxplay.dev>
Date: Mon, 11 Nov 2024 21:35:27 +0100
Subject: [PATCH] Fix incorrect invulnerability damage reduction
Fixes incorrect spigot handling of the invulnerability damage
reduction applied when an already invulnerable entity is damaged with a
larger damage amount than the initial damage.
Vanilla still damages entities even if invulnerable if the damage to be
applied is larger than the previous damage taken. In that case, vanilla
applies the difference between the previous damage taken and the
proposed damage.
Spigot's damage modifier API takes over the computation of damage
reducing effects, however spigot invokes this handling with the initial
damage before computing the difference to the previous damage amount.
This leads to the reduction values to generally be larger than expected,
as they are computed on the not-yet-reduced value.
Spigot applies these reductions after calling the EntityDamageEvent and
*then* subtracts the previous damage point, leading to the final damage
amount being smaller than expected.
This patch cannot simply call the EntityDamageEvent with the reduced
damage, as that would lead to EntityDamageEvent#getDamage() returning
the already reduced damage, which breaks its method contract.
Instead, this patch makes use of the DamageModifier API, implementing
the last-damage-reduction as a DamageModifier.
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 6a3a8f0466998409a01223bc0c16d92b96e50118..51f913a495e7fda7e0e72439c6d7cc9607bd4af8 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -1505,12 +1505,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
}
// Paper start - only call damage event when actuallyHurt will be called - move call logic down
- event = this.handleEntityDamage(source, amount);
+ event = this.handleEntityDamage(source, amount, this.lastHurt); // Paper - fix invulnerability reduction in EntityDamageEvent - pass lastDamage reduction
amount = computeAmountFromEntityDamageEvent(event);
// Paper end - only call damage event when actuallyHurt will be called - move call logic down
// CraftBukkit start
- if (!this.actuallyHurt(world, source, (float) event.getFinalDamage() - this.lastHurt, event)) {
+ if (!this.actuallyHurt(world, source, (float) event.getFinalDamage(), event)) { // Paper - fix invulnerability reduction in EntityDamageEvent - no longer subtract lastHurt, that is part of the damage event calc now
return false;
}
if (this instanceof ServerPlayer && event.getDamage() == 0 && originalAmount == 0) return false; // Paper - revert to vanilla damage - players are not affected by damage that is 0 - skip damage if the vanilla damage is 0 and was not modified by plugins in the event.
@@ -1519,7 +1519,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
flag1 = false;
} else {
// Paper start - only call damage event when actuallyHurt will be called - move call logic down
- event = this.handleEntityDamage(source, amount);
+ event = this.handleEntityDamage(source, amount, 0); // Paper - fix invulnerability reduction in EntityDamageEvent - pass lastDamage reduction (none in this branch)
amount = computeAmountFromEntityDamageEvent(event);
// Paper end - only call damage event when actuallyHurt will be called - move call logic down
// CraftBukkit start
@@ -2322,8 +2322,19 @@ public abstract class LivingEntity extends Entity implements Attackable {
}
// CraftBukkit start
- private EntityDamageEvent handleEntityDamage(final DamageSource damagesource, float f) {
+ private EntityDamageEvent handleEntityDamage(final DamageSource damagesource, float f, final float invulnerabilityRelatedLastDamage) { // Paper - fix invulnerability reduction in EntityDamageEvent
float originalDamage = f;
+ // Paper start - fix invulnerability reduction in EntityDamageEvent
+ final com.google.common.base.Function<Double, Double> invulnerabilityReductionEquation = d -> {
+ if (invulnerabilityRelatedLastDamage == 0) return 0D; // no last damage, no reduction
+ // last damage existed, this means the reduction *technically* is (new damage - last damage).
+ // If the event damage was changed to something less than invul damage, hard lock it at 0.
+ if (d < invulnerabilityRelatedLastDamage) return 0D;
+ return (double) -invulnerabilityRelatedLastDamage;
+ };
+ final float originalInvulnerabilityReduction = invulnerabilityReductionEquation.apply((double) f).floatValue();
+ f += originalInvulnerabilityReduction;
+ // Paper end - fix invulnerability reduction in EntityDamageEvent
com.google.common.base.Function<Double, Double> freezing = new com.google.common.base.Function<Double, Double>() {
@Override
@@ -2400,7 +2411,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
};
float absorptionModifier = absorption.apply((double) f).floatValue();
- return CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, freezingModifier, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, freezing, hardHat, blocking, armor, resistance, magic, absorption);
+ // Paper start - fix invulnerability reduction in EntityDamageEvent
+ return CraftEventFactory.handleLivingEntityDamageEvent(this, damagesource, originalDamage, freezingModifier, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, freezing, hardHat, blocking, armor, resistance, magic, absorption, (damageModifierDoubleMap, damageModifierFunctionMap) -> {
+ damageModifierFunctionMap.put(DamageModifier.INVULNERABILITY_REDUCTION, invulnerabilityReductionEquation);
+ damageModifierDoubleMap.put(DamageModifier.INVULNERABILITY_REDUCTION, (double) originalInvulnerabilityReduction);
+ });
+ // Paper end - fix invulnerability reduction in EntityDamageEvent
}
protected boolean actuallyHurt(ServerLevel worldserver, final DamageSource damagesource, float f, final EntityDamageEvent event) { // void -> boolean, add final
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index deba03eb37012c638e08e20cd1c98e9db190c790..e37aaf77f94b97b736cc20ef070cefdff0400188 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1218,6 +1218,11 @@ public class CraftEventFactory {
private static final Function<? super Double, Double> ZERO = Functions.constant(-0.0);
public static EntityDamageEvent handleLivingEntityDamageEvent(Entity damagee, DamageSource source, double rawDamage, double freezingModifier, double hardHatModifier, double blockingModifier, double armorModifier, double resistanceModifier, double magicModifier, double absorptionModifier, Function<Double, Double> freezing, Function<Double, Double> hardHat, Function<Double, Double> blocking, Function<Double, Double> armor, Function<Double, Double> resistance, Function<Double, Double> magic, Function<Double, Double> absorption) {
+ // Paper start - fix invulnerability reduction in EntityDamageEvent
+ return handleLivingEntityDamageEvent(damagee, source, rawDamage, freezingModifier, hardHatModifier, blockingModifier, armorModifier, resistanceModifier, magicModifier, absorptionModifier, freezing, hardHat, blocking, armor, resistance, magic, absorption, null);
+ }
+ public static EntityDamageEvent handleLivingEntityDamageEvent(Entity damagee, DamageSource source, double rawDamage, double freezingModifier, double hardHatModifier, double blockingModifier, double armorModifier, double resistanceModifier, double magicModifier, double absorptionModifier, Function<Double, Double> freezing, Function<Double, Double> hardHat, Function<Double, Double> blocking, Function<Double, Double> armor, Function<Double, Double> resistance, Function<Double, Double> magic, Function<Double, Double> absorption, java.util.function.BiConsumer<Map<DamageModifier, Double>, Map<DamageModifier, Function<? super Double, Double>>> callback) {
+ // Paper end - fix invulnerability reduction in EntityDamageEvent
Map<DamageModifier, Double> modifiers = new EnumMap<>(DamageModifier.class);
Map<DamageModifier, Function<? super Double, Double>> modifierFunctions = new EnumMap<>(DamageModifier.class);
modifiers.put(DamageModifier.BASE, rawDamage);
@@ -1242,6 +1247,7 @@ public class CraftEventFactory {
modifierFunctions.put(DamageModifier.MAGIC, magic);
modifiers.put(DamageModifier.ABSORPTION, absorptionModifier);
modifierFunctions.put(DamageModifier.ABSORPTION, absorption);
+ if (callback != null) callback.accept(modifiers, modifierFunctions); // Paper - fix invulnerability reduction in EntityDamageEvent
return CraftEventFactory.handleEntityDamageEvent(damagee, source, modifiers, modifierFunctions);
}