|
24 | 24 | */ |
25 | 25 | package org.spongepowered.api.event.cause.entity.damage; |
26 | 26 |
|
27 | | -import org.checkerframework.checker.nullness.qual.Nullable; |
28 | | -import org.spongepowered.api.entity.Entity; |
29 | | -import org.spongepowered.api.event.Cause; |
30 | | -import org.spongepowered.api.item.ItemTypes; |
31 | | -import org.spongepowered.api.item.enchantment.Enchantment; |
32 | | -import org.spongepowered.api.item.inventory.ItemStack; |
33 | | -import org.spongepowered.api.item.inventory.ItemStackLike; |
34 | | -import org.spongepowered.api.item.inventory.ItemStackSnapshot; |
35 | | -import org.spongepowered.api.util.CopyableBuilder; |
36 | | - |
37 | | -import java.util.Objects; |
38 | | -import java.util.Optional; |
39 | | -import java.util.StringJoiner; |
40 | | -import java.util.function.DoubleUnaryOperator; |
41 | | -import java.util.function.Supplier; |
42 | | - |
43 | 27 | /** |
44 | | - * Represents a modifier that will apply a function on a damage value to deal |
45 | | - * towards an entity such that the raw damage is the input of a |
46 | | - * {@link DoubleUnaryOperator} such that the output will be the final damage |
47 | | - * applied to the {@link Entity}. |
| 28 | + * A damage modifier that will be applied before or after a {@link DamageStep}. |
48 | 29 | */ |
| 30 | +@FunctionalInterface |
49 | 31 | public interface DamageModifier { |
50 | 32 |
|
51 | 33 | /** |
52 | | - * Creates a new {@link Builder} for constructing a {@link DamageModifier}. |
53 | | - * |
54 | | - * @return A new builder |
55 | | - */ |
56 | | - static Builder builder() { |
57 | | - return new Builder(); |
58 | | - } |
59 | | - |
60 | | - /** |
61 | | - * Gets the {@link DamageModifierType} for this {@link DamageModifier}. |
| 34 | + * Modifies the damage. |
62 | 35 | * |
63 | | - * @return The damage modifier type |
| 36 | + * @param step The damage step this modifier is associated with. |
| 37 | + * @param damage The current damage value. |
| 38 | + * @return The next damage value |
64 | 39 | */ |
65 | | - DamageModifierType type(); |
66 | | - |
67 | | - /** |
68 | | - * Returns the damage modifier group. |
69 | | - * <p>Grouped modifiers calculate their damage independently from each other</p> |
70 | | - * |
71 | | - * @return The damage modifier group |
72 | | - */ |
73 | | - String group(); |
74 | | - |
75 | | - /** |
76 | | - * Gets the cause of this {@link DamageModifier}. |
77 | | - * |
78 | | - * @return The cause of this damage modifier |
79 | | - */ |
80 | | - Cause cause(); |
81 | | - |
82 | | - /** |
83 | | - * Gets the contributing {@link ItemStackSnapshot} that provided the |
84 | | - * "reason" for this {@link DamageModifier} to exist. An example of a |
85 | | - * contributing {@link ItemStack} is if an {@link ItemTypes#DIAMOND_SWORD} |
86 | | - * provided an {@link Enchantment} that provided a |
87 | | - * {@link DamageModifierTypes#WEAPON_ENCHANTMENT}, this modifier would have |
88 | | - * the {@link ItemStackSnapshot} for the weapon used. Some modifiers however, |
89 | | - * do not require an {@link ItemStack} to be the contributing factor for |
90 | | - * this modifier to exist. |
91 | | - * |
92 | | - * @return The contributing item, if available |
93 | | - */ |
94 | | - Optional<ItemStackSnapshot> contributingItem(); |
95 | | - |
96 | | - /** |
97 | | - * A builder that creates {@link DamageModifier}s, for use in both plugin and |
98 | | - * implementation requirements. |
99 | | - */ |
100 | | - final class Builder implements org.spongepowered.api.util.Builder<DamageModifier, Builder>, CopyableBuilder<DamageModifier, Builder> { |
101 | | - |
102 | | - @Nullable DamageModifierType type; |
103 | | - @Nullable Cause cause; |
104 | | - @Nullable String group; |
105 | | - @Nullable ItemStackSnapshot snapshot; |
106 | | - |
107 | | - Builder() { |
108 | | - } |
109 | | - |
110 | | - |
111 | | - /** |
112 | | - * Sets the {@link DamageModifierType} for the {@link DamageModifier} to |
113 | | - * build. |
114 | | - * |
115 | | - * @param damageModifierType The damage modifier type |
116 | | - * @return This builder, for chaining |
117 | | - */ |
118 | | - public Builder type(final Supplier<? extends DamageModifierType> damageModifierType) { |
119 | | - return this.type(damageModifierType.get()); |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * Sets the {@link DamageModifierType} for the {@link DamageModifier} to |
124 | | - * build. |
125 | | - * |
126 | | - * @param damageModifierType The damage modifier type |
127 | | - * @return This builder, for chaining |
128 | | - */ |
129 | | - public Builder type(final DamageModifierType damageModifierType) { |
130 | | - this.type = java.util.Objects.requireNonNull(damageModifierType); |
131 | | - return this; |
132 | | - } |
133 | | - |
134 | | - /** |
135 | | - * The main attack damage calculated for an {@link org.spongepowered.api.event.entity.AttackEntityEvent} |
136 | | - * |
137 | | - * @return This builder, for chaining |
138 | | - */ |
139 | | - public Builder attackDamageGroup() { |
140 | | - return this.group("minecraft:attack_damage"); |
141 | | - } |
142 | | - |
143 | | - /** |
144 | | - * The enchantment attack damage calculated for an {@link org.spongepowered.api.event.entity.AttackEntityEvent} |
145 | | - * |
146 | | - * @return This builder, for chaining |
147 | | - */ |
148 | | - public Builder attackEnchantmentGroup() { |
149 | | - return this.group("minecraft:attack_enchantment"); |
150 | | - } |
151 | | - |
152 | | - /** |
153 | | - * The damage calculated for an {@link org.spongepowered.api.event.entity.DamageEntityEvent} |
154 | | - * |
155 | | - * @return This builder, for chaining |
156 | | - */ |
157 | | - public Builder damageReductionGroup() { |
158 | | - return this.group("minecraft:damage_reduction"); |
159 | | - } |
160 | | - |
161 | | - public Builder group(final String group) { |
162 | | - this.group = group; |
163 | | - return this; |
164 | | - } |
165 | | - |
166 | | - public Builder item(final ItemStackLike item) { |
167 | | - this.snapshot = java.util.Objects.requireNonNull(item, "item").asImmutable(); |
168 | | - return this; |
169 | | - } |
170 | | - |
171 | | - /** |
172 | | - * Sets the {@link Cause} for the {@link DamageModifier} to build. |
173 | | - * |
174 | | - * @param cause The cause for the damage modifier |
175 | | - * @return This builder, for chaining |
176 | | - */ |
177 | | - public Builder cause(final Cause cause) { |
178 | | - this.cause = java.util.Objects.requireNonNull(cause); |
179 | | - return this; |
180 | | - } |
181 | | - |
182 | | - /** |
183 | | - * Creates a new {@link DamageModifier} with this builder's provided |
184 | | - * {@link Cause} and {@link DamageModifierType}. |
185 | | - * |
186 | | - * @return The newly created damage modifier |
187 | | - */ |
188 | | - @Override |
189 | | - public DamageModifier build() { |
190 | | - if (this.type == null) { |
191 | | - throw new IllegalStateException("The DamageModifierType must not be null!"); |
192 | | - } |
193 | | - if (this.cause == null) { |
194 | | - throw new IllegalStateException("The cause for the DamageModifier must not be null!"); |
195 | | - } |
196 | | - return new ImplementedDamageModifier(this); |
197 | | - } |
198 | | - |
199 | | - @Override |
200 | | - public Builder from(final DamageModifier value) { |
201 | | - this.type = value.type(); |
202 | | - this.cause = value.cause(); |
203 | | - this.snapshot = value.contributingItem().orElse(null); |
204 | | - return this; |
205 | | - } |
206 | | - |
207 | | - @Override |
208 | | - public Builder reset() { |
209 | | - this.type = null; |
210 | | - this.cause = null; |
211 | | - return this; |
212 | | - } |
213 | | - |
214 | | - |
215 | | - private static class ImplementedDamageModifier implements DamageModifier { |
216 | | - private final DamageModifierType type; |
217 | | - private final Cause cause; |
218 | | - @Nullable private final ItemStackSnapshot snapshot; |
219 | | - private final String group; |
220 | | - |
221 | | - ImplementedDamageModifier(final Builder builder) { |
222 | | - this.type = java.util.Objects.requireNonNull(builder.type, "DamageType is null!"); |
223 | | - this.cause = java.util.Objects.requireNonNull(builder.cause, "Cause is null!"); |
224 | | - this.group = java.util.Objects.requireNonNull(builder.group, "Group is null!"); |
225 | | - this.snapshot = builder.snapshot; |
226 | | - } |
227 | | - |
228 | | - @Override |
229 | | - public DamageModifierType type() { |
230 | | - return this.type; |
231 | | - } |
232 | | - |
233 | | - @Override |
234 | | - public Cause cause() { |
235 | | - return this.cause; |
236 | | - } |
237 | | - |
238 | | - @Override |
239 | | - public Optional<ItemStackSnapshot> contributingItem() { |
240 | | - return Optional.ofNullable(this.snapshot); |
241 | | - } |
242 | | - |
243 | | - @Override |
244 | | - public String group() { |
245 | | - return group; |
246 | | - } |
247 | | - |
248 | | - @Override |
249 | | - public int hashCode() { |
250 | | - return Objects.hash(this.type, this.cause); |
251 | | - } |
252 | | - |
253 | | - @Override |
254 | | - public boolean equals(final Object obj) { |
255 | | - if (this == obj) { |
256 | | - return true; |
257 | | - } |
258 | | - if (obj == null || this.getClass() != obj.getClass()) { |
259 | | - return false; |
260 | | - } |
261 | | - final ImplementedDamageModifier other = (ImplementedDamageModifier) obj; |
262 | | - return Objects.equals(this.type, other.type) |
263 | | - && Objects.equals(this.cause, other.cause) |
264 | | - && Objects.equals(this.snapshot, other.snapshot); |
265 | | - } |
266 | | - |
267 | | - @Override |
268 | | - public String toString() { |
269 | | - return new StringJoiner(", ", "DamageModifier[", "]") |
270 | | - .add("type=" + this.type) |
271 | | - .add("cause=" + this.cause) |
272 | | - .add("snapshot=" + this.snapshot) |
273 | | - .toString(); |
274 | | - } |
275 | | - } |
276 | | - |
277 | | - } |
| 40 | + double modify(DamageStep step, double damage); |
278 | 41 | } |
0 commit comments