| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import java.util.Arrays;
|
| 5 | import java.util.Objects;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.function.Predicate;
|
| 8 | import java.util.stream.Stream;
|
| 9 | import net.fabricmc.fabric.api.recipe.v1.ingredient.FabricIngredient;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.HolderSet;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.HolderSetCodec;
|
| 14 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 15 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 16 | import net.minecraft.network.codec.StreamCodec;
|
| 17 | import net.minecraft.util.ExtraCodecs;
|
| 18 | import net.minecraft.world.entity.player.StackedContents;
|
| 19 | import net.minecraft.world.item.Item;
|
| 20 | import net.minecraft.world.item.ItemStack;
|
| 21 | import net.minecraft.world.item.ItemStackTemplate;
|
| 22 | import net.minecraft.world.item.Items;
|
| 23 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 24 | import net.minecraft.world.level.ItemLike;
|
| 25 |
|
| 26 | public final class Ingredient implements Predicate<ItemStack>, StackedContents.IngredientInfo<Holder<Item>>, FabricIngredient {
|
| 27 | public static final StreamCodec<RegistryFriendlyByteBuf, Ingredient> CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.ITEM)
|
| 28 | .map(Ingredient::new, i -> i.values);
|
| 29 | public static final StreamCodec<RegistryFriendlyByteBuf, Optional<Ingredient>> OPTIONAL_CONTENTS_STREAM_CODEC = ByteBufCodecs.holderSet(Registries.ITEM)
|
| 30 | .map(
|
| 31 | ingredient -> ingredient.size() == 0 ? Optional.empty() : Optional.of(new Ingredient((HolderSet<Item>)ingredient)),
|
| 32 | ingredient -> ingredient.<HolderSet<Item>>map(i -> i.values).orElse(HolderSet.empty())
|
| 33 | );
|
| 34 | public static final Codec<HolderSet<Item>> NON_AIR_HOLDER_SET_CODEC = HolderSetCodec.create(Registries.ITEM, Item.CODEC, false);
|
| 35 | public static final Codec<Ingredient> CODEC = ExtraCodecs.nonEmptyHolderSet(NON_AIR_HOLDER_SET_CODEC).xmap(Ingredient::new, i -> i.values);
|
| 36 | private final HolderSet<Item> values;
|
| 37 |
|
| 38 | private Ingredient(final HolderSet<Item> values) {
|
| 39 | values.unwrap().ifRight(directValues -> {
|
| 40 | if (directValues.isEmpty()) {
|
| 41 | throw new UnsupportedOperationException("Ingredients can't be empty");
|
| 42 | }
|
| 43 |
|
| 44 | if (directValues.contains(Items.AIR.builtInRegistryHolder())) {
|
| 45 | throw new UnsupportedOperationException("Ingredient can't contain air");
|
| 46 | }
|
| 47 | });
|
| 48 | this.values = values;
|
| 49 | }
|
| 50 |
|
| 51 | public static boolean testOptionalIngredient(final Optional<Ingredient> ingredient, final ItemStack stack) {
|
| 52 | return ingredient.<Boolean>map(value -> value.test(stack)).orElseGet(stack::isEmpty);
|
| 53 | }
|
| 54 |
|
| 55 | @Deprecated
|
| 56 | public Stream<Holder<Item>> items() {
|
| 57 | return this.values.stream();
|
| 58 | }
|
| 59 |
|
| 60 | public boolean isEmpty() {
|
| 61 | return this.values.size() == 0;
|
| 62 | }
|
| 63 |
|
| 64 | public boolean test(final ItemStack input) {
|
| 65 | return input.is(this.values);
|
| 66 | }
|
| 67 |
|
| 68 | public boolean acceptsItem(final Holder<Item> item) {
|
| 69 | return this.values.contains(item);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | public boolean equals(final Object o) {
|
| 74 | return o instanceof Ingredient other ? Objects.equals(this.values, other.values) : false;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public int hashCode() {
|
| 79 | return Objects.hashCode(this.values);
|
| 80 | }
|
| 81 |
|
| 82 | public static Ingredient of(final ItemLike itemLike) {
|
| 83 | return new Ingredient(HolderSet.direct(itemLike.asItem().builtInRegistryHolder()));
|
| 84 | }
|
| 85 |
|
| 86 | public static Ingredient of(final ItemLike... items) {
|
| 87 | return of(Arrays.stream(items));
|
| 88 | }
|
| 89 |
|
| 90 | public static Ingredient of(final Stream<? extends ItemLike> stream) {
|
| 91 | return new Ingredient(HolderSet.direct(stream.map(e -> e.asItem().builtInRegistryHolder()).toList()));
|
| 92 | }
|
| 93 |
|
| 94 | public static Ingredient of(final HolderSet<Item> tag) {
|
| 95 | return new Ingredient(tag);
|
| 96 | }
|
| 97 |
|
| 98 | public SlotDisplay display() {
|
| 99 | return new SlotDisplay.TagSlotDisplay(this.values);
|
| 100 | }
|
| 101 |
|
| 102 | public static SlotDisplay optionalIngredientToDisplay(final Optional<Ingredient> ingredient) {
|
| 103 | return ingredient.map(Ingredient::display).orElse(SlotDisplay.Empty.INSTANCE);
|
| 104 | }
|
| 105 |
|
| 106 | private static SlotDisplay displayForSingleItem(final Holder<Item> item) {
|
| 107 | SlotDisplay inputDisplay = new SlotDisplay.ItemSlotDisplay(item);
|
| 108 | ItemStackTemplate remainderStack = item.value().getCraftingRemainder();
|
| 109 | if (remainderStack != null) {
|
| 110 | SlotDisplay remainderDisplay = new SlotDisplay.ItemStackSlotDisplay(remainderStack);
|
| 111 | return new SlotDisplay.WithRemainder(inputDisplay, remainderDisplay);
|
| 112 | } else {
|
| 113 | return inputDisplay;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | public Optional<Holder<Item>> getSingleItem() {
|
| 118 | return this.values.size() != 1 ? Optional.empty() : Optional.of(this.values.get(0));
|
| 119 | }
|
| 120 | }
|
| 121 |
|