Skip to content

Commit 81e1f23

Browse files
committed
Introduce trySetSkipped and parts
1 parent ccc383b commit 81e1f23

File tree

1 file changed

+43
-0
lines changed
  • src/main/java/org/spongepowered/api/event/cause/entity/damage

1 file changed

+43
-0
lines changed

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626

2727
import org.spongepowered.api.event.Cause;
2828

29+
import java.util.EnumSet;
2930
import java.util.List;
3031
import java.util.Optional;
3132
import java.util.OptionalDouble;
33+
import java.util.Set;
3234

3335
/**
3436
* A step represent an operation made by the platform (vanilla and mods) or modifiers added by plugins.
@@ -79,6 +81,38 @@ default void skip() {
7981
this.setSkipped(true);
8082
}
8183

84+
/**
85+
* Sets whether parts of this step are skipped.
86+
* This has no effect on steps already finished.
87+
*
88+
* @see #isSkipped()
89+
*/
90+
default void trySetSkipped(Set<Part> parts, boolean skipped) {
91+
if (parts.contains(Part.CHILDREN_BEFORE) && this.damageBeforeSelf().isEmpty()) {
92+
for (DamageStep child : this.childrenBefore()) {
93+
child.trySetSkipped(Part.ALL, skipped);
94+
}
95+
}
96+
if (parts.contains(Part.SELF) && this.damageAfterSelf().isEmpty()) {
97+
this.setSkipped(skipped);
98+
}
99+
if (parts.contains(Part.CHILDREN_AFTER) && this.damageAfterChildren().isEmpty()) {
100+
for (DamageStep child : this.childrenAfter()) {
101+
child.trySetSkipped(Part.ALL, skipped);
102+
}
103+
}
104+
}
105+
106+
/**
107+
* Skips parts of this step.
108+
* This has no effect on steps already finished.
109+
*
110+
* @see #isSkipped()
111+
*/
112+
default void trySkip(Set<Part> parts) {
113+
this.trySetSkipped(parts, true);
114+
}
115+
82116
/**
83117
* The damage just before the children of this step.
84118
* Returns empty if the value is not known yet.
@@ -153,4 +187,13 @@ default DamageStep root() {
153187
* @return The list of children steps
154188
*/
155189
List<DamageStep> childrenAfter();
190+
191+
/**
192+
* The parts composing a step.
193+
*/
194+
enum Part {
195+
SELF, CHILDREN_BEFORE, CHILDREN_AFTER;
196+
197+
public static final EnumSet<Part> ALL = EnumSet.allOf(Part.class);
198+
}
156199
}

0 commit comments

Comments
 (0)