Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a damage step for each modifier, resulting in trees of steps
Browse files Browse the repository at this point in the history
Yeregorix committed Jan 27, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
tvdeyen Thomas von Deyen
1 parent 6fc2a58 commit 352f98c
Showing 3 changed files with 85 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -24,18 +24,44 @@
*/
package org.spongepowered.api.event.cause.entity.damage;

import org.spongepowered.api.Sponge;

/**
* A damage modifier that will be applied before or after a {@link DamageStep}.
*/
@FunctionalInterface
public interface DamageModifier {

/**
* Modifies the damage.
* Gets the {@link DamageStepType} of this modifier.
*
* @return the modifier type.
*/
DamageStepType type();

/**
* Gets the function that modify the damage.
*
* @param step The damage step this modifier is associated with.
* @param damage The current damage value.
* @return The next damage value
* @return the modifier function.
*/
double modify(DamageStep step, double damage);
Function function();

@FunctionalInterface
interface Function {
/**
* Modifies the damage.
*
* @param step The damage step this modifier is associated with.
* @param damage The current damage value.
* @return The next damage value
*/
double modify(DamageStep step, double damage);
}

static DamageModifier of(DamageStepType type, Function function) {
return Sponge.game().factoryProvider().provide(Factory.class).of(type, function);
}

interface Factory {
DamageModifier of(DamageStepType type, Function function);
}
}
Original file line number Diff line number Diff line change
@@ -27,30 +27,26 @@
import org.spongepowered.api.event.Cause;

import java.util.List;
import java.util.OptionalDouble;

/**
* Represents a step in the damage calculation.
* Represents a tree of steps in the damage calculation.
* A damage calculation is made of multiple trees of steps.
*/
public interface DamageStep {

/**
* Gets the {@link DamageStepType} for this {@link DamageStep}.
* Gets the {@link DamageStepType} for this step.
*
* @return The damage step type
*/
DamageStepType type();

/**
* Gets the cause of this {@link DamageStep}.
*
* @return The cause of this step
*/
Cause cause();

/**
* Gets whether this step is skipped.
* When skipped, only the step and its side effects are ignored, modifiers are still applied.
* A modifier willing to ignore every previous modifiers should revert the damage to {@link #damageBeforeModifiers()}.
* When skipped, only the step itself and its side effects are ignored, children are still applied.
* A modifier willing to ignore every previous children should revert the damage to {@link #damageBeforeChildren()},
* or call {@link #skip} on each child.
*
* @return Whether this step is skipped
*/
@@ -75,47 +71,74 @@ default void skip() {
}

/**
* The damage just before the modifiers of this step.
* The damage just before the children of this step.
* Returns empty if the value is not known yet.
*
* @return The damage before this step
* @return The damage before the children of this step
*/
double damageBeforeModifiers();
OptionalDouble damageBeforeChildren();

/**
* The damage just before this step.
* Returns empty if the value is not known yet.
*
* @return The damage before this step
* @throws IllegalStateException if called before the "before" modifiers have finished.
*/
double damageBeforeStep();
OptionalDouble damageBeforeSelf();

/**
* The damage just after this step.
* Returns empty if the value is not known yet.
*
* @return The damage after this step
* @throws IllegalStateException if called before this step has finished
*/
double damageAfterStep();
OptionalDouble damageAfterSelf();

/**
* The damage just after the modifiers of this step.
* The damage just after the children of this step.
* Returns empty if the value is not known yet.
*
* @return The damage after this step
* @throws IllegalStateException if called before the modifiers have finished.
*/
double damageAfterModifiers();
OptionalDouble damageAfterChildren();

/**
* Gets an immutable list of all modifiers that applies just before this step.
* Gets an immutable list of all children steps that applies just before this step.
*
* @return The list of modifiers
* @return The list of children steps
*/
List<DamageModifier> modifiersBefore();
List<DamageStep.Child> childrenBefore();

/**
* Gets an immutable list of all modifiers that applies just after this step.
* Gets an immutable list of all children steps that applies just after this step.
*
* @return The list of modifiers
* @return The list of children steps
*/
List<DamageStep.Child> childrenAfter();

/**
* Represents a step made by the platform (vanilla and mods).
*/
List<DamageModifier> modifiersAfter();
interface Root extends DamageStep {

/**
* Gets the {@link Cause} of this step.
*
* @return The cause of this step
*/
Cause cause();
}

/**
* Represents a step made by a plugin (a modifier).
*/
interface Child extends DamageStep, DamageModifier {

/**
* Gets the parent of this step.
*
* @return The parent of this step
*/
DamageStep parent();
}
}
Original file line number Diff line number Diff line change
@@ -117,12 +117,12 @@ interface Post extends DamageCalculationEvent {
double finalDamage();

/**
* Gets the list of the captured steps during the damage calculation in the order they have been applied.
* Gets the list of the captured root steps during the damage calculation in the order they have been applied.
* Note that this list is not an exhaustive representation of all the operations applied,
* especially in a modded environment.
*
* @return The list of steps
* @return The list of root steps
*/
List<DamageStep> steps();
List<DamageStep.Root> steps();
}
}

0 comments on commit 352f98c

Please sign in to comment.