| 1 | package net.minecraft.world.level.storage.loot.functions;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import java.util.List;
|
| 8 | import java.util.Optional;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.Function;
|
| 11 | import java.util.stream.Stream;
|
| 12 | import net.minecraft.core.Holder;
|
| 13 | import net.minecraft.core.HolderGetter;
|
| 14 | import net.minecraft.core.HolderSet;
|
| 15 | import net.minecraft.core.component.DataComponents;
|
| 16 | import net.minecraft.core.registries.Registries;
|
| 17 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 18 | import net.minecraft.tags.EnchantmentTags;
|
| 19 | import net.minecraft.util.Mth;
|
| 20 | import net.minecraft.util.RandomSource;
|
| 21 | import net.minecraft.util.Util;
|
| 22 | import net.minecraft.util.context.ContextKey;
|
| 23 | import net.minecraft.world.item.ItemStack;
|
| 24 | import net.minecraft.world.item.Items;
|
| 25 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 26 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 27 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 28 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 29 | import org.slf4j.Logger;
|
| 30 |
|
| 31 | public class EnchantRandomlyFunction extends LootItemConditionalFunction {
|
| 32 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 33 | public static final MapCodec<EnchantRandomlyFunction> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 34 | i -> commonFields(i)
|
| 35 | .and(
|
| 36 | i.group(
|
| 37 | RegistryCodecs.holderSet(Registries.ENCHANTMENT).optionalFieldOf("options").forGetter(f -> f.options),
|
| 38 | Codec.BOOL.optionalFieldOf("only_compatible", true).forGetter(f -> f.onlyCompatible),
|
| 39 | Codec.BOOL.optionalFieldOf("include_additional_cost_component", false).forGetter(f -> f.includeAdditionalCostComponent)
|
| 40 | )
|
| 41 | )
|
| 42 | .apply(i, EnchantRandomlyFunction::new)
|
| 43 | );
|
| 44 | private final Optional<HolderSet<Enchantment>> options;
|
| 45 | private final boolean onlyCompatible;
|
| 46 | private final boolean includeAdditionalCostComponent;
|
| 47 |
|
| 48 | private EnchantRandomlyFunction(
|
| 49 | final Optional<Holder<LootItemCondition>> condition,
|
| 50 | final Optional<HolderSet<Enchantment>> options,
|
| 51 | final boolean onlyCompatible,
|
| 52 | final boolean includeAdditionalCostComponent
|
| 53 | ) {
|
| 54 | super(condition);
|
| 55 | this.options = options;
|
| 56 | this.onlyCompatible = onlyCompatible;
|
| 57 | this.includeAdditionalCostComponent = includeAdditionalCostComponent;
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public MapCodec<EnchantRandomlyFunction> codec() {
|
| 62 | return MAP_CODEC;
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public Set<ContextKey<?>> getReferencedContextParams() {
|
| 67 | return this.includeAdditionalCostComponent ? Set.of(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED) : Set.of();
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public ItemStack run(final ItemStack itemStack, final LootContext context) {
|
| 72 | RandomSource random = context.getRandom();
|
| 73 | boolean targetIsBook = itemStack.is(Items.BOOK);
|
| 74 | boolean shouldCheckCompatibility = !targetIsBook && this.onlyCompatible;
|
| 75 | Stream<Holder<Enchantment>> compatibleEnchantmentsStream = this.options
|
| 76 | .map(HolderSet::stream)
|
| 77 | .orElseGet(() -> context.getLevel().registryAccess().lookupOrThrow(Registries.ENCHANTMENT).listElements().map(Function.identity()))
|
| 78 | .filter(candidate -> !shouldCheckCompatibility || candidate.value().canEnchant(itemStack));
|
| 79 | List<Holder<Enchantment>> compatibleEnchantments = compatibleEnchantmentsStream.toList();
|
| 80 | Optional<Holder<Enchantment>> enchantment = Util.getRandomSafe(compatibleEnchantments, random);
|
| 81 | if (enchantment.isEmpty()) {
|
| 82 | LOGGER.warn("Couldn't find a compatible enchantment for {}", itemStack);
|
| 83 | return itemStack;
|
| 84 | } else {
|
| 85 | return this.enchantItem(itemStack, enchantment.get(), context);
|
| 86 | }
|
| 87 | }
|
| 88 |
|
| 89 | private ItemStack enchantItem(ItemStack itemStack, final Holder<Enchantment> enchantment, final LootContext context) {
|
| 90 | RandomSource random = context.getRandom();
|
| 91 | int level = Mth.nextInt(random, enchantment.value().getMinLevel(), enchantment.value().getMaxLevel());
|
| 92 | if (itemStack.is(Items.BOOK)) {
|
| 93 | itemStack = new ItemStack(Items.ENCHANTED_BOOK);
|
| 94 | }
|
| 95 |
|
| 96 | itemStack.enchant(enchantment, level);
|
| 97 | if (this.includeAdditionalCostComponent && context.hasParameter(LootContextParams.ADDITIONAL_COST_COMPONENT_ALLOWED)) {
|
| 98 | itemStack.set(DataComponents.ADDITIONAL_TRADE_COST, 2 + random.nextInt(5 + level * 10) + 3 * level);
|
| 99 | }
|
| 100 |
|
| 101 | return itemStack;
|
| 102 | }
|
| 103 |
|
| 104 | public static EnchantRandomlyFunction.Builder randomEnchantment() {
|
| 105 | return new EnchantRandomlyFunction.Builder();
|
| 106 | }
|
| 107 |
|
| 108 | public static EnchantRandomlyFunction.Builder randomApplicableEnchantment(final HolderGetter<Enchantment> enchantments) {
|
| 109 | return randomEnchantment().withOneOf(enchantments.getOrThrow(EnchantmentTags.ON_RANDOM_LOOT));
|
| 110 | }
|
| 111 |
|
| 112 | public static class Builder extends LootItemConditionalFunction.Builder<EnchantRandomlyFunction.Builder> {
|
| 113 | private Optional<HolderSet<Enchantment>> options = Optional.empty();
|
| 114 | private boolean onlyCompatible = true;
|
| 115 | private boolean includeAdditionalCostComponent = false;
|
| 116 |
|
| 117 | protected EnchantRandomlyFunction.Builder getThis() {
|
| 118 | return this;
|
| 119 | }
|
| 120 |
|
| 121 | public EnchantRandomlyFunction.Builder withEnchantment(final Holder<Enchantment> enchantment) {
|
| 122 | this.options = Optional.of(HolderSet.direct(enchantment));
|
| 123 | return this;
|
| 124 | }
|
| 125 |
|
| 126 | public EnchantRandomlyFunction.Builder withOneOf(final HolderSet<Enchantment> enchantments) {
|
| 127 | this.options = Optional.of(enchantments);
|
| 128 | return this;
|
| 129 | }
|
| 130 |
|
| 131 | public EnchantRandomlyFunction.Builder withOptions(final HolderSet<Enchantment> enchantments) {
|
| 132 | this.options = Optional.of(enchantments);
|
| 133 | return this;
|
| 134 | }
|
| 135 |
|
| 136 | public EnchantRandomlyFunction.Builder allowingIncompatibleEnchantments() {
|
| 137 | this.onlyCompatible = false;
|
| 138 | return this;
|
| 139 | }
|
| 140 |
|
| 141 | public EnchantRandomlyFunction.Builder includeAdditionalCostComponent() {
|
| 142 | this.includeAdditionalCostComponent = true;
|
| 143 | return this;
|
| 144 | }
|
| 145 |
|
| 146 | @Override
|
| 147 | public LootItemFunction build() {
|
| 148 | return new EnchantRandomlyFunction(this.getCondition(), this.options, this.onlyCompatible, this.includeAdditionalCostComponent);
|
| 149 | }
|
| 150 | }
|
| 151 | }
|
| 152 |
|