Skip to content

Commit

Permalink
Fixed error while crafting the herbo book
Browse files Browse the repository at this point in the history
  • Loading branch information
Redblueflame committed Jun 26, 2020
1 parent 25ba230 commit 760b9cd
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/main/java/com/redblueflame/herbocraft/HerboCraft.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Ingredient> 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<PatchouliBookRecipe> {
@Override
public PatchouliBookRecipe read(Identifier identifier, JsonObject jsonObject) {
String string = JsonHelper.getString(jsonObject, "group", "");
DefaultedList<Ingredient> 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<Ingredient> getIngredients(JsonArray json) {
DefaultedList<Ingredient> 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<Ingredient> 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);
}
}
}
25 changes: 8 additions & 17 deletions src/main/resources/data/herbocraft/recipes/herbo_book.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 760b9cd

Please sign in to comment.