Skip to content

Commit 352f98c

Browse files
committed
Add a damage step for each modifier, resulting in trees of steps
1 parent 6fc2a58 commit 352f98c

File tree

3 files changed

+85
-36
lines changed

3 files changed

+85
-36
lines changed

src/main/java/org/spongepowered/api/event/cause/entity/damage/DamageModifier.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,44 @@
2424
*/
2525
package org.spongepowered.api.event.cause.entity.damage;
2626

27+
import org.spongepowered.api.Sponge;
28+
2729
/**
2830
* A damage modifier that will be applied before or after a {@link DamageStep}.
2931
*/
30-
@FunctionalInterface
3132
public interface DamageModifier {
3233

3334
/**
34-
* Modifies the damage.
35+
* Gets the {@link DamageStepType} of this modifier.
36+
*
37+
* @return the modifier type.
38+
*/
39+
DamageStepType type();
40+
41+
/**
42+
* Gets the function that modify the damage.
3543
*
36-
* @param step The damage step this modifier is associated with.
37-
* @param damage The current damage value.
38-
* @return The next damage value
44+
* @return the modifier function.
3945
*/
40-
double modify(DamageStep step, double damage);
46+
Function function();
47+
48+
@FunctionalInterface
49+
interface Function {
50+
/**
51+
* Modifies the damage.
52+
*
53+
* @param step The damage step this modifier is associated with.
54+
* @param damage The current damage value.
55+
* @return The next damage value
56+
*/
57+
double modify(DamageStep step, double damage);
58+
}
59+
60+
static DamageModifier of(DamageStepType type, Function function) {
61+
return Sponge.game().factoryProvider().provide(Factory.class).of(type, function);
62+
}
63+
64+
interface Factory {
65+
DamageModifier of(DamageStepType type, Function function);
66+
}
4167
}

src/main/java/org/spongepowered/api/event/cause/entity/damage/DamageStep.java

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,26 @@
2727
import org.spongepowered.api.event.Cause;
2828

2929
import java.util.List;
30+
import java.util.OptionalDouble;
3031

3132
/**
32-
* Represents a step in the damage calculation.
33+
* Represents a tree of steps in the damage calculation.
34+
* A damage calculation is made of multiple trees of steps.
3335
*/
3436
public interface DamageStep {
3537

3638
/**
37-
* Gets the {@link DamageStepType} for this {@link DamageStep}.
39+
* Gets the {@link DamageStepType} for this step.
3840
*
3941
* @return The damage step type
4042
*/
4143
DamageStepType type();
4244

43-
/**
44-
* Gets the cause of this {@link DamageStep}.
45-
*
46-
* @return The cause of this step
47-
*/
48-
Cause cause();
49-
5045
/**
5146
* Gets whether this step is skipped.
52-
* When skipped, only the step and its side effects are ignored, modifiers are still applied.
53-
* A modifier willing to ignore every previous modifiers should revert the damage to {@link #damageBeforeModifiers()}.
47+
* When skipped, only the step itself and its side effects are ignored, children are still applied.
48+
* A modifier willing to ignore every previous children should revert the damage to {@link #damageBeforeChildren()},
49+
* or call {@link #skip} on each child.
5450
*
5551
* @return Whether this step is skipped
5652
*/
@@ -75,47 +71,74 @@ default void skip() {
7571
}
7672

7773
/**
78-
* The damage just before the modifiers of this step.
74+
* The damage just before the children of this step.
75+
* Returns empty if the value is not known yet.
7976
*
80-
* @return The damage before this step
77+
* @return The damage before the children of this step
8178
*/
82-
double damageBeforeModifiers();
79+
OptionalDouble damageBeforeChildren();
8380

8481
/**
8582
* The damage just before this step.
83+
* Returns empty if the value is not known yet.
8684
*
8785
* @return The damage before this step
88-
* @throws IllegalStateException if called before the "before" modifiers have finished.
8986
*/
90-
double damageBeforeStep();
87+
OptionalDouble damageBeforeSelf();
9188

9289
/**
9390
* The damage just after this step.
91+
* Returns empty if the value is not known yet.
9492
*
9593
* @return The damage after this step
96-
* @throws IllegalStateException if called before this step has finished
9794
*/
98-
double damageAfterStep();
95+
OptionalDouble damageAfterSelf();
9996

10097
/**
101-
* The damage just after the modifiers of this step.
98+
* The damage just after the children of this step.
99+
* Returns empty if the value is not known yet.
102100
*
103101
* @return The damage after this step
104-
* @throws IllegalStateException if called before the modifiers have finished.
105102
*/
106-
double damageAfterModifiers();
103+
OptionalDouble damageAfterChildren();
107104

108105
/**
109-
* Gets an immutable list of all modifiers that applies just before this step.
106+
* Gets an immutable list of all children steps that applies just before this step.
110107
*
111-
* @return The list of modifiers
108+
* @return The list of children steps
112109
*/
113-
List<DamageModifier> modifiersBefore();
110+
List<DamageStep.Child> childrenBefore();
114111

115112
/**
116-
* Gets an immutable list of all modifiers that applies just after this step.
113+
* Gets an immutable list of all children steps that applies just after this step.
117114
*
118-
* @return The list of modifiers
115+
* @return The list of children steps
116+
*/
117+
List<DamageStep.Child> childrenAfter();
118+
119+
/**
120+
* Represents a step made by the platform (vanilla and mods).
119121
*/
120-
List<DamageModifier> modifiersAfter();
122+
interface Root extends DamageStep {
123+
124+
/**
125+
* Gets the {@link Cause} of this step.
126+
*
127+
* @return The cause of this step
128+
*/
129+
Cause cause();
130+
}
131+
132+
/**
133+
* Represents a step made by a plugin (a modifier).
134+
*/
135+
interface Child extends DamageStep, DamageModifier {
136+
137+
/**
138+
* Gets the parent of this step.
139+
*
140+
* @return The parent of this step
141+
*/
142+
DamageStep parent();
143+
}
121144
}

src/main/java/org/spongepowered/api/event/entity/DamageCalculationEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,12 @@ interface Post extends DamageCalculationEvent {
117117
double finalDamage();
118118

119119
/**
120-
* Gets the list of the captured steps during the damage calculation in the order they have been applied.
120+
* Gets the list of the captured root steps during the damage calculation in the order they have been applied.
121121
* Note that this list is not an exhaustive representation of all the operations applied,
122122
* especially in a modded environment.
123123
*
124-
* @return The list of steps
124+
* @return The list of root steps
125125
*/
126-
List<DamageStep> steps();
126+
List<DamageStep.Root> steps();
127127
}
128128
}

0 commit comments

Comments
 (0)