From a82e9f41b95475d134b70b4ba42cb61275008f06 Mon Sep 17 00:00:00 2001 From: Vincent Lee Date: Mon, 10 Jul 2023 18:20:05 -0700 Subject: [PATCH] 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 --- .../common/recipe/BookRecipeSerializer.java | 3 ++- .../common/recipe/ShapedBookRecipe.java | 21 +++++++++++++++---- .../common/recipe/ShapelessBookRecipe.java | 21 +++++++++++++++---- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java b/Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java index a628a247..23fd0f2b 100644 --- a/Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java +++ b/Xplat/src/main/java/vazkii/patchouli/common/recipe/BookRecipeSerializer.java @@ -13,10 +13,11 @@ import vazkii.patchouli.common.item.PatchouliItems; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.function.BiFunction; -public record BookRecipeSerializer, U extends T> (RecipeSerializer compose, BiFunction converter) implements RecipeSerializer { +public record BookRecipeSerializer, U extends T> (RecipeSerializer compose, BiFunction converter) implements RecipeSerializer { @Override @NotNull public U fromJson(@NotNull ResourceLocation id, @NotNull JsonObject json) { diff --git a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java index f17e6bed..e56adaa7 100644 --- a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java +++ b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java @@ -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 @@ public class ShapedBookRecipe extends ShapedRecipe { public static final RecipeSerializer 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 diff --git a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java index b7232d69..4fb8ff54 100644 --- a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java +++ b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java @@ -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 @@ public class ShapelessBookRecipe extends ShapelessRecipe { public static final RecipeSerializer 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