Skip to content

Commit 955a1f8

Browse files
committed
Add a Cause to DamageModifier and remove DamageStep subclasses
1 parent f5ba1f0 commit 955a1f8

File tree

4 files changed

+83
-51
lines changed

4 files changed

+83
-51
lines changed

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,37 @@
2525
package org.spongepowered.api.event.cause.entity.damage;
2626

2727
import org.spongepowered.api.Sponge;
28+
import org.spongepowered.api.event.Cause;
29+
30+
import java.util.Optional;
2831

2932
/**
30-
* A damage modifier that will be applied before or after a {@link DamageStep}.
33+
* A damage modifier that is or may become a {@link DamageStep}.
3134
*/
3235
public interface DamageModifier {
3336

3437
/**
3538
* Gets the {@link DamageStepType} of this modifier.
3639
*
37-
* @return the modifier type.
40+
* @return the step type
3841
*/
3942
DamageStepType type();
4043

44+
/**
45+
* Gets the {@link Cause} of this modifier.
46+
*
47+
* @return The cause
48+
*/
49+
Cause cause();
50+
4151
/**
4252
* Gets the function that modify the damage.
53+
* The function may be absent if the sole purpose of this modifier is to apply children steps,
54+
* or if the step was made by the platform, usually the root step.
4355
*
44-
* @return the modifier function.
56+
* @return the modifier function
4557
*/
46-
Function function();
58+
Optional<Function> function();
4759

4860
@FunctionalInterface
4961
interface Function {
@@ -57,11 +69,42 @@ interface Function {
5769
double modify(DamageStep step, double damage);
5870
}
5971

60-
static DamageModifier of(DamageStepType type, Function function) {
61-
return Sponge.game().factoryProvider().provide(Factory.class).of(type, function);
72+
/**
73+
* Creates a new {@link Builder} to create {@link DamageModifier}s.
74+
*
75+
* @return The new builder
76+
*/
77+
static Builder builder() {
78+
return Sponge.game().builderProvider().provide(Builder.class);
6279
}
6380

64-
interface Factory {
65-
DamageModifier of(DamageStepType type, Function function);
81+
/**
82+
* A builder to create {@link DamageModifier}s.
83+
*/
84+
interface Builder extends org.spongepowered.api.util.Builder<DamageModifier, Builder> {
85+
86+
/**
87+
* Sets the {@link DamageStepType} for this modifier.
88+
*
89+
* @param type The damage step type
90+
* @return this builder for chaining
91+
*/
92+
Builder type(DamageStepType type);
93+
94+
/**
95+
* Sets the {@link Cause} for this modifier.
96+
*
97+
* @param cause The modifier cause
98+
* @return this builder for chaining
99+
*/
100+
Builder cause(Cause cause);
101+
102+
/**
103+
* Sets the {@link Function} for this modifier.
104+
*
105+
* @param function The modifier function
106+
* @return this builder for chaining
107+
*/
108+
Builder function(Function function);
66109
}
67110
}

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

Lines changed: 26 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,16 @@
2424
*/
2525
package org.spongepowered.api.event.cause.entity.damage;
2626

27-
import org.spongepowered.api.event.Cause;
28-
2927
import java.util.List;
28+
import java.util.Optional;
3029
import java.util.OptionalDouble;
3130

3231
/**
33-
* Represents a tree of steps in the damage calculation.
32+
* A step represent an operation made by the platform (vanilla and mods) or modifiers added by plugins.
33+
* Steps are structured as trees where children modify the input or output of the parent step.
3434
* A damage calculation is made of multiple trees of steps.
3535
*/
36-
public interface DamageStep {
37-
38-
/**
39-
* Gets the {@link DamageStepType} for this step.
40-
*
41-
* @return The damage step type
42-
*/
43-
DamageStepType type();
36+
public interface DamageStep extends DamageModifier {
4437

4538
/**
4639
* Gets whether this step is skipped.
@@ -103,42 +96,38 @@ default void skip() {
10396
OptionalDouble damageAfterChildren();
10497

10598
/**
106-
* Gets an immutable list of all children steps that applies just before this step.
99+
* Gets the parent of this step.
100+
* Returns empty if this step is the root of its tree.
107101
*
108-
* @return The list of children steps
102+
* @return The parent of this step
109103
*/
110-
List<DamageStep.Child> childrenBefore();
104+
Optional<DamageStep> parent();
111105

112106
/**
113-
* Gets an immutable list of all children steps that applies just after this step.
107+
* Gets the root of this step.
114108
*
115-
* @return The list of children steps
109+
* @return The root of this step
116110
*/
117-
List<DamageStep.Child> childrenAfter();
111+
default DamageStep root() {
112+
DamageStep step = this;
113+
Optional<DamageStep> parent;
114+
while ((parent = step.parent()).isPresent()) {
115+
step = parent.get();
116+
}
117+
return step;
118+
}
118119

119120
/**
120-
* Represents a step made by the platform (vanilla and mods).
121+
* Gets an immutable list of all children steps that applies just before this step.
122+
*
123+
* @return The list of children steps
121124
*/
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-
}
125+
List<DamageStep> childrenBefore();
131126

132127
/**
133-
* Represents a step made by a plugin (a modifier).
128+
* Gets an immutable list of all children steps that applies just after this step.
129+
*
130+
* @return The list of children steps
134131
*/
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-
}
132+
List<DamageStep> childrenAfter();
144133
}

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.Root#cause()} will reside
72+
* <p>Usually, within the {@link DamageStep#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.Root#cause()} will reside
99+
* <p>Usually, within the {@link DamageStep#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.Root#cause()} will reside
128+
* <p>Usually, within the {@link DamageStep#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.Root#cause()} will reside
145+
* <p>Usually, within the {@link DamageStep#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.Root#cause()} will reside
183+
* <p>Usually, within the {@link DamageStep#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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ interface Post extends DamageCalculationEvent {
123123
*
124124
* @return The list of root steps
125125
*/
126-
List<DamageStep.Root> steps();
126+
List<DamageStep> steps();
127127
}
128128
}

0 commit comments

Comments
 (0)