Skip to content

Commit f3d8cc5

Browse files
committed
Rework ShieldDamageReduction & ShieldItemDamageFunction to be even more future proof
1 parent 6b0b7ed commit f3d8cc5

File tree

2 files changed

+117
-80
lines changed

2 files changed

+117
-80
lines changed

src/main/java/org/spongepowered/api/data/type/ShieldDamageReduction.java

Lines changed: 67 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,62 +32,81 @@
3232

3333
import java.util.Set;
3434

35+
3536
/**
3637
* Defines the amount of damage reduced when blocking with a shield-like {@link org.spongepowered.api.item.inventory.ItemStack}.
37-
* The final amount of blocked damage will be {@code constantReduction + fractionalReduction * damage}
3838
*/
39-
public interface ShieldDamageReduction {
39+
public interface ShieldDamageReduction<T> {
4040

4141
double resolve(DamageSource source, double damage, double angle);
4242

43-
static Builder builder() {
44-
return Sponge.game().builderProvider().provide(Builder.class);
43+
static ShieldDamageReduction<MultiplyAdd> of(MultiplyAdd config) {
44+
return Sponge.game().factoryProvider().provide(Factory.class).create(config);
45+
}
46+
47+
interface Factory {
48+
49+
ShieldDamageReduction<MultiplyAdd> create(MultiplyAdd config);
50+
4551
}
4652

47-
interface Builder extends ResettableBuilder<ShieldDamageReduction, Builder> {
48-
49-
/**
50-
* Limits the {@link DamageType damage types} this reduction applies to.
51-
*
52-
* @param damageTypes the affected damage types
53-
* @return This builder, for chaining
54-
*/
55-
Builder damageTypes(Set<DamageType> damageTypes);
56-
57-
/**
58-
* Limits the {@link DamageType damage types} this reduction applies to.
59-
*
60-
* @param tag the tag defining affected damage types
61-
* @return This builder, for chaining
62-
*/
63-
Builder damageTypes(Tag<DamageType> tag);
64-
65-
/**
66-
* Sets the maximum angle between the users facing direction and the direction of the incoming attack.
67-
*
68-
* @param angle the maximum angle
69-
* @return This builder, for chaining
70-
*/
71-
Builder horizontalBlockingAngle(double angle);
72-
73-
/**
74-
* Sets the constant amount of damage to be blocked.
75-
*
76-
* @param constant a constant amount of damage to block
77-
* @return This builder, for chaining
78-
*/
79-
Builder constantReduction(double constant);
80-
81-
/**
82-
* Sets fractional amount of damage to block, where a factor of 1 means that all damage is blocked,
83-
* and a factor of 0 that no damage is blocked.
84-
*
85-
* @param fraction fractional amount of damage to block
86-
* @return This builder, for chaining
87-
*/
88-
Builder fractionalReduction(double fraction);
89-
90-
ShieldDamageReduction build();
53+
/**
54+
* The final amount of blocked damage will be {@code constantReduction + fractionalReduction * damage}
55+
*/
56+
interface MultiplyAdd {
57+
58+
double resolve(DamageSource source, double damage, double angle);
59+
60+
static Builder builder() {
61+
return Sponge.game().builderProvider().provide(Builder.class);
62+
}
63+
64+
interface Builder extends ResettableBuilder<MultiplyAdd, Builder> {
65+
66+
/**
67+
* Limits the {@link DamageType damage types} this reduction applies to.
68+
*
69+
* @param damageTypes the affected damage types
70+
* @return This builder, for chaining
71+
*/
72+
Builder damageTypes(Set<DamageType> damageTypes);
73+
74+
/**
75+
* Limits the {@link DamageType damage types} this reduction applies to.
76+
*
77+
* @param tag the tag defining affected damage types
78+
* @return This builder, for chaining
79+
*/
80+
Builder damageTypes(Tag<DamageType> tag);
81+
82+
/**
83+
* Sets the maximum angle between the users facing direction and the direction of the incoming attack.
84+
*
85+
* @param angle the maximum angle
86+
* @return This builder, for chaining
87+
*/
88+
Builder horizontalBlockingAngle(double angle);
89+
90+
/**
91+
* Sets the constant amount of damage to be blocked.
92+
*
93+
* @param constant a constant amount of damage to block
94+
* @return This builder, for chaining
95+
*/
96+
Builder constantReduction(double constant);
97+
98+
/**
99+
* Sets fractional amount of damage to block, where a factor of 1 means that all damage is blocked,
100+
* and a factor of 0 that no damage is blocked.
101+
*
102+
* @param fraction fractional amount of damage to block
103+
* @return This builder, for chaining
104+
*/
105+
Builder fractionalReduction(double fraction);
106+
107+
MultiplyAdd build();
108+
109+
}
91110

92111
}
93112

src/main/java/org/spongepowered/api/data/type/ShieldItemDamageFunction.java

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,44 +30,62 @@
3030
/**
3131
* Defines the amount of {@link org.spongepowered.api.data.Keys#ITEM_DURABILITY} damage a shield-like
3232
* {@link org.spongepowered.api.item.inventory.ItemStack} takes, when blocking an attack.
33-
* The final amount of damage will be {@code constantDamage + fractionalDamage * attackDamage}
3433
*/
35-
public interface ShieldItemDamageFunction {
34+
public interface ShieldItemDamageFunction<T> {
3635

3736
double resolve(double damage);
3837

39-
static Builder builder() {
40-
return Sponge.game().builderProvider().provide(Builder.class);
38+
static ShieldItemDamageFunction<MultiplyAdd> of(MultiplyAdd config) {
39+
return Sponge.game().factoryProvider().provide(Factory.class).create(config);
4140
}
4241

43-
interface Builder extends ResettableBuilder<ShieldItemDamageFunction, Builder> {
44-
45-
/**
46-
* Sets the minimum amount of damage blocked attack must have had, for the item to take damage at all.
47-
*
48-
* @param minDamage minimum attack damage required for any durability loss
49-
* @return This builder, for chaining
50-
*/
51-
Builder minAttackDamage(double minDamage);
52-
53-
/**
54-
* Sets the constant amount of damage taken.
55-
*
56-
* @param constantDamage a constant amount of damage to take
57-
* @return This builder, for chaining
58-
*/
59-
Builder constantDamage(double constantDamage);
60-
61-
/**
62-
* Sets fractional amount of damage to take, where a factor of 1 means that the amount of durability lost is equal to attack damage,
63-
* and a factor of 0 that no durability is lost.
64-
*
65-
* @param fractionalDamage fractional amount of damage to take
66-
* @return This builder, for chaining
67-
*/
68-
Builder fractionalDamage(double fractionalDamage);
69-
70-
ShieldItemDamageFunction build();
42+
interface Factory {
43+
44+
ShieldItemDamageFunction<MultiplyAdd> create(MultiplyAdd config);
45+
46+
}
47+
48+
/**
49+
* The final amount of damage will be {@code constantDamage + fractionalDamage * attackDamage}
50+
*/
51+
interface MultiplyAdd {
52+
53+
double resolve(double damage);
54+
55+
static Builder builder() {
56+
return Sponge.game().builderProvider().provide(Builder.class);
57+
}
58+
59+
interface Builder extends ResettableBuilder<MultiplyAdd, Builder> {
60+
61+
/**
62+
* Sets the minimum amount of damage blocked attack must have had, for the item to take damage at all.
63+
*
64+
* @param minDamage minimum attack damage required for any durability loss
65+
* @return This builder, for chaining
66+
*/
67+
Builder minAttackDamage(double minDamage);
68+
69+
/**
70+
* Sets the constant amount of damage taken.
71+
*
72+
* @param constantDamage a constant amount of damage to take
73+
* @return This builder, for chaining
74+
*/
75+
Builder constantDamage(double constantDamage);
76+
77+
/**
78+
* Sets fractional amount of damage to take, where a factor of 1 means that the amount of durability lost is equal to attack damage,
79+
* and a factor of 0 that no durability is lost.
80+
*
81+
* @param fractionalDamage fractional amount of damage to take
82+
* @return This builder, for chaining
83+
*/
84+
Builder fractionalDamage(double fractionalDamage);
85+
86+
MultiplyAdd build();
87+
88+
}
7189

7290
}
7391

0 commit comments

Comments
 (0)