4
4
import com .provismet .CombatPlusCore .enchantment .effect .CPCEnchantmentEntityEffect ;
5
5
import com .provismet .CombatPlusCore .enchantment .loot .context .CPCLootContext ;
6
6
import it .unimi .dsi .fastutil .objects .Object2IntMap ;
7
+ import net .minecraft .component .ComponentType ;
8
+ import net .minecraft .component .DataComponentTypes ;
7
9
import net .minecraft .component .type .ItemEnchantmentsComponent ;
8
10
import net .minecraft .enchantment .EnchantmentEffectContext ;
9
11
import net .minecraft .enchantment .effect .EnchantmentEffectEntry ;
12
+ import net .minecraft .enchantment .effect .EnchantmentValueEffect ;
13
+ import net .minecraft .entity .damage .DamageSource ;
10
14
import net .minecraft .loot .context .LootContext ;
11
15
import net .minecraft .registry .entry .RegistryEntry ;
12
16
import net .minecraft .server .world .ServerWorld ;
17
21
import net .minecraft .entity .LivingEntity ;
18
22
import net .minecraft .item .ItemStack ;
19
23
import net .minecraft .registry .tag .TagKey ;
24
+ import net .minecraft .util .math .random .Random ;
25
+ import org .apache .commons .lang3 .mutable .MutableFloat ;
26
+
27
+ import java .util .List ;
20
28
21
29
/**
22
30
* Enchantment helper to apply hooks from Combat+ enchantment components.
@@ -100,14 +108,84 @@ public static void postKill (ServerWorld world, LivingEntity user, LivingEntity
100
108
}, user , slot );
101
109
}
102
110
111
+ /**
112
+ * Modifies a value from a given enchantment component type.
113
+ *
114
+ * @param valueType The type of value to modify.
115
+ * @param random Random source.
116
+ * @param item The enchanted item.
117
+ * @param base The original float value.
118
+ * @return The new float value.
119
+ */
120
+ public static float modifyValue (ComponentType <EnchantmentValueEffect > valueType , Random random , ItemStack item , float base ) {
121
+ MutableFloat value = new MutableFloat (base );
122
+ CPCEnchantmentHelper .forEachEnchantment ((enchantment , level ) -> enchantment .value ().modifyValue (valueType , random , level , value ), item );
123
+ return value .floatValue ();
124
+ }
125
+
126
+ /**
127
+ * Modifies a value from a given enchantment component type.
128
+ *
129
+ * @param valueType The type of value to modify.
130
+ * @param world The server-side world.
131
+ * @param item The enchanted item.
132
+ * @param base The original float value.
133
+ * @return The new float value.
134
+ */
135
+ public static float modifyValue (ComponentType <List <EnchantmentEffectEntry <EnchantmentValueEffect >>> valueType , ServerWorld world , ItemStack item , float base ) {
136
+ MutableFloat value = new MutableFloat (base );
137
+ CPCEnchantmentHelper .forEachEnchantment ((enchantment , level ) -> enchantment .value ().modifyValue (valueType , world , level , item , value ), item );
138
+ return value .floatValue ();
139
+ }
140
+
141
+ /**
142
+ * Modifies a value from a given enchantment component type.
143
+ *
144
+ * @param valueType The type of value to modify.
145
+ * @param world The server-side world.
146
+ * @param item The enchanted item.
147
+ * @param user The owner of the item.
148
+ * @param base The original float value.
149
+ * @return The new float value.
150
+ */
151
+ public static float modifyValue (ComponentType <List <EnchantmentEffectEntry <EnchantmentValueEffect >>> valueType , ServerWorld world , ItemStack item , LivingEntity user , float base ) {
152
+ MutableFloat value = new MutableFloat (base );
153
+ CPCEnchantmentHelper .forEachEnchantment ((enchantment , level ) -> enchantment .value ().modifyValue (valueType , world , level , item , user , value ), item );
154
+ return value .floatValue ();
155
+ }
156
+
157
+ /**
158
+ * Modifies a value from a given enchantment component type.
159
+ *
160
+ * @param valueType The type of value to modify.
161
+ * @param world The server-side world.
162
+ * @param item The enchanted item.
163
+ * @param user The owner of the item.
164
+ * @param damageSource The associated damage source.
165
+ * @param base The original float value.
166
+ * @return The new float value.
167
+ */
168
+ public static float modifyValue (ComponentType <List <EnchantmentEffectEntry <EnchantmentValueEffect >>> valueType , ServerWorld world , ItemStack item , LivingEntity user , DamageSource damageSource , float base ) {
169
+ MutableFloat value = new MutableFloat (base );
170
+ CPCEnchantmentHelper .forEachEnchantment ((enchantment , level ) -> enchantment .value ().modifyValue (valueType , world , level , item , user , damageSource , value ), item );
171
+ return value .floatValue ();
172
+ }
173
+
174
+ public static void forEachEnchantment (Consumer consumer , ItemStack item ) {
175
+ ItemEnchantmentsComponent itemEnchantmentsComponent = item .getOrDefault (DataComponentTypes .ENCHANTMENTS , ItemEnchantmentsComponent .DEFAULT );
176
+ for (Object2IntMap .Entry <RegistryEntry <Enchantment >> entry : itemEnchantmentsComponent .getEnchantmentEntries ()) {
177
+ consumer .accept (entry .getKey (), entry .getIntValue ());
178
+ }
179
+ }
180
+
103
181
/**
104
182
* Iterates over all enchantments on an item, activating the consumer for each.
105
183
*
106
184
* @param consumer A consumer / lambda to be called.
107
185
* @param user The owner of the enchanted item.
108
186
* @param slot The equipment slot the item is in.
109
187
*/
110
- public static void forEachEnchantment (Consumer consumer , LivingEntity user , EquipmentSlot slot ) {
188
+ public static void forEachEnchantment (ContextConsumer consumer , LivingEntity user , EquipmentSlot slot ) {
111
189
CPCEnchantmentHelper .forEachEnchantment (consumer , user , slot , user .getEquippedStack (slot ));
112
190
}
113
191
@@ -119,7 +197,7 @@ public static void forEachEnchantment (Consumer consumer, LivingEntity user, Equ
119
197
* @param slot The equipment slot the item is in.
120
198
* @param itemStack The enchanted item.
121
199
*/
122
- public static void forEachEnchantment (Consumer consumer , LivingEntity user , EquipmentSlot slot , ItemStack itemStack ) {
200
+ public static void forEachEnchantment (ContextConsumer consumer , LivingEntity user , EquipmentSlot slot , ItemStack itemStack ) {
123
201
if (itemStack .isEmpty ()) return ;
124
202
125
203
ItemEnchantmentsComponent enchantments = itemStack .getEnchantments ();
@@ -133,6 +211,11 @@ public static void forEachEnchantment (Consumer consumer, LivingEntity user, Equ
133
211
134
212
@ FunctionalInterface
135
213
public interface Consumer {
214
+ public void accept (RegistryEntry <Enchantment > enchantment , int level );
215
+ }
216
+
217
+ @ FunctionalInterface
218
+ public interface ContextConsumer {
136
219
public void accept (RegistryEntry <Enchantment > enchantment , int level , EnchantmentEffectContext context );
137
220
}
138
221
0 commit comments