| 1 | package net.minecraft.world.item.trading;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.ArrayList;
|
| 5 | import java.util.Collection;
|
| 6 | import java.util.List;
|
| 7 | import java.util.function.Function;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 |
|
| 14 | public class MerchantOffers extends ArrayList<MerchantOffer> {
|
| 15 | public static final Codec<MerchantOffers> CODEC = MerchantOffer.CODEC
|
| 16 | .listOf()
|
| 17 | .optionalFieldOf("Recipes", List.of())
|
| 18 | .xmap(MerchantOffers::new, Function.identity())
|
| 19 | .codec();
|
| 20 | public static final StreamCodec<RegistryFriendlyByteBuf, MerchantOffers> STREAM_CODEC = MerchantOffer.STREAM_CODEC
|
| 21 | .apply(ByteBufCodecs.collection(MerchantOffers::new));
|
| 22 |
|
| 23 | public MerchantOffers() {
|
| 24 | }
|
| 25 |
|
| 26 | private MerchantOffers(final int initialCapacity) {
|
| 27 | super(initialCapacity);
|
| 28 | }
|
| 29 |
|
| 30 | private MerchantOffers(final Collection<MerchantOffer> offers) {
|
| 31 | super(offers);
|
| 32 | }
|
| 33 |
|
| 34 | public @Nullable MerchantOffer getRecipeFor(final ItemStack buyA, final ItemStack buyB, final int selectionHint) {
|
| 35 | if (selectionHint > 0 && selectionHint < this.size()) {
|
| 36 | MerchantOffer offer = this.get(selectionHint);
|
| 37 | return offer.satisfiedBy(buyA, buyB) ? offer : null;
|
| 38 | }
|
| 39 |
|
| 40 | for (int i = 0; i < this.size(); i++) {
|
| 41 | MerchantOffer offer = this.get(i);
|
| 42 | if (offer.satisfiedBy(buyA, buyB)) {
|
| 43 | return offer;
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | return null;
|
| 48 | }
|
| 49 |
|
| 50 | public MerchantOffers copy() {
|
| 51 | MerchantOffers offers = new MerchantOffers(this.size());
|
| 52 |
|
| 53 | for (MerchantOffer offer : this) {
|
| 54 | offers.add(offer.copy());
|
| 55 | }
|
| 56 |
|
| 57 | return offers;
|
| 58 | }
|
| 59 | }
|
| 60 |
|