| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableMap;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.util.Mth;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.Items;
|
| 13 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 14 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 15 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 16 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 17 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 18 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 19 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
|
| 20 |
|
| 21 | public class SetEnchantmentsFunction extends LootItemConditionalFunction {
|
| 22 | public static final MapCodec<SetEnchantmentsFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 23 | i -> commonFields(i)
|
| 24 | .and(
|
| 25 | i.group(
|
| 26 | Codec.unboundedMap(Enchantment.CODEC, ContextIntProviders.CODEC).optionalFieldOf("enchantments", Map.of()).forGetter(f -> f.enchantments),
|
| 27 | Codec.BOOL.optionalFieldOf("add", false).forGetter(f -> f.add)
|
| 28 | )
|
| 29 | )
|
| 30 | .apply(i, SetEnchantmentsFunction::new)
|
| 31 | );
|
| 32 | private final Map<Holder<Enchantment>, Holder<ContextIntProvider>> enchantments;
|
| 33 | private final boolean add;
|
| 34 |
|
| 35 | private SetEnchantmentsFunction(
|
| 36 | final Optional<Holder<LootItemCondition>> condition, final Map<Holder<Enchantment>, Holder<ContextIntProvider>> enchantments, final boolean add
|
| 37 | ) {
|
| 38 | super(condition);
|
| 39 | this.enchantments = Map.copyOf(enchantments);
|
| 40 | this.add = add;
|
| 41 | }
|
| 42 |
|
| 43 | @Override
|
| 44 | public MapCodec<SetEnchantmentsFunction> codec() {
|
| 45 | return MAP_CODEC;
|
| 46 | }
|
| 47 |
|
| 48 | @Override
|
| 49 | public void validate(final ValidationContext context) {
|
| 50 | super.validate(context);
|
| 51 | this.enchantments.forEach((enchantment, holder) -> holder.value().validate(context.forMapField("enchantments", enchantment.getRegisteredName())));
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | public ItemStack run(ItemStack itemStack, final LootContext context) {
|
| 56 | if (itemStack.is(Items.BOOK)) {
|
| 57 | itemStack = itemStack.transmuteCopy(Items.ENCHANTED_BOOK);
|
| 58 | }
|
| 59 |
|
| 60 | EnchantmentHelper.updateEnchantments(
|
| 61 | itemStack,
|
| 62 | enchantments -> {
|
| 63 | if (this.add) {
|
| 64 | this.enchantments
|
| 65 | .forEach(
|
| 66 | (enchantment, levelProvider) -> enchantments.set(
|
| 67 | (Holder<Enchantment>)enchantment, Mth.clamp(enchantments.getLevel((Holder<Enchantment>)enchantment) + levelProvider.value().getInt(context), 0, 255)
|
| 68 | )
|
| 69 | );
|
| 70 | } else {
|
| 71 | this.enchantments
|
| 72 | .forEach((enchantment, levelProvider) -> enchantments.set((Holder<Enchantment>)enchantment, Mth.clamp(levelProvider.value().getInt(context), 0, 255)));
|
| 73 | }
|
| 74 | }
|
| 75 | );
|
| 76 | return itemStack;
|
| 77 | }
|
| 78 |
|
| 79 | public static class Builder extends LootItemConditionalFunction.Builder<SetEnchantmentsFunction.Builder> {
|
| 80 | private final ImmutableMap.Builder<Holder<Enchantment>, Holder<ContextIntProvider>> enchantments = ImmutableMap.builder();
|
| 81 | private final boolean add;
|
| 82 |
|
| 83 | public Builder() {
|
| 84 | this(false);
|
| 85 | }
|
| 86 |
|
| 87 | public Builder(final boolean add) {
|
| 88 | this.add = add;
|
| 89 | }
|
| 90 |
|
| 91 | protected SetEnchantmentsFunction.Builder getThis() {
|
| 92 | return this;
|
| 93 | }
|
| 94 |
|
| 95 | public SetEnchantmentsFunction.Builder withEnchantment(final Holder<Enchantment> enchantment, final Holder<ContextIntProvider> levelProvider) {
|
| 96 | this.enchantments.put(enchantment, levelProvider);
|
| 97 | return this;
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public LootItemFunction build() {
|
| 102 | return new SetEnchantmentsFunction(this.getCondition(), this.enchantments.build(), this.add);
|
| 103 | }
|
| 104 | }
|
| 105 | }
|
| 106 |
|