| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.MapCodec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.core.Holder;
|
| 9 | import net.minecraft.core.HolderGetter;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.core.RegistryAccess;
|
| 12 | import net.minecraft.core.component.DataComponents;
|
| 13 | import net.minecraft.core.registries.Registries;
|
| 14 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 15 | import net.minecraft.tags.EnchantmentTags;
|
| 16 | import net.minecraft.util.RandomSource;
|
| 17 | import net.minecraft.util.context.ContextKey;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 20 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 21 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 22 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 23 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 24 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 25 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 26 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 27 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
|
| 28 |
|
| 29 | public class EnchantWithLevelsFunction extends LootItemConditionalFunction {
|
| 30 | public static final MapCodec<EnchantWithLevelsFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 31 | i -> commonFields(i)
|
| 32 | .and(
|
| 33 | i.group(
|
| 34 | ContextIntProviders.CODEC.fieldOf("levels").forGetter(f -> f.levels),
|
| 35 | RegistryCodecs.holderSet(Registries.ENCHANTMENT).optionalFieldOf("options").forGetter(f -> f.options),
|
| 36 | Codec.BOOL.optionalFieldOf("include_additional_cost_component", false).forGetter(f -> f.includeAdditionalCostComponent)
|
| 37 | )
|
| 38 | )
|
| 39 | .apply(i, EnchantWithLevelsFunction::new)
|
| 40 | );
|
| 41 | private final Holder<ContextIntProvider> levels;
|
| 42 | private final Optional<HolderSet<Enchantment>> options;
|
| 43 | private final boolean includeAdditionalCostComponent;
|
| 44 |
|
| 45 | private EnchantWithLevelsFunction(
|
| 46 | final Optional<Holder<LootItemCondition>> condition,
|
| 47 | final Holder<ContextIntProvider> levels,
|
| 48 | final Optional<HolderSet<Enchantment>> options,
|
| 49 | final boolean includeAdditionalCostComponent
|
| 50 | ) {
|
| 51 | super(condition);
|
| 52 | this.levels = levels;
|
| 53 | this.options = options;
|
| 54 | this.includeAdditionalCostComponent = includeAdditionalCostComponent;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public MapCodec<EnchantWithLevelsFunction> codec() {
|
| 59 | return MAP_CODEC;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 64 | return this.includeAdditionalCostComponent ? Set.of(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED) : Set.of();
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | public void validate(final ValidationContext context) {
|
| 69 | super.validate(context);
|
| 70 | Validatable.validateHolder(context, "levels", this.levels);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 75 | RandomSource random = context.getRandom();
|
| 76 | RegistryAccess registryAccess = context.getLevel().registryAccess();
|
| 77 | int enchantmentCost = this.levels.value().getInt(context);
|
| 78 | ItemStack result = EnchantmentHelper.enchantItem(random, itemStack, enchantmentCost, registryAccess, this.options);
|
| 79 | if (this.includeAdditionalCostComponent
|
| 80 | && context.hasParameter(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED)
|
| 81 | && !result.isEmpty()
|
| 82 | && enchantmentCost > 0) {
|
| 83 | result.set(DataComponents.ADDITIONAL_TRADE_COST, enchantmentCost);
|
| 84 | }
|
| 85 |
|
| 86 | return result;
|
| 87 | }
|
| 88 |
|
| 89 | public static EnchantWithLevelsFunction.Builder enchantWithLevels(final HolderGetter<Enchantment> enchantments, final Holder<ContextIntProvider> levels) {
|
| 90 | return new EnchantWithLevelsFunction.Builder(levels).withOptions(enchantments.getOrThrow(EnchantmentTags.ON_RANDOM_LOOT));
|
| 91 | }
|
| 92 |
|
| 93 | public static class Builder extends LootItemConditionalFunction.Builder<EnchantWithLevelsFunction.Builder> {
|
| 94 | private final Holder<ContextIntProvider> levels;
|
| 95 | private Optional<HolderSet<Enchantment>> options = Optional.empty();
|
| 96 | private boolean includeAdditionalCostComponent = false;
|
| 97 |
|
| 98 | public Builder(final Holder<ContextIntProvider> levels) {
|
| 99 | this.levels = levels;
|
| 100 | }
|
| 101 |
|
| 102 | protected EnchantWithLevelsFunction.Builder getThis() {
|
| 103 | return this;
|
| 104 | }
|
| 105 |
|
| 106 | public EnchantWithLevelsFunction.Builder withOptions(final HolderSet<Enchantment> tag) {
|
| 107 | this.options = Optional.of(tag);
|
| 108 | return this;
|
| 109 | }
|
| 110 |
|
| 111 | public EnchantWithLevelsFunction.Builder withOptions(final Optional<HolderSet<Enchantment>> options) {
|
| 112 | this.options = options;
|
| 113 | return this;
|
| 114 | }
|
| 115 |
|
| 116 | public EnchantWithLevelsFunction.Builder includeAdditionalCostComponent() {
|
| 117 | this.includeAdditionalCostComponent = true;
|
| 118 | return this;
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public LootItemFunction build() {
|
| 123 | return new EnchantWithLevelsFunction(this.getCondition(), this.levels, this.options, this.includeAdditionalCostComponent);
|
| 124 | }
|
| 125 | }
|
| 126 | }
|
| 127 |
|