| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import com.mojang.serialization.MapCodec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.Optional;
|
| 6 | import java.util.function.Predicate;
|
| 7 | import net.minecraft.core.component.predicates.PotionsPredicate;
|
| 8 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 9 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 10 | import net.minecraft.network.codec.StreamCodec;
|
| 11 | import net.minecraft.tags.ItemTags;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 |
|
| 15 | public record PotionIngredient(Ingredient ingredient, Optional<PotionsPredicate> potions) implements Predicate<ItemStack> {
|
| 16 | public static final MapCodec<PotionIngredient> MAP_CODEC = RecordCodecBuilder.mapCodec(
|
| 17 | i -> i.group(
|
| 18 | Ingredient.CODEC.fieldOf("item").forGetter(o -> o.ingredient), PotionsPredicate.CODEC.optionalFieldOf("potion_contents").forGetter(o -> o.potions)
|
| 19 | )
|
| 20 | .apply(i, PotionIngredient::new)
|
| 21 | );
|
| 22 | public static final StreamCodec<RegistryFriendlyByteBuf, PotionIngredient> STREAM_CODEC = StreamCodec.composite(
|
| 23 | Ingredient.CONTENTS_STREAM_CODEC, o -> o.ingredient, ByteBufCodecs.optional(PotionsPredicate.STREAM_CODEC), o -> o.potions, PotionIngredient::new
|
| 24 | );
|
| 25 |
|
| 26 | public boolean test(final ItemStack input) {
|
| 27 | return !this.ingredient.test(input) ? false : this.potions.isEmpty() || this.potions.get().matches(input);
|
| 28 | }
|
| 29 |
|
| 30 | public static PotionIngredient of(final Item item, final PotionsPredicate potions) {
|
| 31 | return new PotionIngredient(Ingredient.of(item), Optional.of(potions));
|
| 32 | }
|
| 33 |
|
| 34 | public static PotionIngredient of(final Item item) {
|
| 35 | return new PotionIngredient(Ingredient.of(item), Optional.empty());
|
| 36 | }
|
| 37 |
|
| 38 | public static boolean isPotionInput(final ItemStack itemStack, final RecipeAccess recipeAccess) {
|
| 39 | RecipePropertySet brewingInputs = recipeAccess.propertySet(RecipePropertySet.BREWING_INPUTS);
|
| 40 | return brewingInputs.test(itemStack) || itemStack.is(ItemTags.BREWING_POTION_INPUTS);
|
| 41 | }
|
| 42 | }
|
| 43 |
|