Skip to content

Commit

Permalink
provide value before and after modifier (this is used in impl)
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Jun 5, 2024
1 parent 267fc93 commit a7990fd
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<DamageModifier, Double> originalDamages();
Map<DamageModifier, Tuple<Double, Double>> originalDamages();

/**
* Gets the original damage for the provided {@link DamageModifier}. If
Expand All @@ -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<Double, Double> originalModifierDamage(DamageModifier damageModifier);

/**
* Gets the original {@link List} of {@link DamageModifier} to
Expand Down Expand Up @@ -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<Double, Double> outputDamage(DamageModifier damageModifier);

/**
* Sets the provided {@link Function} to be used for the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DamageModifier, Double> originalDamages();
Map<DamageModifier, Tuple<Double, Double>> originalDamages();

/**
* Gets the original damage for the provided {@link DamageModifier}. If
Expand All @@ -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<Double, Double> originalModifierDamage(DamageModifier damageModifier);

/**
* Gets the original {@link List} of {@link DamageModifier} to
Expand Down Expand Up @@ -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<Double, Double> damage(DamageModifier damageModifier);

/**
* Sets the provided {@link DamageFunction} to be used for the given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ protected final void init() {
}

@Override
public final double originalModifierDamage(final DamageModifier damageModifier) {
public final Tuple<Double, Double> originalModifierDamage(final DamageModifier damageModifier) {
Objects.requireNonNull(damageModifier, "Damage modifier cannot be null!");
for (final Tuple<DamageModifier, Double> tuple : this.originalModifiers) {
for (final var tuple : this.originalModifiers) {
if (tuple.first().equals(damageModifier)) {
return tuple.second();
}
Expand All @@ -71,7 +71,7 @@ public final double originalFinalDamage() {
}

@Override
public final Map<DamageModifier, Double> originalDamages() {
public final Map<DamageModifier, Tuple<Double, Double>> originalDamages() {

return this.originalModifierMap;
}
Expand All @@ -87,7 +87,7 @@ public final boolean isModifierApplicable(final DamageModifier damageModifier) {
}

@Override
public final double outputDamage(final DamageModifier damageModifier) {
public final Tuple<Double, Double> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ protected final void init() {
}

@Override
public final double originalModifierDamage(DamageModifier damageModifier) {
public final Tuple<Double, Double> originalModifierDamage(DamageModifier damageModifier) {
Objects.requireNonNull(damageModifier, "The damage modifier cannot be null!");
for (Tuple<DamageModifier, Double> tuple : this.originalModifiers) {
for (var tuple : this.originalModifiers) {
if (tuple.first().equals(damageModifier)) {
return tuple.second();
}
Expand All @@ -72,7 +72,7 @@ public final double originalFinalDamage() {
}

@Override
public final Map<DamageModifier, Double> originalDamages() {
public final Map<DamageModifier, Tuple<Double, Double> > originalDamages() {

return this.originalModifierMap;
}
Expand All @@ -88,7 +88,7 @@ public final boolean isModifierApplicable(DamageModifier damageModifier) {
}

@Override
public final double damage(DamageModifier damageModifier) {
public final Tuple<Double, Double> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@
public abstract class AbstractModifierEvent<T extends ModifierFunction<M>, M> extends AbstractEvent {

protected double originalFinalAmount;
protected List<Tuple<M, Double>> originalModifiers;
protected Map<M, Double> originalModifierMap;
protected final LinkedHashMap<M, Double> modifiers = new LinkedHashMap<>();
protected List<Tuple<M, Tuple<Double, Double>>> originalModifiers;
protected Map<M, Tuple<Double, Double>> originalModifierMap;
protected final LinkedHashMap<M, Tuple<Double, Double>> modifiers = new LinkedHashMap<>();
protected final List<T> modifierFunctions = new ArrayList<>();

protected List<T> init(double baseAmount, List<T> functions) {
Expand All @@ -66,7 +66,7 @@ protected double recalculate(final double baseAmount) {
return amounts.values().stream().mapToDouble(Double::doubleValue).sum();
}

private static <T extends ModifierFunction<M>, M> Map<ResourceKey, Double> recalculate(final List<T> functions, final double baseAmount, final Map<M, Double> into) {
private static <T extends ModifierFunction<M>, M> Map<ResourceKey, Double> recalculate(final List<T> functions, final double baseAmount, final Map<M, Tuple<Double, Double>> into) {
into.clear();
final var defaultGroup = ResourceKey.sponge("default");
final Map<ResourceKey, Double> amounts = new HashMap<>();
Expand All @@ -75,8 +75,10 @@ private static <T extends ModifierFunction<M>, M> Map<ResourceKey, Double> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -102,15 +101,15 @@ void testUseModifiers() {
Assertions.assertEquals(event.originalDamage(), originalDamage, SpongeAbstractDamageEntityEventTest.ERROR);
Assertions.assertEquals(event.originalFinalDamage(), originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR);

final Map<DamageModifier, Double> 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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a7990fd

Please sign in to comment.