Skip to content

Commit 1c9b856

Browse files
committed
Add enchantment helper methods for modifying values.
The associated enchantment methods are no longer transitive public.
1 parent 6468b1c commit 1c9b856

File tree

2 files changed

+88
-5
lines changed

2 files changed

+88
-5
lines changed

src/main/java/com/provismet/CombatPlusCore/utility/CPCEnchantmentHelper.java

+85-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
import com.provismet.CombatPlusCore.enchantment.effect.CPCEnchantmentEntityEffect;
55
import com.provismet.CombatPlusCore.enchantment.loot.context.CPCLootContext;
66
import it.unimi.dsi.fastutil.objects.Object2IntMap;
7+
import net.minecraft.component.ComponentType;
8+
import net.minecraft.component.DataComponentTypes;
79
import net.minecraft.component.type.ItemEnchantmentsComponent;
810
import net.minecraft.enchantment.EnchantmentEffectContext;
911
import net.minecraft.enchantment.effect.EnchantmentEffectEntry;
12+
import net.minecraft.enchantment.effect.EnchantmentValueEffect;
13+
import net.minecraft.entity.damage.DamageSource;
1014
import net.minecraft.loot.context.LootContext;
1115
import net.minecraft.registry.entry.RegistryEntry;
1216
import net.minecraft.server.world.ServerWorld;
@@ -17,6 +21,10 @@
1721
import net.minecraft.entity.LivingEntity;
1822
import net.minecraft.item.ItemStack;
1923
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;
2028

2129
/**
2230
* Enchantment helper to apply hooks from Combat+ enchantment components.
@@ -100,14 +108,84 @@ public static void postKill (ServerWorld world, LivingEntity user, LivingEntity
100108
}, user, slot);
101109
}
102110

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+
103181
/**
104182
* Iterates over all enchantments on an item, activating the consumer for each.
105183
*
106184
* @param consumer A consumer / lambda to be called.
107185
* @param user The owner of the enchanted item.
108186
* @param slot The equipment slot the item is in.
109187
*/
110-
public static void forEachEnchantment (Consumer consumer, LivingEntity user, EquipmentSlot slot) {
188+
public static void forEachEnchantment (ContextConsumer consumer, LivingEntity user, EquipmentSlot slot) {
111189
CPCEnchantmentHelper.forEachEnchantment(consumer, user, slot, user.getEquippedStack(slot));
112190
}
113191

@@ -119,7 +197,7 @@ public static void forEachEnchantment (Consumer consumer, LivingEntity user, Equ
119197
* @param slot The equipment slot the item is in.
120198
* @param itemStack The enchanted item.
121199
*/
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) {
123201
if (itemStack.isEmpty()) return;
124202

125203
ItemEnchantmentsComponent enchantments = itemStack.getEnchantments();
@@ -133,6 +211,11 @@ public static void forEachEnchantment (Consumer consumer, LivingEntity user, Equ
133211

134212
@FunctionalInterface
135213
public interface Consumer {
214+
public void accept (RegistryEntry<Enchantment> enchantment, int level);
215+
}
216+
217+
@FunctionalInterface
218+
public interface ContextConsumer {
136219
public void accept (RegistryEntry<Enchantment> enchantment, int level, EnchantmentEffectContext context);
137220
}
138221

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
accessWidener v2 named
22

3-
transitive-accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
4-
transitive-accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lnet/minecraft/entity/Entity;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
5-
transitive-accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lnet/minecraft/entity/Entity;Lnet/minecraft/entity/damage/DamageSource;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
3+
accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
4+
accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lnet/minecraft/entity/Entity;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
5+
accessible method net/minecraft/enchantment/Enchantment modifyValue (Lnet/minecraft/component/ComponentType;Lnet/minecraft/server/world/ServerWorld;ILnet/minecraft/item/ItemStack;Lnet/minecraft/entity/Entity;Lnet/minecraft/entity/damage/DamageSource;Lorg/apache/commons/lang3/mutable/MutableFloat;)V
66

77
accessible field net/minecraft/loot/context/LootContextTypes MAP Lcom/google/common/collect/BiMap;

0 commit comments

Comments
 (0)