From a7990fd2cdf625e01dd460b656628ea4d9afcedc Mon Sep 17 00:00:00 2001 From: Anselm Brehme Date: Wed, 5 Jun 2024 22:05:02 +0200 Subject: [PATCH] provide value before and after modifier (this is used in impl) --- .../api/event/entity/AttackEntityEvent.java | 6 ++-- .../api/event/entity/DamageEntityEvent.java | 6 ++-- .../entity/AbstractAttackEntityEvent.java | 8 ++--- .../entity/AbstractDamageEntityEvent.java | 8 ++--- .../impl/entity/AbstractModifierEvent.java | 14 +++++---- .../SpongeAbstractDamageEntityEventTest.java | 29 +++++++++---------- 6 files changed, 36 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/spongepowered/api/event/entity/AttackEntityEvent.java b/src/main/java/org/spongepowered/api/event/entity/AttackEntityEvent.java index bda57f0024..af39e58ab8 100644 --- a/src/main/java/org/spongepowered/api/event/entity/AttackEntityEvent.java +++ b/src/main/java/org/spongepowered/api/event/entity/AttackEntityEvent.java @@ -259,7 +259,7 @@ public interface AttackEntityEvent extends Event, Cancellable { * @return An immutable map of the original modified damages */ @PropertySettings(requiredParameter = false, generateMethods = false) - Map originalDamages(); + Map> originalDamages(); /** * Gets the original damage for the provided {@link DamageModifier}. If @@ -270,7 +270,7 @@ public interface AttackEntityEvent extends Event, Cancellable { * @param damageModifier The original damage modifier * @return The original damage change */ - double originalModifierDamage(DamageModifier damageModifier); + Tuple originalModifierDamage(DamageModifier damageModifier); /** * Gets the original {@link List} of {@link DamageModifier} to @@ -328,7 +328,7 @@ public interface AttackEntityEvent extends Event, Cancellable { * @param damageModifier The damage modifier to get the damage for * @return The modifier */ - double outputDamage(DamageModifier damageModifier); + Tuple outputDamage(DamageModifier damageModifier); /** * Sets the provided {@link Function} to be used for the given diff --git a/src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java b/src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java index 8e230024db..93c1656773 100644 --- a/src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java +++ b/src/main/java/org/spongepowered/api/event/entity/DamageEntityEvent.java @@ -179,7 +179,7 @@ public interface DamageEntityEvent extends Event, Cancellable { * @return An immutable map of the original modified damages */ @PropertySettings(requiredParameter = false, generateMethods = false) - Map originalDamages(); + Map> originalDamages(); /** * Gets the original damage for the provided {@link DamageModifier}. If @@ -190,7 +190,7 @@ public interface DamageEntityEvent extends Event, Cancellable { * @param damageModifier The original damage modifier * @return The original damage change */ - double originalModifierDamage(DamageModifier damageModifier); + Tuple originalModifierDamage(DamageModifier damageModifier); /** * Gets the original {@link List} of {@link DamageModifier} to @@ -248,7 +248,7 @@ public interface DamageEntityEvent extends Event, Cancellable { * @param damageModifier The damage modifier to get the damage for * @return The modifier */ - double damage(DamageModifier damageModifier); + Tuple damage(DamageModifier damageModifier); /** * Sets the provided {@link DamageFunction} to be used for the given diff --git a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractAttackEntityEvent.java b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractAttackEntityEvent.java index 2e2afef576..6d8531e060 100644 --- a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractAttackEntityEvent.java +++ b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractAttackEntityEvent.java @@ -55,9 +55,9 @@ protected final void init() { } @Override - public final double originalModifierDamage(final DamageModifier damageModifier) { + public final Tuple originalModifierDamage(final DamageModifier damageModifier) { Objects.requireNonNull(damageModifier, "Damage modifier cannot be null!"); - for (final Tuple tuple : this.originalModifiers) { + for (final var tuple : this.originalModifiers) { if (tuple.first().equals(damageModifier)) { return tuple.second(); } @@ -71,7 +71,7 @@ public final double originalFinalDamage() { } @Override - public final Map originalDamages() { + public final Map> originalDamages() { return this.originalModifierMap; } @@ -87,7 +87,7 @@ public final boolean isModifierApplicable(final DamageModifier damageModifier) { } @Override - public final double outputDamage(final DamageModifier damageModifier) { + public final Tuple outputDamage(final DamageModifier damageModifier) { if (!this.modifiers.containsKey(Objects.requireNonNull(damageModifier, "Damage Modifier cannot be null!"))) { throw new IllegalArgumentException("The provided damage modifier is not applicable: " + damageModifier.toString()); } diff --git a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractDamageEntityEvent.java b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractDamageEntityEvent.java index ff87ea5a05..a0f600a688 100644 --- a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractDamageEntityEvent.java +++ b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractDamageEntityEvent.java @@ -56,9 +56,9 @@ protected final void init() { } @Override - public final double originalModifierDamage(DamageModifier damageModifier) { + public final Tuple originalModifierDamage(DamageModifier damageModifier) { Objects.requireNonNull(damageModifier, "The damage modifier cannot be null!"); - for (Tuple tuple : this.originalModifiers) { + for (var tuple : this.originalModifiers) { if (tuple.first().equals(damageModifier)) { return tuple.second(); } @@ -72,7 +72,7 @@ public final double originalFinalDamage() { } @Override - public final Map originalDamages() { + public final Map > originalDamages() { return this.originalModifierMap; } @@ -88,7 +88,7 @@ public final boolean isModifierApplicable(DamageModifier damageModifier) { } @Override - public final double damage(DamageModifier damageModifier) { + public final Tuple damage(DamageModifier damageModifier) { if (!this.modifiers.containsKey(Objects.requireNonNull(damageModifier, "Damage Modifier cannot be null!"))) { throw new IllegalArgumentException("The provided damage modifier is not applicable: " + damageModifier); } diff --git a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractModifierEvent.java b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractModifierEvent.java index 4ba3702e9e..4597da900c 100644 --- a/src/main/java/org/spongepowered/api/event/impl/entity/AbstractModifierEvent.java +++ b/src/main/java/org/spongepowered/api/event/impl/entity/AbstractModifierEvent.java @@ -46,9 +46,9 @@ public abstract class AbstractModifierEvent, M> extends AbstractEvent { protected double originalFinalAmount; - protected List> originalModifiers; - protected Map originalModifierMap; - protected final LinkedHashMap modifiers = new LinkedHashMap<>(); + protected List>> originalModifiers; + protected Map> originalModifierMap; + protected final LinkedHashMap> modifiers = new LinkedHashMap<>(); protected final List modifierFunctions = new ArrayList<>(); protected List init(double baseAmount, List functions) { @@ -66,7 +66,7 @@ protected double recalculate(final double baseAmount) { return amounts.values().stream().mapToDouble(Double::doubleValue).sum(); } - private static , M> Map recalculate(final List functions, final double baseAmount, final Map into) { + private static , M> Map recalculate(final List functions, final double baseAmount, final Map> into) { into.clear(); final var defaultGroup = ResourceKey.sponge("default"); final Map amounts = new HashMap<>(); @@ -75,8 +75,10 @@ private static , M> Map recal if (func.modifier() instanceof DamageModifier damageModifier) { group = damageModifier.group(); } - final var amount = amounts.compute(group, (k, v) -> func.function().applyAsDouble(v == null ? baseAmount : v)); - into.put(func.modifier(), amount); + final var oldAmount = amounts.getOrDefault(group, baseAmount); + final var newAmount = func.function().applyAsDouble(oldAmount); + amounts.put(group, newAmount); + into.put(func.modifier(), new Tuple<>(oldAmount, newAmount)); } if (amounts.isEmpty()) { amounts.put(defaultGroup, baseAmount); diff --git a/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java b/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java index 6e3b4bbba9..94d68aa258 100644 --- a/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java +++ b/src/test/java/org/spongepowered/api/event/SpongeAbstractDamageEntityEventTest.java @@ -34,7 +34,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.function.DoubleUnaryOperator; @@ -102,15 +101,15 @@ void testUseModifiers() { Assertions.assertEquals(event.originalDamage(), originalDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalFinalDamage(), originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR); - final Map originalDamages = event.originalDamages(); + final var originalDamages = event.originalDamages(); Assertions.assertEquals(originalDamages.size(), originalFunctions.size()); - Assertions.assertEquals(originalDamages.get(firstModifer), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(originalDamages.get(secondModifier), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(originalDamages.get(firstModifer).second(), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(originalDamages.get(secondModifier).second(), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(firstModifer), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(secondModifier), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(firstModifer).second(), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(secondModifier).second(), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalFunctions(), originalFunctions); } @@ -145,11 +144,11 @@ void testSetModifiers() { event.setDamage(firstModifer, newFunction); - Assertions.assertEquals(event.damage(firstModifer), firstChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.damage(secondModifier), secondChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.damage(firstModifer).second(), firstChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.damage(secondModifier).second(), secondChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(firstModifer), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(secondModifier), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(firstModifer).second(), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(secondModifier).second(), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalDamage(), originalDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalFinalDamage(), originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR); @@ -195,12 +194,12 @@ void testAddModifier() { event.setDamage(thirdModifier, thirdFunction); - Assertions.assertEquals(event.damage(firstModifier), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.damage(secondModifier), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.damage(thirdModifier), thirdDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.damage(firstModifier).second(), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.damage(secondModifier).second(), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.damage(thirdModifier).second(), thirdDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(firstModifier), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); - Assertions.assertEquals(event.originalModifierDamage(secondModifier), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(firstModifier).second(), firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); + Assertions.assertEquals(event.originalModifierDamage(secondModifier).second(), secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalDamage(), originalDamage, SpongeAbstractDamageEntityEventTest.ERROR); Assertions.assertEquals(event.originalFinalDamage(), originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR);