| 1 | package net.minecraft.data.recipes;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.advancements.triggers.Criterion;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.HolderSet;
|
| 7 | import net.minecraft.core.component.DataComponentPatch;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.component.predicates.PotionsPredicate;
|
| 10 | import net.minecraft.core.registries.Registries;
|
| 11 | import net.minecraft.resources.Identifier;
|
| 12 | import net.minecraft.resources.ResourceKey;
|
| 13 | import net.minecraft.world.item.Item;
|
| 14 | import net.minecraft.world.item.ItemStackTemplate;
|
| 15 | import net.minecraft.world.item.alchemy.Potion;
|
| 16 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 17 | import net.minecraft.world.item.crafting.BrewingRecipe;
|
| 18 | import net.minecraft.world.item.crafting.PotionIngredient;
|
| 19 | import net.minecraft.world.item.crafting.Recipe;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | public class BrewingRecipeBuilder implements RecipeBuilder {
|
| 23 | private final PotionIngredient input;
|
| 24 | private final PotionIngredient reagent;
|
| 25 | private final ItemStackTemplate output;
|
| 26 |
|
| 27 | private BrewingRecipeBuilder(final PotionIngredient input, final PotionIngredient reagent, final ItemStackTemplate output) {
|
| 28 | this.input = input;
|
| 29 | this.reagent = reagent;
|
| 30 | this.output = output;
|
| 31 | }
|
| 32 |
|
| 33 | private static PotionIngredient potionIngredient(final Item potionContainer, final Holder<Potion> potion) {
|
| 34 | return PotionIngredient.of(potionContainer, PotionsPredicate.ofPotion(potion));
|
| 35 | }
|
| 36 |
|
| 37 | private static ItemStackTemplate potionOutput(final Item potionContainer, final Holder<Potion> potion) {
|
| 38 | return new ItemStackTemplate(potionContainer, DataComponentPatch.builder().set(DataComponents.POTION_CONTENTS, new PotionContents(potion)).build());
|
| 39 | }
|
| 40 |
|
| 41 | public static BrewingRecipeBuilder brewingMix(
|
| 42 | final Item container, final Holder<Potion> inputPotion, final Item reagentItem, final Holder<Potion> outputPotion
|
| 43 | ) {
|
| 44 | PotionIngredient input = potionIngredient(container, inputPotion);
|
| 45 | PotionIngredient reagent = PotionIngredient.of(reagentItem);
|
| 46 | ItemStackTemplate output = potionOutput(container, outputPotion);
|
| 47 | return new BrewingRecipeBuilder(input, reagent, output);
|
| 48 | }
|
| 49 |
|
| 50 | public static BrewingRecipeBuilder brewingContainerTransform(
|
| 51 | final Item inputContainer, final Holder<Potion> inputPotion, final Item reagentItem, final Item outputContainer
|
| 52 | ) {
|
| 53 | PotionIngredient input = potionIngredient(inputContainer, inputPotion);
|
| 54 | PotionIngredient reagent = PotionIngredient.of(reagentItem);
|
| 55 | ItemStackTemplate output = potionOutput(outputContainer, inputPotion);
|
| 56 | return new BrewingRecipeBuilder(input, reagent, output);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | public RecipeBuilder unlockedBy(final String name, final Criterion<?> criterion) {
|
| 61 | throw new IllegalStateException("Brewing recipes cannot be unlocked");
|
| 62 | }
|
| 63 |
|
| 64 | public BrewingRecipeBuilder group(final @Nullable String group) {
|
| 65 | throw new IllegalStateException("Brewing recipes do not have groups");
|
| 66 | }
|
| 67 |
|
| 68 | public static Optional<Holder<Potion>> getExactPotion(final PotionsPredicate predicate) {
|
| 69 | if (predicate.potions().isEmpty()) {
|
| 70 | return Optional.empty();
|
| 71 | }
|
| 72 |
|
| 73 | if (predicate.effects().isPresent()) {
|
| 74 | return Optional.empty();
|
| 75 | }
|
| 76 |
|
| 77 | HolderSet<Potion> potionSet = predicate.potions().get();
|
| 78 | return potionSet.size() != 1 ? Optional.empty() : Optional.of(potionSet.get(0));
|
| 79 | }
|
| 80 |
|
| 81 | private Optional<Holder<Potion>> getIngredientPotion(final PotionIngredient ingredient) {
|
| 82 | return ingredient.potions().flatMap(BrewingRecipeBuilder::getExactPotion);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public ResourceKey<Recipe<?>> defaultId() {
|
| 87 | ResourceKey<Item> inputItem = this.input.ingredient().getSingleItem().flatMap(Holder::unwrapKey).orElseThrow();
|
| 88 | ResourceKey<Potion> potionId = this.getIngredientPotion(this.input).flatMap(Holder::unwrapKey).orElseThrow();
|
| 89 | ResourceKey<Item> reagentItem = this.reagent.ingredient().getSingleItem().flatMap(Holder::unwrapKey).orElseThrow();
|
| 90 | Identifier combined = inputItem.identifier()
|
| 91 | .withPath(inputPath -> "brewing/" + inputPath + "_" + potionId.identifier().getPath() + "_" + reagentItem.identifier().getPath());
|
| 92 | return ResourceKey.create(Registries.RECIPE, combined);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public void save(final RecipeOutput recipeOutput, final ResourceKey<Recipe<?>> id) {
|
| 97 | BrewingRecipe recipe = new BrewingRecipe(this.input, this.reagent, this.output);
|
| 98 | recipeOutput.accept(id, recipe, null);
|
| 99 | }
|
| 100 | }
|
| 101 |
|