60 lines
1.7 KiB
Java
60 lines
1.7 KiB
Java
package net.minecraft.world.item.trading;
|
|
|
|
import com.mojang.serialization.Codec;
|
|
import java.util.ArrayList;
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.function.Function;
|
|
import net.minecraft.network.RegistryFriendlyByteBuf;
|
|
import net.minecraft.network.codec.ByteBufCodecs;
|
|
import net.minecraft.network.codec.StreamCodec;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import org.jetbrains.annotations.Nullable;
|
|
|
|
public class MerchantOffers extends ArrayList<MerchantOffer> {
|
|
public static final Codec<MerchantOffers> CODEC = MerchantOffer.CODEC
|
|
.listOf()
|
|
.optionalFieldOf("Recipes", List.of())
|
|
.<MerchantOffers>xmap(MerchantOffers::new, Function.identity())
|
|
.codec();
|
|
public static final StreamCodec<RegistryFriendlyByteBuf, MerchantOffers> STREAM_CODEC = MerchantOffer.STREAM_CODEC
|
|
.apply(ByteBufCodecs.collection(MerchantOffers::new));
|
|
|
|
public MerchantOffers() {
|
|
}
|
|
|
|
private MerchantOffers(int size) {
|
|
super(size);
|
|
}
|
|
|
|
private MerchantOffers(Collection<MerchantOffer> offers) {
|
|
super(offers);
|
|
}
|
|
|
|
@Nullable
|
|
public MerchantOffer getRecipeFor(ItemStack stackA, ItemStack stackB, int index) {
|
|
if (index > 0 && index < this.size()) {
|
|
MerchantOffer merchantOffer = (MerchantOffer)this.get(index);
|
|
return merchantOffer.satisfiedBy(stackA, stackB) ? merchantOffer : null;
|
|
} else {
|
|
for (int i = 0; i < this.size(); i++) {
|
|
MerchantOffer merchantOffer2 = (MerchantOffer)this.get(i);
|
|
if (merchantOffer2.satisfiedBy(stackA, stackB)) {
|
|
return merchantOffer2;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public MerchantOffers copy() {
|
|
MerchantOffers merchantOffers = new MerchantOffers(this.size());
|
|
|
|
for (MerchantOffer merchantOffer : this) {
|
|
merchantOffers.add(merchantOffer.copy());
|
|
}
|
|
|
|
return merchantOffers;
|
|
}
|
|
}
|