Skip to content

Commit

Permalink
TODO unfinished attack/damage event overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Jun 3, 2024
1 parent 7a36c2e commit 83b2339
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.spongepowered.api.event.cause.entity.damage;

import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.item.ItemTypes;
Expand Down Expand Up @@ -63,6 +64,14 @@ static Builder builder() {
*/
DamageModifierType type();

/**
* Returns the damage modifier group.
* <p>Grouped modifiers calculate their damage independently from each other</p>
*
* @return The damage modifier group
*/
ResourceKey group();

/**
* Gets the cause of this {@link DamageModifier}.
*
Expand Down Expand Up @@ -92,6 +101,7 @@ final class Builder implements org.spongepowered.api.util.Builder<DamageModifier

@Nullable DamageModifierType type;
@Nullable Cause cause;
@Nullable ResourceKey group;
@Nullable ItemStackSnapshot snapshot;

Builder() {
Expand Down Expand Up @@ -121,6 +131,41 @@ public Builder type(final DamageModifierType damageModifierType) {
return this;
}

/**
* The main attack damage calculated for an {@link org.spongepowered.api.event.entity.AttackEntityEvent}
*
* @return This builder, for chaining
*/
public Builder attackDamageGroup() {
this.group = ResourceKey.minecraft("attack_damage");
return this;
}

/**
* The enchantment attack damage calculated for an {@link org.spongepowered.api.event.entity.AttackEntityEvent}
*
* @return This builder, for chaining
*/
public Builder attackEnchantmentGroup() {
this.group = ResourceKey.minecraft("attack_enchantment");
return this;
}

/**
* The damage calculated for an {@link org.spongepowered.api.event.entity.DamageEntityEvent}
*
* @return This builder, for chaining
*/
public Builder damageReductionGroup() {
this.group = ResourceKey.minecraft("damage_reduction");
return this;
}

public Builder group(final ResourceKey group) {
this.group = group;
return this;
}

public Builder item(final ItemStack itemStack) {
this.item(java.util.Objects.requireNonNull(itemStack, "ItemStack").createSnapshot());
return this;
Expand Down Expand Up @@ -179,10 +224,12 @@ private static class ImplementedDamageModifier implements DamageModifier {
private final DamageModifierType type;
private final Cause cause;
@Nullable private final ItemStackSnapshot snapshot;
private final ResourceKey group;

ImplementedDamageModifier(final Builder builder) {
this.type = java.util.Objects.requireNonNull(builder.type, "DamageType is null!");
this.cause = java.util.Objects.requireNonNull(builder.cause, "Cause is null!");
this.group = java.util.Objects.requireNonNull(builder.group, "Group is null!");
this.snapshot = builder.snapshot;
}

Expand All @@ -201,6 +248,10 @@ public Optional<ItemStackSnapshot> contributingItem() {
return Optional.ofNullable(this.snapshot);
}

public ResourceKey group() {
return group;
}

@Override
public int hashCode() {
return Objects.hash(this.type, this.cause);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
* return damage;
* }</pre></blockquote>
*
* TODO explain groups
* <p>After which, the "final" damage is simply the summation of the
* "base" damage and all "modified damage" for each {@link DamageModifier}
* provided in this event.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
* return damage;
* }</pre></blockquote>
*
* TODO explain groups
* <p>After which, the "final" damage is simply the summation of the
* "base" damage and all "modified damage" for each {@link DamageModifier}
* provided in this event.</p>
Expand All @@ -134,6 +135,7 @@
* the provided pairing will be added at the
* "end" of the list for "modifying" the "base" damage.</p>
*
* TODO this is wrong?
* <p>Note that this event is intended for processing incoming damage to
* an {@link Entity} prior to any {@link DamageModifier}s associated with
* the {@link #entity()}. The {@link DamageEntityEvent} is used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package org.spongepowered.api.event.impl.entity;

import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.event.cause.entity.damage.DamageModifier;
import org.spongepowered.api.event.cause.entity.damage.ModifierFunction;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.impl.AbstractEvent;
Expand Down Expand Up @@ -57,7 +59,7 @@ protected List<T> init(double originalValue, List<T> originalFunctions) {
for (T tuple : originalFunctions) {
this.modifierFunctions.add(this.convertTuple(tuple.modifier(), tuple.function()));
final double tempDamage = tuple.function().applyAsDouble(finalDamage);
finalDamage += tempDamage;
finalDamage += tempDamage; // TODO fix
modifierMapBuilder.add(new Tuple<>(tuple.modifier(), tempDamage));
mapBuilder.put(tuple.modifier(), tempDamage);
this.modifiers.put(tuple.modifier(), tempDamage);
Expand Down Expand Up @@ -87,16 +89,26 @@ protected void recalculateDamages(final double baseAmount) {
} else {
this.modifiers.put(entry.modifier(), modifierAmount);
}
tempAmount += modifierAmount;
tempAmount += modifierAmount; // TODO fix
}
}

protected double finalAmount(final double baseAmount) {
double damage = baseAmount;
for (final T entry : this.modifierFunctions) {
damage += entry.function().applyAsDouble(damage);
final var amounts = finalAmounts(baseAmount, this.modifierFunctions);
return amounts.values().stream().mapToDouble(Double::doubleValue).sum();
}

public static <T extends ModifierFunction<M>, M> Map<ResourceKey, Double> finalAmounts(final double baseAmount, final List<T> modifiers) {
final var defaultGroup = ResourceKey.sponge("default");
final Map<ResourceKey, Double> amounts = new HashMap<>();
for (final T entry : modifiers) {
var group = defaultGroup;
if (entry.modifier() instanceof DamageModifier damageModifier) {
group = damageModifier.group();
}
amounts.compute(group, (k, v) -> entry.function().applyAsDouble(v == null ? baseAmount : v));
}
return damage;
return amounts;
}

/**
Expand Down

0 comments on commit 83b2339

Please sign in to comment.