From e7af05688d92c6065caebeff17cb794eadc498a4 Mon Sep 17 00:00:00 2001 From: Mrbysco Date: Sat, 4 May 2024 00:07:58 +0200 Subject: [PATCH] Remove custom book recipe You can specify components in vanilla results now --- .../fabric/common/FabricModInitializer.java | 1 - .../common/NeoForgeModInitializer.java | 3 - .../TextComponentVariableSerializer.java | 6 +- .../patchouli/common/item/PatchouliItems.java | 8 -- .../common/recipe/ShapedBookRecipe.java | 95 -------------- .../common/recipe/ShapelessBookRecipe.java | 123 ------------------ 6 files changed, 4 insertions(+), 232 deletions(-) delete mode 100644 Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java delete mode 100644 Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java diff --git a/Fabric/src/main/java/vazkii/patchouli/fabric/common/FabricModInitializer.java b/Fabric/src/main/java/vazkii/patchouli/fabric/common/FabricModInitializer.java index 390dc27a9..27f077b9e 100644 --- a/Fabric/src/main/java/vazkii/patchouli/fabric/common/FabricModInitializer.java +++ b/Fabric/src/main/java/vazkii/patchouli/fabric/common/FabricModInitializer.java @@ -29,7 +29,6 @@ public class FabricModInitializer implements ModInitializer { public void onInitialize() { PatchouliSounds.submitRegistrations((id, e) -> Registry.register(BuiltInRegistries.SOUND_EVENT, id, e)); PatchouliItems.submitItemRegistrations((id, e) -> Registry.register(BuiltInRegistries.ITEM, id, e)); - PatchouliItems.submitRecipeSerializerRegistrations((id, e) -> Registry.register(BuiltInRegistries.RECIPE_SERIALIZER, id, e)); PatchouliDataComponents.submitDataComponentRegistrations((id, e) -> Registry.register(BuiltInRegistries.DATA_COMPONENT_TYPE, id, e)); FiberPatchouliConfig.setup(); CommandRegistrationCallback.EVENT.register((disp, buildCtx, selection) -> OpenBookCommand.register(disp)); diff --git a/NeoForge/src/main/java/vazkii/patchouli/neoforge/common/NeoForgeModInitializer.java b/NeoForge/src/main/java/vazkii/patchouli/neoforge/common/NeoForgeModInitializer.java index 719939e5c..3b060ca47 100644 --- a/NeoForge/src/main/java/vazkii/patchouli/neoforge/common/NeoForgeModInitializer.java +++ b/NeoForge/src/main/java/vazkii/patchouli/neoforge/common/NeoForgeModInitializer.java @@ -46,9 +46,6 @@ public static void register(RegisterEvent evt) { evt.register(Registries.ITEM, rh -> { PatchouliItems.submitItemRegistrations(rh::register); }); - evt.register(Registries.RECIPE_SERIALIZER, rh -> { - PatchouliItems.submitRecipeSerializerRegistrations(rh::register); - }); evt.register(Registries.DATA_COMPONENT_TYPE, rh -> { PatchouliDataComponents.submitDataComponentRegistrations(rh::register); }); diff --git a/Xplat/src/main/java/vazkii/patchouli/client/book/template/variable/TextComponentVariableSerializer.java b/Xplat/src/main/java/vazkii/patchouli/client/book/template/variable/TextComponentVariableSerializer.java index ea27908bf..fa2ab383e 100644 --- a/Xplat/src/main/java/vazkii/patchouli/client/book/template/variable/TextComponentVariableSerializer.java +++ b/Xplat/src/main/java/vazkii/patchouli/client/book/template/variable/TextComponentVariableSerializer.java @@ -1,13 +1,15 @@ package vazkii.patchouli.client.book.template.variable; import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.mojang.serialization.JsonOps; import net.minecraft.core.RegistryAccess; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component.Serializer; +import net.minecraft.network.chat.ComponentSerialization; import vazkii.patchouli.api.IVariableSerializer; -import vazkii.patchouli.mixin.AccessorComponentSerializer; public class TextComponentVariableSerializer implements IVariableSerializer { @Override @@ -23,6 +25,6 @@ public Component fromJson(JsonElement json) { @Override public JsonElement toJson(Component stack) { - return AccessorComponentSerializer.invokeSerialize(stack, RegistryAccess.EMPTY); + return ComponentSerialization.CODEC.encodeStart(RegistryAccess.EMPTY.createSerializationContext(JsonOps.INSTANCE), stack).getOrThrow(JsonParseException::new); } } diff --git a/Xplat/src/main/java/vazkii/patchouli/common/item/PatchouliItems.java b/Xplat/src/main/java/vazkii/patchouli/common/item/PatchouliItems.java index 0eb57e670..a469d6a58 100644 --- a/Xplat/src/main/java/vazkii/patchouli/common/item/PatchouliItems.java +++ b/Xplat/src/main/java/vazkii/patchouli/common/item/PatchouliItems.java @@ -2,11 +2,8 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; -import net.minecraft.world.item.crafting.RecipeSerializer; import vazkii.patchouli.api.PatchouliAPI; -import vazkii.patchouli.common.recipe.ShapedBookRecipe; -import vazkii.patchouli.common.recipe.ShapelessBookRecipe; import java.util.function.BiConsumer; @@ -18,9 +15,4 @@ public class PatchouliItems { public static void submitItemRegistrations(BiConsumer consumer) { consumer.accept(BOOK_ID, BOOK); } - - public static void submitRecipeSerializerRegistrations(BiConsumer> consumer) { - consumer.accept(new ResourceLocation(PatchouliAPI.MOD_ID, "shaped_book_recipe"), ShapedBookRecipe.SERIALIZER); - consumer.accept(new ResourceLocation(PatchouliAPI.MOD_ID, "shapeless_book_recipe"), ShapelessBookRecipe.SERIALIZER); - } } diff --git a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java deleted file mode 100644 index 55b92fbb6..000000000 --- a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapedBookRecipe.java +++ /dev/null @@ -1,95 +0,0 @@ -package vazkii.patchouli.common.recipe; - -import com.mojang.serialization.Codec; -import com.mojang.serialization.MapCodec; -import com.mojang.serialization.codecs.RecordCodecBuilder; - -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.StreamCodec; -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 net.minecraft.world.item.crafting.ShapedRecipePattern; - -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 - * "result" object is replaced by a "book" string for the book ID. - */ -public class ShapedBookRecipe extends ShapedRecipe { - public static final RecipeSerializer SERIALIZER = new Serializer(); - - final ShapedRecipePattern pattern; - final ItemStack result; - final String group; - final @Nullable ResourceLocation outputBook; - - public ShapedBookRecipe(String group, ShapedRecipePattern recipePattern, ItemStack result, @Nullable ResourceLocation outputBook) { - super(group, CraftingBookCategory.MISC, recipePattern, getOutputBook(result, outputBook)); - this.pattern = recipePattern; - this.result = result; - this.group = group; - this.outputBook = outputBook; - } - - private static ItemStack getOutputBook(ItemStack result, @Nullable ResourceLocation outputBook) { - if (outputBook != null) { - return PatchouliAPI.get().getBookStack(outputBook); - } - return result; - } - - @Override - public RecipeSerializer getSerializer() { - return SERIALIZER; - } - - public static class Serializer implements RecipeSerializer { - public static final MapCodec CODEC = RecordCodecBuilder.mapCodec( - instance -> instance.group( - Codec.STRING.optionalFieldOf("group", "").forGetter(bookRecipe -> bookRecipe.group), - ShapedRecipePattern.MAP_CODEC.forGetter(bookRecipe -> bookRecipe.pattern), - ItemStack.STRICT_CODEC.optionalFieldOf("result", ItemStack.EMPTY).forGetter(bookRecipe -> bookRecipe.result), - ResourceLocation.CODEC.optionalFieldOf("book", null).forGetter(bookRecipe -> bookRecipe.outputBook) - ) - .apply(instance, ShapedBookRecipe::new) - ); - public static final StreamCodec STREAM_CODEC = StreamCodec.of( - ShapedBookRecipe.Serializer::toNetwork, ShapedBookRecipe.Serializer::fromNetwork - ); - - @Override - public MapCodec codec() { - return CODEC; - } - - @Override - public StreamCodec streamCodec() { - return STREAM_CODEC; - } - - private static ShapedBookRecipe fromNetwork(RegistryFriendlyByteBuf buf) { - String group = buf.readUtf(); - ShapedRecipePattern recipePattern = ShapedRecipePattern.STREAM_CODEC.decode(buf); - ItemStack result = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf); - ResourceLocation outputBook = buf.readBoolean() ? buf.readResourceLocation() : null; - return new ShapedBookRecipe(group, recipePattern, result, outputBook); - } - - private static void toNetwork(RegistryFriendlyByteBuf buf, ShapedBookRecipe bookRecipe) { - buf.writeUtf(bookRecipe.group); - ShapedRecipePattern.STREAM_CODEC.encode(buf, bookRecipe.pattern); - ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, bookRecipe.result); - buf.writeBoolean(bookRecipe.outputBook != null); - if (bookRecipe.outputBook != null) { - buf.writeResourceLocation(bookRecipe.outputBook); - } - } - } -} diff --git a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java b/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java deleted file mode 100644 index d7611d53d..000000000 --- a/Xplat/src/main/java/vazkii/patchouli/common/recipe/ShapelessBookRecipe.java +++ /dev/null @@ -1,123 +0,0 @@ -package vazkii.patchouli.common.recipe; - -import com.mojang.serialization.Codec; -import com.mojang.serialization.DataResult; -import com.mojang.serialization.MapCodec; -import com.mojang.serialization.codecs.RecordCodecBuilder; - -import net.minecraft.core.NonNullList; -import net.minecraft.network.RegistryFriendlyByteBuf; -import net.minecraft.network.codec.StreamCodec; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.crafting.CraftingBookCategory; -import net.minecraft.world.item.crafting.Ingredient; -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 - * "result" object is replaced by a "book" string for the book ID. - */ -public class ShapelessBookRecipe extends ShapelessRecipe { - public static final RecipeSerializer SERIALIZER = new Serializer(); - - final String group; - final ItemStack result; - final NonNullList ingredients; - final @Nullable ResourceLocation outputBook; - - public ShapelessBookRecipe(String group, ItemStack result, NonNullList ingredients, @Nullable ResourceLocation outputBook) { - super(group, CraftingBookCategory.MISC, getOutputBook(result, outputBook), ingredients); - this.group = group; - this.result = result; - this.ingredients = ingredients; - this.outputBook = outputBook; - } - - private static ItemStack getOutputBook(ItemStack result, @Nullable ResourceLocation outputBook) { - if (outputBook != null) { - return PatchouliAPI.get().getBookStack(outputBook); - } - return result; - } - - @Override - public RecipeSerializer getSerializer() { - return SERIALIZER; - } - - public static class Serializer implements RecipeSerializer { - static int maxWidth = 3; - static int maxHeight = 3; - private static final MapCodec CODEC = RecordCodecBuilder.mapCodec( - instance -> instance.group( - Codec.STRING.optionalFieldOf("group", "").forGetter(bookRecipe -> bookRecipe.group), - ItemStack.STRICT_CODEC.optionalFieldOf("result", ItemStack.EMPTY).forGetter(bookRecipe -> bookRecipe.result), - Ingredient.CODEC_NONEMPTY - .listOf() - .fieldOf("ingredients") - .flatXmap( - ingredientList -> { - Ingredient[] aingredient = ingredientList - .toArray(Ingredient[]::new); - if (aingredient.length == 0) { - return DataResult.error(() -> "No ingredients for shapeless book recipe"); - } else { - return aingredient.length > maxHeight * maxWidth - ? DataResult.error(() -> "Too many ingredients for shapeless book recipe. The maximum is: %s".formatted(maxHeight * maxWidth)) - : DataResult.success(NonNullList.of(Ingredient.EMPTY, aingredient)); - } - }, - DataResult::success - ) - .forGetter(bookRecipe -> bookRecipe.ingredients), - ResourceLocation.CODEC.optionalFieldOf("book", null).forGetter(bookRecipe -> bookRecipe.outputBook) - ) - .apply(instance, ShapelessBookRecipe::new) - ); - public static final StreamCodec STREAM_CODEC = StreamCodec.of( - ShapelessBookRecipe.Serializer::toNetwork, ShapelessBookRecipe.Serializer::fromNetwork - ); - - @Override - public MapCodec codec() { - return CODEC; - } - - @Override - public StreamCodec streamCodec() { - return STREAM_CODEC; - } - - private static ShapelessBookRecipe fromNetwork(RegistryFriendlyByteBuf buf) { - String group = buf.readUtf(); - int i = buf.readVarInt(); - NonNullList ingredients = NonNullList.withSize(i, Ingredient.EMPTY); - ingredients.replaceAll(p_319735_ -> Ingredient.CONTENTS_STREAM_CODEC.decode(buf)); - ItemStack result = ItemStack.OPTIONAL_STREAM_CODEC.decode(buf); - ResourceLocation outputBook = buf.readBoolean() ? buf.readResourceLocation() : null; - return new ShapelessBookRecipe(group, result, ingredients, outputBook); - } - - private static void toNetwork(RegistryFriendlyByteBuf buf, ShapelessBookRecipe bookRecipe) { - buf.writeUtf(bookRecipe.group); - buf.writeVarInt(bookRecipe.ingredients.size()); - - for (Ingredient ingredient : bookRecipe.ingredients) { - Ingredient.CONTENTS_STREAM_CODEC.encode(buf, ingredient); - } - - ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, bookRecipe.result); - buf.writeBoolean(bookRecipe.outputBook != null); - if (bookRecipe.outputBook != null) { - buf.writeResourceLocation(bookRecipe.outputBook); - } - } - } -}