-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed error while crafting the herbo book
- Loading branch information
1 parent
25ba230
commit 760b9cd
Showing
3 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/com/redblueflame/herbocraft/utils/PatchouliBookRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/main/resources/data/herbocraft/recipes/herbo_book.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |