| 1 | package net.minecraft.world.item.enchantment.providers;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import net.minecraft.core.HolderSet;
|
| 6 | import net.minecraft.core.registries.Registries;
|
| 7 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 8 | import net.minecraft.util.ExtraCodecs;
|
| 9 | import net.minecraft.util.Mth;
|
| 10 | import net.minecraft.util.RandomSource;
|
| 11 | import net.minecraft.world.DifficultyInstance;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 14 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 15 | import net.minecraft.world.item.enchantment.EnchantmentInstance;
|
| 16 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 17 |
|
| 18 | public record EnchantmentsByCostWithDifficulty(HolderSet<Enchantment> enchantments, int minCost, int maxCostSpan) implements EnchantmentProvider {
|
| 19 | public static final int MAX_ALLOWED_VALUE_PART = 10000;
|
| 20 | public static final MapCodec<EnchantmentsByCostWithDifficulty> CODEC = RecordCodecBuilder.mapCodec(
|
| 21 | i -> i.group(
|
| 22 | RegistryCodecs.holderSet(Registries.ENCHANTMENT).fieldOf("enchantments").forGetter(EnchantmentsByCostWithDifficulty::enchantments),
|
| 23 | ExtraCodecs.intRange(1, 10000).fieldOf("min_cost").forGetter(EnchantmentsByCostWithDifficulty::minCost),
|
| 24 | ExtraCodecs.intRange(0, 10000).fieldOf("max_cost_span").forGetter(EnchantmentsByCostWithDifficulty::maxCostSpan)
|
| 25 | )
|
| 26 | .apply(i, EnchantmentsByCostWithDifficulty::new)
|
| 27 | );
|
| 28 |
|
| 29 | @Override
|
| 30 | public void enchant(final ItemStack item, final ItemEnchantments.Mutable itemEnchantments, final RandomSource random, final DifficultyInstance difficulty) {
|
| 31 | float difficultyModifier = difficulty.getSpecialMultiplier();
|
| 32 | int cost = Mth.randomBetweenInclusive(random, this.minCost, this.minCost + (int)(difficultyModifier * this.maxCostSpan));
|
| 33 |
|
| 34 | for (EnchantmentInstance instance : EnchantmentHelper.selectEnchantment(random, item, cost, this.enchantments.stream())) {
|
| 35 | itemEnchantments.upgrade(instance.enchantment(), instance.level());
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | public MapCodec<EnchantmentsByCostWithDifficulty> codec() {
|
| 41 | return CODEC;
|
| 42 | }
|
| 43 | }
|
| 44 |
|