~williewillus/patchouli

12f89b0fe3da2dbbbd5d8edf87b3fffa4cf0bee2 — Vincent Lee 4 months ago d119d28 release-1.19.4-79.1
Fix book recipe type being broken.

In the original commit message, I claimed outputBook was always null, but that was a lie,
I just can't read.

Restore calling getResultItem and add comments/safety checks about why what we're doing is safe.

Back out "Refine commit 8224fcf29781f7820c38b172645c3d14ead96b3d by dropping the method entirely"
Original commit changeset: f994182f635f
M Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java => Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java +2 -1
@@ 13,10 13,11 @@ import vazkii.patchouli.common.book.BookRegistry;
import vazkii.patchouli.common.item.PatchouliItems;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.BiFunction;

public record BookRecipeSerializer<T extends Recipe<?>, U extends T> (RecipeSerializer<T> compose, BiFunction<T, ResourceLocation, U> converter) implements RecipeSerializer<U> {
public record BookRecipeSerializer<T extends Recipe<?>, U extends T> (RecipeSerializer<T> compose, BiFunction<T, @Nullable ResourceLocation, U> converter) implements RecipeSerializer<U> {
	@Override
	@NotNull
	public U fromJson(@NotNull ResourceLocation id, @NotNull JsonObject json) {

M Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java => Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java +17 -4
@@ 1,12 1,18 @@
package vazkii.patchouli.common.recipe;

import com.google.common.base.Preconditions;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;

import vazkii.patchouli.api.PatchouliAPI;

import org.jetbrains.annotations.Nullable;

/**
 * Recipe type for shaped book recipes.
 * The format is the same as vanilla shaped recipes, but the


@@ 15,10 21,17 @@ import vazkii.patchouli.api.PatchouliAPI;
public class ShapedBookRecipe extends ShapedRecipe {
	public static final RecipeSerializer<ShapedBookRecipe> SERIALIZER = new BookRecipeSerializer<>(RecipeSerializer.SHAPED_RECIPE, ShapedBookRecipe::new);

	public ShapedBookRecipe(ShapedRecipe compose, ResourceLocation outputBook) {
		super(compose.getId(), compose.getGroup(), CraftingBookCategory.MISC,
				compose.getWidth(), compose.getHeight(), compose.getIngredients(),
				PatchouliAPI.get().getBookStack(outputBook));
	public ShapedBookRecipe(ShapedRecipe compose, @Nullable ResourceLocation outputBook) {
		super(compose.getId(), compose.getGroup(), CraftingBookCategory.MISC, compose.getWidth(), compose.getHeight(), compose.getIngredients(), getOutputBook(compose, outputBook));
	}

	private static ItemStack getOutputBook(ShapedRecipe compose, @Nullable ResourceLocation outputBook) {
		Preconditions.checkArgument(compose.getClass() == ShapedRecipe.class, "Must be exactly ShapedRecipe");
		if (outputBook != null) {
			return PatchouliAPI.get().getBookStack(outputBook);
		}
		// The vanilla ShapedRecipe implementation never uses the passed RegistryAccess, so this is ok.
		return compose.getResultItem(RegistryAccess.EMPTY);
	}

	@Override

M Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java => Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java +17 -4
@@ 1,12 1,18 @@
package vazkii.patchouli.common.recipe;

import com.google.common.base.Preconditions;

import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.CraftingBookCategory;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapelessRecipe;

import vazkii.patchouli.api.PatchouliAPI;

import org.jetbrains.annotations.Nullable;

/**
 * Recipe type for shapeless book recipes.
 * The format is the same as vanilla shapeless recipes, but the


@@ 15,10 21,17 @@ import vazkii.patchouli.api.PatchouliAPI;
public class ShapelessBookRecipe extends ShapelessRecipe {
	public static final RecipeSerializer<ShapelessBookRecipe> SERIALIZER = new BookRecipeSerializer<>(RecipeSerializer.SHAPELESS_RECIPE, ShapelessBookRecipe::new);

	public ShapelessBookRecipe(ShapelessRecipe compose, ResourceLocation outputBook) {
		super(compose.getId(), compose.getGroup(), CraftingBookCategory.MISC,
				PatchouliAPI.get().getBookStack(outputBook),
				compose.getIngredients());
	public ShapelessBookRecipe(ShapelessRecipe compose, @Nullable ResourceLocation outputBook) {
		super(compose.getId(), compose.getGroup(), CraftingBookCategory.MISC, getOutputBook(compose, outputBook), compose.getIngredients());
	}

	private static ItemStack getOutputBook(ShapelessRecipe compose, @Nullable ResourceLocation outputBook) {
		Preconditions.checkArgument(compose.getClass() == ShapelessRecipe.class, "Must be exactly ShapelessRecipe");
		if (outputBook != null) {
			return PatchouliAPI.get().getBookStack(outputBook);
		}
		// The vanilla ShapelessRecipe implementation never uses the passed RegistryAccess, so this is ok.
		return compose.getResultItem(RegistryAccess.EMPTY);
	}

	@Override