| 1 | package net.minecraft.world.item.trading;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 6 | import java.util.Arrays;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.Optional;
|
| 9 | import net.minecraft.core.Holder;
|
| 10 | import net.minecraft.core.HolderSet;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.ItemStackTemplate;
|
| 16 | import net.minecraft.world.item.enchantment.Enchantment;
|
| 17 | import net.minecraft.world.item.enchantment.ItemEnchantments;
|
| 18 | import net.minecraft.world.level.storage.loot.LootContext;
|
| 19 | import net.minecraft.world.level.storage.loot.Validatable;
|
| 20 | import net.minecraft.world.level.storage.loot.ValidationContext;
|
| 21 | import net.minecraft.world.level.storage.loot.functions.FunctionUserBuilder;
|
| 22 | import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
|
| 23 | import net.minecraft.world.level.storage.loot.functions.LootItemFunctions;
|
| 24 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 25 | import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
|
| 26 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProvider;
|
| 27 | import net.minecraft.world.level.storage.loot.providers.number.floats.ContextFloatProviders;
|
| 28 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProvider;
|
| 29 | import net.minecraft.world.level.storage.loot.providers.number.ints.ContextIntProviders;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public class VillagerTrade implements Validatable {
|
| 33 | public static final Codec<VillagerTrade> CODEC = RecordCodecBuilder.<VillagerTrade>create(
|
| 34 | i -> i.group(
|
| 35 | TradeCost.CODEC.fieldOf("wants").forGetter(villagerTrade -> villagerTrade.wants),
|
| 36 | TradeCost.CODEC.optionalFieldOf("additional_wants").forGetter(villagerTrade -> villagerTrade.additionalWants),
|
| 37 | ItemStackTemplate.CODEC.fieldOf("gives").forGetter(villagerTrade -> villagerTrade.gives),
|
| 38 | ContextIntProviders.CODEC.optionalFieldOf("max_uses", ContextIntProviders.exactly(4)).forGetter(villagerTrade -> villagerTrade.maxUses),
|
| 39 | ContextIntProviders.CODEC.optionalFieldOf("xp", ContextIntProviders.exactly(1)).forGetter(villagerTrade -> villagerTrade.xp),
|
| 40 | ContextFloatProviders.CODEC
|
| 41 | .optionalFieldOf("reputation_discount", ContextFloatProviders.exactly(0.0F))
|
| 42 | .forGetter(villagerTrade -> villagerTrade.reputationDiscount),
|
| 43 | LootItemCondition.CODEC.optionalFieldOf("merchant_predicate").forGetter(villagerTrade -> villagerTrade.merchantPredicate),
|
| 44 | LootItemFunctions.CODEC.optionalFieldOf("given_item_modifier").forGetter(villagerTrade -> villagerTrade.givenItemModifier),
|
| 45 | RegistryCodecs.holderSet(Registries.ENCHANTMENT)
|
| 46 | .optionalFieldOf("double_trade_price_enchantments")
|
| 47 | .forGetter(villagerTrade -> villagerTrade.doubleTradePriceEnchantments)
|
| 48 | )
|
| 49 | .apply(i, VillagerTrade::new)
|
| 50 | )
|
| 51 | .validate(Validatable.validatorForContext(LootContextParamSets.VILLAGER_TRADE));
|
| 52 | private final TradeCost wants;
|
| 53 | private final Optional<TradeCost> additionalWants;
|
| 54 | private final ItemStackTemplate gives;
|
| 55 | private final Optional<Holder<LootItemCondition>> merchantPredicate;
|
| 56 | private final Optional<Holder<LootItemFunction>> givenItemModifier;
|
| 57 | private final Holder<ContextIntProvider> maxUses;
|
| 58 | private final Holder<ContextIntProvider> xp;
|
| 59 | private final Holder<ContextFloatProvider> reputationDiscount;
|
| 60 | private final Optional<HolderSet<Enchantment>> doubleTradePriceEnchantments;
|
| 61 |
|
| 62 | private VillagerTrade(
|
| 63 | final TradeCost wants,
|
| 64 | final Optional<TradeCost> additionalWants,
|
| 65 | final ItemStackTemplate gives,
|
| 66 | final Holder<ContextIntProvider> maxUses,
|
| 67 | final Holder<ContextIntProvider> xp,
|
| 68 | final Holder<ContextFloatProvider> reputationDiscount,
|
| 69 | final Optional<Holder<LootItemCondition>> merchantPredicate,
|
| 70 | final Optional<Holder<LootItemFunction>> givenItemModifier,
|
| 71 | final Optional<HolderSet<Enchantment>> doubleTradePriceEnchantments
|
| 72 | ) {
|
| 73 | this.wants = wants;
|
| 74 | this.additionalWants = additionalWants;
|
| 75 | this.gives = gives;
|
| 76 | this.maxUses = maxUses;
|
| 77 | this.xp = xp;
|
| 78 | this.reputationDiscount = reputationDiscount;
|
| 79 | this.merchantPredicate = merchantPredicate;
|
| 80 | this.givenItemModifier = givenItemModifier;
|
| 81 | this.doubleTradePriceEnchantments = doubleTradePriceEnchantments;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void validate(final ValidationContext context) {
|
| 86 | Validatable.validate(context, "wants", this.wants);
|
| 87 | Validatable.validate(context, "additional_wants", this.additionalWants);
|
| 88 | Validatable.validateHolder(context, "max_uses", this.maxUses);
|
| 89 | Validatable.validateHolder(context, "reputation_discount", this.reputationDiscount);
|
| 90 | Validatable.validateHolder(context, "xp", this.xp);
|
| 91 | Validatable.validateHolder(context, "merchant_predicate", this.merchantPredicate);
|
| 92 | Validatable.validateHolder(context, "given_item_modifier", this.givenItemModifier);
|
| 93 | }
|
| 94 |
|
| 95 | public @Nullable MerchantOffer getOffer(final LootContext lootContext) {
|
| 96 | if (this.merchantPredicate.isPresent() && !this.merchantPredicate.get().value().test(lootContext)) {
|
| 97 | return null;
|
| 98 | }
|
| 99 |
|
| 100 | ItemStack result = this.gives.create();
|
| 101 | int additionalCost = 0;
|
| 102 | if (this.givenItemModifier.isPresent()) {
|
| 103 | result = this.givenItemModifier.get().value().apply(result, lootContext);
|
| 104 | if (result.isEmpty()) {
|
| 105 | return null;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | Integer additionalTradeCost = result.remove(DataComponents.ADDITIONAL_TRADE_COST);
|
| 110 | if (additionalTradeCost != null) {
|
| 111 | additionalCost += additionalTradeCost;
|
| 112 | }
|
| 113 |
|
| 114 | if (this.doubleTradePriceEnchantments.isPresent()) {
|
| 115 | HolderSet<Enchantment> enchantments = this.doubleTradePriceEnchantments.get();
|
| 116 | ItemEnchantments itemEnchantments = result.get(DataComponents.STORED_ENCHANTMENTS);
|
| 117 | if (itemEnchantments != null && itemEnchantments.keySet().stream().anyMatch(enchantments::contains)) {
|
| 118 | additionalCost *= 2;
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | ItemCost itemCost = this.wants.toItemCost(lootContext, additionalCost);
|
| 123 | if (itemCost.count() < 1) {
|
| 124 | return null;
|
| 125 | }
|
| 126 |
|
| 127 | Optional<ItemCost> additionalItemCost = this.additionalWants.map(tradeCost -> tradeCost.toItemCost(lootContext, 0));
|
| 128 | return additionalItemCost.isPresent() && additionalItemCost.get().count() < 1
|
| 129 | ? null
|
| 130 | : new MerchantOffer(
|
| 131 | itemCost,
|
| 132 | additionalItemCost,
|
| 133 | result,
|
| 134 | Math.max(this.maxUses.value().getInt(lootContext), 1),
|
| 135 | Math.max(this.xp.value().getInt(lootContext), 0),
|
| 136 | Math.max(this.reputationDiscount.value().getFloat(lootContext), 0.0F)
|
| 137 | );
|
| 138 | }
|
| 139 |
|
| 140 | public static VillagerTrade.Builder builder(
|
| 141 | final TradeCost wants, final ItemStackTemplate gives, final int maxUses, final int xp, final float reputationDiscount
|
| 142 | ) {
|
| 143 | return new VillagerTrade.Builder(
|
| 144 | wants, gives, ContextIntProviders.exactly(maxUses), ContextIntProviders.exactly(xp), ContextFloatProviders.exactly(reputationDiscount)
|
| 145 | );
|
| 146 | }
|
| 147 |
|
| 148 | public static VillagerTrade.Builder builder(
|
| 149 | final TradeCost wants, final TradeCost additionalWants, final ItemStackTemplate gives, final int maxUses, final int xp, final float reputationDiscount
|
| 150 | ) {
|
| 151 | return new VillagerTrade.Builder(
|
| 152 | wants, gives, ContextIntProviders.exactly(maxUses), ContextIntProviders.exactly(xp), ContextFloatProviders.exactly(reputationDiscount)
|
| 153 | )
|
| 154 | .additionalWants(additionalWants);
|
| 155 | }
|
| 156 |
|
| 157 | public static class Builder {
|
| 158 | private final TradeCost wants;
|
| 159 | private final ItemStackTemplate gives;
|
| 160 | private final Holder<ContextIntProvider> maxUses;
|
| 161 | private final Holder<ContextIntProvider> xp;
|
| 162 | private final Holder<ContextFloatProvider> reputationDiscount;
|
| 163 | private Optional<TradeCost> additionalWants = Optional.empty();
|
| 164 | private Optional<Holder<LootItemCondition>> merchantPredicate = Optional.empty();
|
| 165 | private Optional<HolderSet<Enchantment>> doubleTradePriceEnchantments = Optional.empty();
|
| 166 | private final ImmutableList.Builder<Holder<LootItemFunction>> givenItemModifiers = ImmutableList.builder();
|
| 167 |
|
| 168 | public Builder(
|
| 169 | final TradeCost wants,
|
| 170 | final ItemStackTemplate gives,
|
| 171 | final Holder<ContextIntProvider> maxUses,
|
| 172 | final Holder<ContextIntProvider> xp,
|
| 173 | final Holder<ContextFloatProvider> reputationDiscount
|
| 174 | ) {
|
| 175 | this.wants = wants;
|
| 176 | this.gives = gives;
|
| 177 | this.maxUses = maxUses;
|
| 178 | this.xp = xp;
|
| 179 | this.reputationDiscount = reputationDiscount;
|
| 180 | }
|
| 181 |
|
| 182 | public VillagerTrade.Builder additionalWants(final TradeCost additionalWants) {
|
| 183 | this.additionalWants = Optional.of(additionalWants);
|
| 184 | return this;
|
| 185 | }
|
| 186 |
|
| 187 | public VillagerTrade.Builder merchantPredicate(final Holder<LootItemCondition> merchantPredicate) {
|
| 188 | this.merchantPredicate = Optional.of(merchantPredicate);
|
| 189 | return this;
|
| 190 | }
|
| 191 |
|
| 192 | public VillagerTrade.Builder addModifier(final LootItemFunction.Builder function) {
|
| 193 | this.givenItemModifiers.add(Holder.direct(function.build()));
|
| 194 | return this;
|
| 195 | }
|
| 196 |
|
| 197 | @SafeVarargs
|
| 198 | public final VillagerTrade.Builder addModifiers(final Holder<LootItemFunction>... functions) {
|
| 199 | return this.addModifiers(Arrays.asList(functions));
|
| 200 | }
|
| 201 |
|
| 202 | public VillagerTrade.Builder addModifiers(final Collection<Holder<LootItemFunction>> function) {
|
| 203 | this.givenItemModifiers.addAll(function);
|
| 204 | return this;
|
| 205 | }
|
| 206 |
|
| 207 | public VillagerTrade.Builder doubleTradePriceEnchantments(final HolderSet<Enchantment> doubleTradePriceEnchantments) {
|
| 208 | this.doubleTradePriceEnchantments = Optional.of(doubleTradePriceEnchantments);
|
| 209 | return this;
|
| 210 | }
|
| 211 |
|
| 212 | public VillagerTrade build() {
|
| 213 | return new VillagerTrade(
|
| 214 | this.wants,
|
| 215 | this.additionalWants,
|
| 216 | this.gives,
|
| 217 | this.maxUses,
|
| 218 | this.xp,
|
| 219 | this.reputationDiscount,
|
| 220 | this.merchantPredicate,
|
| 221 | FunctionUserBuilder.buildFunction(this.givenItemModifiers.build()),
|
| 222 | this.doubleTradePriceEnchantments
|
| 223 | );
|
| 224 | }
|
| 225 | }
|
| 226 | }
|
| 227 |
|