Skip to content

Commit

Permalink
Fix book recipe type being broken.
Browse files Browse the repository at this point in the history
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 8224fcf by dropping the method entirely"
Original commit changeset: f994182
  • Loading branch information
williewillus committed Jul 11, 2023
1 parent 798dd7a commit a82e9f4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,10 +21,17 @@
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,10 +21,17 @@
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
Expand Down

0 comments on commit a82e9f4

Please sign in to comment.