diff --git a/src/main/java/com/redblueflame/herbocraft/HerboCraft.java b/src/main/java/com/redblueflame/herbocraft/HerboCraft.java index 2a75382..3a370ac 100644 --- a/src/main/java/com/redblueflame/herbocraft/HerboCraft.java +++ b/src/main/java/com/redblueflame/herbocraft/HerboCraft.java @@ -13,6 +13,7 @@ import com.redblueflame.herbocraft.items.TurretSeed; import com.redblueflame.herbocraft.items.UpgradeItem; import com.redblueflame.herbocraft.items.WateringCanItem; +import com.redblueflame.herbocraft.utils.PatchouliBookRecipe; import com.redblueflame.herbocraft.utils.TurretLooter; import io.netty.buffer.Unpooled; import nerdhub.cardinal.components.api.ComponentRegistry; @@ -38,6 +39,8 @@ import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.*; import net.minecraft.network.PacketByteBuf; +import net.minecraft.recipe.Recipe; +import net.minecraft.recipe.RecipeType; import net.minecraft.resource.ResourceManager; import net.minecraft.resource.ResourceType; import net.minecraft.server.network.ServerPlayerEntity; @@ -222,6 +225,9 @@ public void onInitialize() { (syncId, id, player, buf) -> new ReproducerBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); ContainerProviderRegistry.INSTANCE.registerFactory(UPGRADER_CONTAINER, (syncId, id, player, buf) -> new UpgraderBlockContainer(syncId, buf.readText(), player.inventory, buf.readBlockPos(), player.world)); + // Register recipes + Registry.register(Registry.RECIPE_SERIALIZER, new Identifier(name, "patchouli_book"), new PatchouliBookRecipe.Serializer()); + // region Packets ServerSidePacketRegistry.INSTANCE.register(HerboCraftPackets.WATERING_CAN_USAGE_PACKET, (packetContext, packetByteBuf) -> { BlockPos pos = packetByteBuf.readBlockPos(); diff --git a/src/main/java/com/redblueflame/herbocraft/utils/PatchouliBookRecipe.java b/src/main/java/com/redblueflame/herbocraft/utils/PatchouliBookRecipe.java new file mode 100644 index 0000000..3128441 --- /dev/null +++ b/src/main/java/com/redblueflame/herbocraft/utils/PatchouliBookRecipe.java @@ -0,0 +1,88 @@ +package com.redblueflame.herbocraft.utils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketByteBuf; +import net.minecraft.recipe.Ingredient; +import net.minecraft.recipe.RecipeSerializer; +import net.minecraft.recipe.ShapelessRecipe; +import net.minecraft.util.Identifier; +import net.minecraft.util.JsonHelper; +import net.minecraft.util.collection.DefaultedList; +import vazkii.patchouli.common.item.PatchouliItems; + +public class PatchouliBookRecipe extends ShapelessRecipe { + private Identifier book; + private String group; + + public PatchouliBookRecipe(Identifier id, Identifier book, String group, DefaultedList input) { + super(id, group, new ItemStack(PatchouliItems.book), input); + this.book = book; + this.group = group; + } + + @Override + public ItemStack craft(CraftingInventory craftingInventory) { + ItemStack ret = super.craft(craftingInventory); + ret.getOrCreateTag().putString("patchouli:book", book.toString()); + return ret; + } + + public static class Serializer implements RecipeSerializer { + @Override + public PatchouliBookRecipe read(Identifier identifier, JsonObject jsonObject) { + String string = JsonHelper.getString(jsonObject, "group", ""); + DefaultedList defaultedList = getIngredients(JsonHelper.getArray(jsonObject, "ingredients")); + if (defaultedList.isEmpty()) { + throw new JsonParseException("No ingredients for shapeless recipe"); + } else if (defaultedList.size() > 9) { + throw new JsonParseException("Too many ingredients for shapeless recipe"); + } else { + Identifier bookId = new Identifier(JsonHelper.getString(jsonObject, "book")); + return new PatchouliBookRecipe(identifier, bookId, string, defaultedList); + } + } + + private static DefaultedList getIngredients(JsonArray json) { + DefaultedList defaultedList = DefaultedList.of(); + + for(int i = 0; i < json.size(); ++i) { + Ingredient ingredient = Ingredient.fromJson(json.get(i)); + if (!ingredient.isEmpty()) { + defaultedList.add(ingredient); + } + } + + return defaultedList; + } + + @Override + public PatchouliBookRecipe read(Identifier id, PacketByteBuf buf) { + String string = buf.readString(32767); + int i = buf.readVarInt(); + DefaultedList defaultedList = DefaultedList.ofSize(i, Ingredient.EMPTY); + + for(int j = 0; j < defaultedList.size(); ++j) { + defaultedList.set(j, Ingredient.fromPacket(buf)); + } + + Identifier book = buf.readIdentifier(); + return new PatchouliBookRecipe(id, book, string, defaultedList); + } + + @Override + public void write(PacketByteBuf buf, PatchouliBookRecipe recipe) { + buf.writeString(recipe.group); + buf.writeVarInt(recipe.getPreviewInputs().size()); + + for (Ingredient ingredient : recipe.getPreviewInputs()) { + ingredient.write(buf); + } + + buf.writeIdentifier(recipe.book); + } + } +} diff --git a/src/main/resources/data/herbocraft/recipes/herbo_book.json b/src/main/resources/data/herbocraft/recipes/herbo_book.json index 1aa8403..5d7aaf8 100644 --- a/src/main/resources/data/herbocraft/recipes/herbo_book.json +++ b/src/main/resources/data/herbocraft/recipes/herbo_book.json @@ -1,21 +1,12 @@ { - "type": "minecraft:crafting_shaped", - "pattern": [ - "BS" - ], - "key": { - "B": { - "item": "minecraft:book" + "type": "herbocraft:patchouli_book", + "ingredients": [ + { + "item": "minecraft:book" }, - "S": { - "item": "minecraft:wheat_seeds" + { + "item": "minecraft:wheat_seeds" } - }, - "result": { - "item": "patchouli:guide_book", - "nbt": { - "patchouli:book": "herbocraft:main-book" - }, - "count": 1 - } + ], + "book": "herbocraft:main-book" } \ No newline at end of file