Skip to content

Commit 79ffbf0

Browse files
committed
Add helper for working with entity attributes.
1 parent d875b9f commit 79ffbf0

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package net.darkhax.bookshelf.api.util;
2+
3+
import net.minecraft.world.entity.EquipmentSlot;
4+
import net.minecraft.world.entity.ai.attributes.Attribute;
5+
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
6+
import net.minecraft.world.entity.ai.attributes.Attributes;
7+
import net.minecraft.world.item.ItemStack;
8+
9+
import java.util.Collection;
10+
11+
/**
12+
* This class contains static helper functions for working with entity attributes and calculating attribute values.
13+
*/
14+
public final class AttributeHelper {
15+
16+
/**
17+
* Calculates the attack damage attribute value for a given ItemStack.
18+
*
19+
* @param stack The ItemStack to calculate values for.
20+
* @return The attack damage value for the ItemStack.
21+
*/
22+
public static double getAttackDamage(ItemStack stack) {
23+
24+
return calculateValue(stack, Attributes.ATTACK_DAMAGE, EquipmentSlot.MAINHAND);
25+
}
26+
27+
/**
28+
* Calculates the attack speed attribute value for a given ItemStack.
29+
*
30+
* @param stack The ItemStack to calculate values for.
31+
* @return The attack speed value for the ItemStack.
32+
*/
33+
public static double getAttackSpeed(ItemStack stack) {
34+
35+
return calculateValue(stack, Attributes.ATTACK_SPEED, EquipmentSlot.MAINHAND);
36+
}
37+
38+
/**
39+
* Calculates the attack knockback attribute value for a given ItemStack.
40+
*
41+
* @param stack The ItemStack to calculate values for.
42+
* @return The attack knockback value for the ItemStack.
43+
*/
44+
public static double getAttackKnockback(ItemStack stack) {
45+
46+
return calculateValue(stack, Attributes.ATTACK_KNOCKBACK, EquipmentSlot.MAINHAND);
47+
}
48+
49+
/**
50+
* Calculates an attribute value for an ItemStack when used in a given slot.
51+
*
52+
* @param stack The ItemStack to calculate values for.
53+
* @param attribute The attribute to calculate.
54+
* @param slot The equipment slot to calculate for.
55+
* @return The calculated attribute value.
56+
*/
57+
public static double calculateValue(ItemStack stack, Attribute attribute, EquipmentSlot slot) {
58+
59+
return calculateValue(attribute, stack.getAttributeModifiers(slot).get(attribute));
60+
}
61+
62+
/**
63+
* Calculates an attribute value using a collection of attribute modifiers.
64+
*
65+
* @param attribute The attribute to calculate a value for.
66+
* @param modifiers A collection of modifiers to process.
67+
* @return The calculated attribute value.
68+
*/
69+
public static double calculateValue(Attribute attribute, Collection<AttributeModifier> modifiers) {
70+
71+
return attribute.sanitizeValue(calculateValue(modifiers, attribute.getDefaultValue()));
72+
}
73+
74+
/**
75+
* Calculates an attribute value using a collection of attribute modifiers.
76+
*
77+
* @param modifiers A collection of modifiers to process.
78+
* @param baseValue The base value to use for the calculation.
79+
* @return The calculated attribute value.
80+
*/
81+
public static double calculateValue(Collection<AttributeModifier> modifiers, double baseValue) {
82+
83+
double baseTotal = baseValue;
84+
85+
for (AttributeModifier modifier : modifiers) {
86+
87+
if (modifier.getOperation() == AttributeModifier.Operation.ADDITION) {
88+
89+
baseTotal += modifier.getAmount();
90+
}
91+
}
92+
93+
double modifiedValue = baseTotal;
94+
95+
for (AttributeModifier modifier : modifiers) {
96+
97+
if (modifier.getOperation() == AttributeModifier.Operation.MULTIPLY_BASE) {
98+
99+
modifiedValue += baseTotal * modifier.getAmount();
100+
}
101+
}
102+
103+
for (AttributeModifier modifier : modifiers) {
104+
105+
if (modifier.getOperation() == AttributeModifier.Operation.MULTIPLY_TOTAL) {
106+
107+
modifiedValue *= 1 + modifier.getAmount();
108+
}
109+
}
110+
111+
return modifiedValue;
112+
}
113+
}

0 commit comments

Comments
 (0)