Skip to content

Commit 3134f48

Browse files
committed
Only allow adding modifiers
1 parent e6a3529 commit 3134f48

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

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

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public interface DamageCalculationEvent extends Event, Cancellable {
8585
interface Pre extends DamageCalculationEvent {
8686

8787
/**
88-
* Gets a mutable list of all modifiers that applies just before the step.
88+
* Gets an unmodifiable list of all modifiers that applies just before the step.
8989
*
9090
* @param type The step type
9191
* @return The list of modifiers
@@ -95,15 +95,33 @@ default List<DamageModifier> modifiersBefore(Supplier<DamageStepType> type) {
9595
}
9696

9797
/**
98-
* Gets a mutable list of all modifiers that applies just before the step.
98+
* Gets an unmodifiable list of all modifiers that applies just before the step.
9999
*
100100
* @param type The step type
101101
* @return The list of modifiers
102102
*/
103103
List<DamageModifier> modifiersBefore(DamageStepType type);
104104

105105
/**
106-
* Gets a mutable list of all modifiers that applies just after the step.
106+
* Adds a modifier that applies just before the step.
107+
*
108+
* @param type The step type
109+
* @param modifier The modifier
110+
*/
111+
default void addModifierBefore(Supplier<DamageStepType> type, DamageModifier modifier) {
112+
this.addModifierBefore(type.get(), modifier);
113+
}
114+
115+
/**
116+
* Adds a modifier that applies just before the step.
117+
*
118+
* @param type The step type
119+
* @param modifier The modifier
120+
*/
121+
void addModifierBefore(DamageStepType type, DamageModifier modifier);
122+
123+
/**
124+
* Gets an unmodifiable list of all modifiers that applies just after the step.
107125
*
108126
* @param type The step type
109127
* @return The list of modifiers
@@ -113,12 +131,30 @@ default List<DamageModifier> modifiersAfter(Supplier<DamageStepType> type) {
113131
}
114132

115133
/**
116-
* Gets a mutable list of all modifiers that applies just after the step.
134+
* Gets an unmodifiable list of all modifiers that applies just after the step.
117135
*
118136
* @param type The step type
119137
* @return The list of modifiers
120138
*/
121139
List<DamageModifier> modifiersAfter(DamageStepType type);
140+
141+
/**
142+
* Adds a modifier that applies just after the step.
143+
*
144+
* @param type The step type
145+
* @param modifier The modifier
146+
*/
147+
default void addModifierAfter(Supplier<DamageStepType> type, DamageModifier modifier) {
148+
this.addModifierAfter(type.get(), modifier);
149+
}
150+
151+
/**
152+
* Adds a modifier that applies just after the step.
153+
*
154+
* @param type The step type
155+
* @param modifier The modifier
156+
*/
157+
void addModifierAfter(DamageStepType type, DamageModifier modifier);
122158
}
123159

124160
/**

src/main/java/org/spongepowered/api/event/impl/entity/AbstractDamageCalculationEventPre.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.spongepowered.api.event.entity.DamageCalculationEvent;
3030
import org.spongepowered.api.event.impl.AbstractEvent;
3131

32+
import java.util.Collections;
3233
import java.util.HashMap;
3334
import java.util.LinkedList;
3435
import java.util.List;
@@ -38,13 +39,31 @@ public abstract class AbstractDamageCalculationEventPre extends AbstractEvent im
3839
private final Map<DamageStepType, List<DamageModifier>> modifiersBeforeMap = new HashMap<>();
3940
private final Map<DamageStepType, List<DamageModifier>> modifiersAfterMap = new HashMap<>();
4041

42+
private List<DamageModifier> getModifiersBefore(DamageStepType type) {
43+
return this.modifiersBeforeMap.computeIfAbsent(type, k -> new LinkedList<>());
44+
}
45+
46+
private List<DamageModifier> getModifiersAfter(DamageStepType type) {
47+
return this.modifiersAfterMap.computeIfAbsent(type, k -> new LinkedList<>());
48+
}
49+
4150
@Override
4251
public List<DamageModifier> modifiersBefore(DamageStepType type) {
43-
return this.modifiersBeforeMap.computeIfAbsent(type, k -> new LinkedList<>());
52+
return Collections.unmodifiableList(this.getModifiersBefore(type));
4453
}
4554

4655
@Override
4756
public List<DamageModifier> modifiersAfter(DamageStepType type) {
48-
return this.modifiersAfterMap.computeIfAbsent(type, k -> new LinkedList<>());
57+
return Collections.unmodifiableList(this.getModifiersAfter(type));
58+
}
59+
60+
@Override
61+
public void addModifierBefore(DamageStepType type, DamageModifier modifier) {
62+
this.getModifiersBefore(type).addFirst(modifier);
63+
}
64+
65+
@Override
66+
public void addModifierAfter(DamageStepType type, DamageModifier modifier) {
67+
this.getModifiersAfter(type).addLast(modifier);
4968
}
5069
}

0 commit comments

Comments
 (0)