| 1 | package net.minecraft.world.item.trading;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.item.ItemStack;
|
| 10 |
|
| 11 | public class MerchantOffer {
|
| 12 | public static final Codec<MerchantOffer> CODEC = RecordCodecBuilder.create(
|
| 13 | i -> i.group(
|
| 14 | ItemCost.CODEC.fieldOf("buy").forGetter(o -> o.baseCostA),
|
| 15 | ItemCost.CODEC.lenientOptionalFieldOf("buyB").forGetter(o -> o.costB),
|
| 16 | ItemStack.CODEC.fieldOf("sell").forGetter(o -> o.result),
|
| 17 | Codec.INT.lenientOptionalFieldOf("uses", 0).forGetter(o -> o.uses),
|
| 18 | Codec.INT.lenientOptionalFieldOf("maxUses", 4).forGetter(o -> o.maxUses),
|
| 19 | Codec.BOOL.lenientOptionalFieldOf("rewardExp", true).forGetter(o -> o.rewardExp),
|
| 20 | Codec.INT.lenientOptionalFieldOf("specialPrice", 0).forGetter(o -> o.specialPriceDiff),
|
| 21 | Codec.INT.lenientOptionalFieldOf("demand", 0).forGetter(o -> o.demand),
|
| 22 | Codec.FLOAT.lenientOptionalFieldOf("priceMultiplier", 0.0F).forGetter(o -> o.priceMultiplier),
|
| 23 | Codec.INT.lenientOptionalFieldOf("xp", 1).forGetter(o -> o.xp)
|
| 24 | )
|
| 25 | .apply(i, MerchantOffer::new)
|
| 26 | );
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, MerchantOffer> STREAM_CODEC = StreamCodec.of(
|
| 28 | MerchantOffer::writeToStream, MerchantOffer::createFromStream
|
| 29 | );
|
| 30 | private final ItemCost baseCostA;
|
| 31 | private final Optional<ItemCost> costB;
|
| 32 | private final ItemStack result;
|
| 33 | private int uses;
|
| 34 | private final int maxUses;
|
| 35 | private final boolean rewardExp;
|
| 36 | private int specialPriceDiff;
|
| 37 | private int demand;
|
| 38 | private final float priceMultiplier;
|
| 39 | private final int xp;
|
| 40 |
|
| 41 | private MerchantOffer(
|
| 42 | final ItemCost baseCostA,
|
| 43 | final Optional<ItemCost> costB,
|
| 44 | final ItemStack result,
|
| 45 | final int uses,
|
| 46 | final int maxUses,
|
| 47 | final boolean rewardExp,
|
| 48 | final int specialPriceDiff,
|
| 49 | final int demand,
|
| 50 | final float priceMultiplier,
|
| 51 | final int xp
|
| 52 | ) {
|
| 53 | this.baseCostA = baseCostA;
|
| 54 | this.costB = costB;
|
| 55 | this.result = result;
|
| 56 | this.uses = uses;
|
| 57 | this.maxUses = maxUses;
|
| 58 | this.rewardExp = rewardExp;
|
| 59 | this.specialPriceDiff = specialPriceDiff;
|
| 60 | this.demand = demand;
|
| 61 | this.priceMultiplier = priceMultiplier;
|
| 62 | this.xp = xp;
|
| 63 | }
|
| 64 |
|
| 65 | public MerchantOffer(final ItemCost buy, final ItemStack result, final int maxUses, final int xp, final float priceMultiplier) {
|
| 66 | this(buy, Optional.empty(), result, maxUses, xp, priceMultiplier);
|
| 67 | }
|
| 68 |
|
| 69 | public MerchantOffer(
|
| 70 | final ItemCost baseCostA, final Optional<ItemCost> costB, final ItemStack result, final int maxUses, final int xp, final float priceMultiplier
|
| 71 | ) {
|
| 72 | this(baseCostA, costB, result, 0, maxUses, xp, priceMultiplier);
|
| 73 | }
|
| 74 |
|
| 75 | public MerchantOffer(
|
| 76 | final ItemCost baseCostA,
|
| 77 | final Optional<ItemCost> costB,
|
| 78 | final ItemStack result,
|
| 79 | final int uses,
|
| 80 | final int maxUses,
|
| 81 | final int xp,
|
| 82 | final float priceMultiplier
|
| 83 | ) {
|
| 84 | this(baseCostA, costB, result, uses, maxUses, xp, priceMultiplier, 0);
|
| 85 | }
|
| 86 |
|
| 87 | public MerchantOffer(
|
| 88 | final ItemCost baseCostA,
|
| 89 | final Optional<ItemCost> costB,
|
| 90 | final ItemStack result,
|
| 91 | final int uses,
|
| 92 | final int maxUses,
|
| 93 | final int xp,
|
| 94 | final float priceMultiplier,
|
| 95 | final int demand
|
| 96 | ) {
|
| 97 | this(baseCostA, costB, result, uses, maxUses, true, 0, demand, priceMultiplier, xp);
|
| 98 | }
|
| 99 |
|
| 100 | private MerchantOffer(final MerchantOffer offer) {
|
| 101 | this(
|
| 102 | offer.baseCostA,
|
| 103 | offer.costB,
|
| 104 | offer.result.copy(),
|
| 105 | offer.uses,
|
| 106 | offer.maxUses,
|
| 107 | offer.rewardExp,
|
| 108 | offer.specialPriceDiff,
|
| 109 | offer.demand,
|
| 110 | offer.priceMultiplier,
|
| 111 | offer.xp
|
| 112 | );
|
| 113 | }
|
| 114 |
|
| 115 | public ItemStack getBaseCostA() {
|
| 116 | return this.baseCostA.itemStack();
|
| 117 | }
|
| 118 |
|
| 119 | public ItemStack getCostA() {
|
| 120 | return this.baseCostA.itemStack().copyWithCount(this.getModifiedCostCount(this.baseCostA));
|
| 121 | }
|
| 122 |
|
| 123 | private int getModifiedCostCount(final ItemCost cost) {
|
| 124 | int basePrice = cost.count();
|
| 125 | int demandDiff = Math.max(0, Mth.floor(basePrice * this.demand * this.priceMultiplier));
|
| 126 | return Mth.clamp(basePrice + demandDiff + this.specialPriceDiff, 1, cost.itemStack().getMaxStackSize());
|
| 127 | }
|
| 128 |
|
| 129 | public ItemStack getCostB() {
|
| 130 | return this.costB.map(ItemCost::itemStack).orElse(ItemStack.EMPTY);
|
| 131 | }
|
| 132 |
|
| 133 | public ItemCost getItemCostA() {
|
| 134 | return this.baseCostA;
|
| 135 | }
|
| 136 |
|
| 137 | public Optional<ItemCost> getItemCostB() {
|
| 138 | return this.costB;
|
| 139 | }
|
| 140 |
|
| 141 | public ItemStack getResult() {
|
| 142 | return this.result;
|
| 143 | }
|
| 144 |
|
| 145 | public void updateDemand() {
|
| 146 | this.demand = this.demand + this.uses - (this.maxUses - this.uses);
|
| 147 | }
|
| 148 |
|
| 149 | public ItemStack assemble() {
|
| 150 | return this.result.copy();
|
| 151 | }
|
| 152 |
|
| 153 | public int getUses() {
|
| 154 | return this.uses;
|
| 155 | }
|
| 156 |
|
| 157 | public void resetUses() {
|
| 158 | this.uses = 0;
|
| 159 | }
|
| 160 |
|
| 161 | public int getMaxUses() {
|
| 162 | return this.maxUses;
|
| 163 | }
|
| 164 |
|
| 165 | public void increaseUses() {
|
| 166 | this.uses++;
|
| 167 | }
|
| 168 |
|
| 169 | public int getDemand() {
|
| 170 | return this.demand;
|
| 171 | }
|
| 172 |
|
| 173 | public void addToSpecialPriceDiff(final int add) {
|
| 174 | this.specialPriceDiff += add;
|
| 175 | }
|
| 176 |
|
| 177 | public void resetSpecialPriceDiff() {
|
| 178 | this.specialPriceDiff = 0;
|
| 179 | }
|
| 180 |
|
| 181 | public int getSpecialPriceDiff() {
|
| 182 | return this.specialPriceDiff;
|
| 183 | }
|
| 184 |
|
| 185 | public void setSpecialPriceDiff(final int value) {
|
| 186 | this.specialPriceDiff = value;
|
| 187 | }
|
| 188 |
|
| 189 | public float getPriceMultiplier() {
|
| 190 | return this.priceMultiplier;
|
| 191 | }
|
| 192 |
|
| 193 | public int getXp() {
|
| 194 | return this.xp;
|
| 195 | }
|
| 196 |
|
| 197 | public boolean isOutOfStock() {
|
| 198 | return this.uses >= this.maxUses;
|
| 199 | }
|
| 200 |
|
| 201 | public void setToOutOfStock() {
|
| 202 | this.uses = this.maxUses;
|
| 203 | }
|
| 204 |
|
| 205 | public boolean needsRestock() {
|
| 206 | return this.uses > 0;
|
| 207 | }
|
| 208 |
|
| 209 | public boolean shouldRewardExp() {
|
| 210 | return this.rewardExp;
|
| 211 | }
|
| 212 |
|
| 213 | public boolean satisfiedBy(final ItemStack buyA, final ItemStack buyB) {
|
| 214 | if (!this.baseCostA.test(buyA) || buyA.getCount() < this.getModifiedCostCount(this.baseCostA)) {
|
| 215 | return false;
|
| 216 | } else {
|
| 217 | return !this.costB.isPresent() ? buyB.isEmpty() : this.costB.get().test(buyB) && buyB.getCount() >= this.costB.get().count();
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | public boolean take(final ItemStack buyA, final ItemStack buyB) {
|
| 222 | if (!this.satisfiedBy(buyA, buyB)) {
|
| 223 | return false;
|
| 224 | }
|
| 225 |
|
| 226 | buyA.shrink(this.getCostA().getCount());
|
| 227 | if (!this.getCostB().isEmpty()) {
|
| 228 | buyB.shrink(this.getCostB().getCount());
|
| 229 | }
|
| 230 |
|
| 231 | return true;
|
| 232 | }
|
| 233 |
|
| 234 | public MerchantOffer copy() {
|
| 235 | return new MerchantOffer(this);
|
| 236 | }
|
| 237 |
|
| 238 | private static void writeToStream(final RegistryFriendlyByteBuf output, final MerchantOffer offer) {
|
| 239 | ItemCost.STREAM_CODEC.encode(output, offer.getItemCostA());
|
| 240 | ItemStack.STREAM_CODEC.encode(output, offer.getResult());
|
| 241 | ItemCost.OPTIONAL_STREAM_CODEC.encode(output, offer.getItemCostB());
|
| 242 | output.writeBoolean(offer.isOutOfStock());
|
| 243 | output.writeInt(offer.getUses());
|
| 244 | output.writeInt(offer.getMaxUses());
|
| 245 | output.writeInt(offer.getXp());
|
| 246 | output.writeInt(offer.getSpecialPriceDiff());
|
| 247 | output.writeFloat(offer.getPriceMultiplier());
|
| 248 | output.writeInt(offer.getDemand());
|
| 249 | }
|
| 250 |
|
| 251 | public static MerchantOffer createFromStream(final RegistryFriendlyByteBuf input) {
|
| 252 | ItemCost buy = ItemCost.STREAM_CODEC.decode(input);
|
| 253 | ItemStack sell = ItemStack.STREAM_CODEC.decode(input);
|
| 254 | Optional<ItemCost> buyB = ItemCost.OPTIONAL_STREAM_CODEC.decode(input);
|
| 255 | boolean isExhausted = input.readBoolean();
|
| 256 | int uses = input.readInt();
|
| 257 | int maxUses = input.readInt();
|
| 258 | int xp = input.readInt();
|
| 259 | int specialPriceDiff = input.readInt();
|
| 260 | float priceMultiplier = input.readFloat();
|
| 261 | int demand = input.readInt();
|
| 262 | MerchantOffer offer = new MerchantOffer(buy, buyB, sell, uses, maxUses, xp, priceMultiplier, demand);
|
| 263 | if (isExhausted) {
|
| 264 | offer.setToOutOfStock();
|
| 265 | }
|
| 266 |
|
| 267 | offer.setSpecialPriceDiff(specialPriceDiff);
|
| 268 | return offer;
|
| 269 | }
|
| 270 | }
|
| 271 |
|