Skip to content

Commit f5ba1f0

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

File tree

5 files changed

+91
-47
lines changed

5 files changed

+91
-47
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/cause/entity/damage/DamageStepType.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,11 @@
2424
*/
2525
package org.spongepowered.api.event.cause.entity.damage;
2626

27-
import org.spongepowered.api.event.Cause;
2827
import org.spongepowered.api.registry.DefaultedRegistryValue;
2928
import org.spongepowered.api.util.annotation.CatalogedBy;
3029

3130
/**
32-
* A type of {@link DamageStep} that can apply a "grouping" so to speak
33-
* for the damage modifier. The use case is being able to differentiate between
34-
* various {@link DamageStep}s based on the {@link DamageStepType}
35-
* without digging through the {@link Cause} provided by
36-
* {@link DamageStep#cause()}.
31+
* Represents a type of {@link DamageStep}.
3732
*/
3833
@CatalogedBy(DamageStepTypes.class)
3934
public interface DamageStepType extends DefaultedRegistryValue {

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public final class DamageStepTypes {
6969
* the {@link EnchantmentType}s applicable to an {@link ItemStack} that is
7070
* considered to be "armor" currently equipped on the owner.
7171
*
72-
* <p>Usually, within the {@link DamageStep#cause()} will reside
72+
* <p>Usually, within the {@link DamageStep.Root#cause()} will reside
7373
* an {@link ItemStackSnapshot} and an {@link Enchantment} signifying
7474
* that the {@link EnchantmentType} of the {@link ItemStack} is modifying the
7575
* incoming/outgoing damage. There can be multiple {@link DamageStep}s
@@ -96,7 +96,7 @@ public final class DamageStepTypes {
9696
* the {@link PotionEffectTypes#RESISTANCE} or any other
9797
* {@link PotionEffectType} that can be deemed as reducing incoming damage.
9898
*
99-
* <p>Usually, within the {@link DamageStep#cause()} will reside
99+
* <p>Usually, within the {@link DamageStep.Root#cause()} will reside
100100
* a {@link PotionEffect} including the amplifier and duration, signifying
101101
* that the {@link PotionEffectType} is modifying the incoming damage.</p>
102102
*/
@@ -125,7 +125,7 @@ public final class DamageStepTypes {
125125
* Represents the {@link DamageStep} that will modify damage from
126126
* a {@link DamageSource#source()} that is a {@link org.spongepowered.api.entity.FallingBlock}.
127127
*
128-
* <p>Usually, within the {@link DamageStep#cause()} will reside
128+
* <p>Usually, within the {@link DamageStep.Root#cause()} will reside
129129
* an {@link ItemStackSnapshot} and an {@link Enchantment} signifying
130130
* that the {@link EnchantmentType} of the {@link ItemStack} is modifying the
131131
* incoming/outgoing damage.</p>
@@ -142,7 +142,7 @@ public final class DamageStepTypes {
142142
* Represents a {@link DamageStep} that will reduce outgoing damage
143143
* based on a {@link PotionEffect}.
144144
*
145-
* <p>Usually, within the {@link DamageStep#cause()} will reside
145+
* <p>Usually, within the {@link DamageStep.Root#cause()} will reside
146146
* a {@link PotionEffect} including the amplifier and duration, signifying
147147
* that the {@link PotionEffectType} is reducing the outgoing damage.</p>
148148
*/
@@ -180,7 +180,7 @@ public final class DamageStepTypes {
180180
* Represents the {@link DamageStep} that will modify damage from
181181
* an {@link EnchantmentType} on an equipped {@link ItemStack}.
182182
*
183-
* <p>Usually, within the {@link DamageStep#cause()} will reside
183+
* <p>Usually, within the {@link DamageStep.Root#cause()} will reside
184184
* an {@link ItemStackSnapshot} and an {@link Enchantment} signifying
185185
* that the {@link EnchantmentType} of the {@link ItemStack} is modifying the
186186
* incoming/outgoing damage.</p>

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)