Unicopia/src/main/java/com/minelittlepony/unicopia/item/ZapAppleRecipe.java

59 lines
2.6 KiB
Java
Raw Normal View History

2020-10-02 21:09:14 +02:00
package com.minelittlepony.unicopia.item;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSyntaxException;
2020-10-02 21:09:14 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.ShapelessRecipe;
2022-12-18 22:07:24 +01:00
import net.minecraft.recipe.book.CraftingRecipeCategory;
2020-10-02 21:09:14 +02:00
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
2022-12-18 22:07:24 +01:00
import net.minecraft.registry.Registries;
2020-10-02 21:09:14 +02:00
public class ZapAppleRecipe extends ShapelessRecipe {
2022-12-18 22:07:24 +01:00
public ZapAppleRecipe(Identifier id, String group, CraftingRecipeCategory category, ItemStack output, DefaultedList<Ingredient> input) {
super(id, group, category, output, input);
2020-10-02 21:09:14 +02:00
}
public static class Serializer extends ShapelessRecipe.Serializer {
@Override
public ShapelessRecipe read(Identifier identifier, JsonObject json) {
String group = JsonHelper.getString(json, "group", "");
2022-12-18 22:07:24 +01:00
@SuppressWarnings("deprecation")
CraftingRecipeCategory category = CraftingRecipeCategory.CODEC.byId(JsonHelper.getString(json, "category", null), CraftingRecipeCategory.MISC);
2021-02-03 21:25:42 +01:00
DefaultedList<Ingredient> ingredients = URecipes.getIngredients(JsonHelper.getArray(json, "ingredients"));
2020-10-02 21:09:14 +02:00
if (ingredients.isEmpty()) {
throw new JsonParseException("No ingredients for shapeless recipe");
} else if (ingredients.size() > 9) {
throw new JsonParseException("Too many ingredients for shapeless recipe");
}
Identifier id = new Identifier(JsonHelper.getString(json, "appearance"));
2020-10-02 21:09:14 +02:00
2022-12-18 22:07:24 +01:00
return new ZapAppleRecipe(identifier, group, category, UItems.ZAP_APPLE.setAppearance(UItems.ZAP_APPLE.getDefaultStack(), Registries.ITEM.getOrEmpty(id).orElseThrow(() -> {
return new JsonSyntaxException("Unknown item '" + id + "'");
}).getDefaultStack()), ingredients);
2020-10-02 21:09:14 +02:00
}
@Override
public ShapelessRecipe read(Identifier identifier, PacketByteBuf input) {
String group = input.readString(32767);
2022-12-18 22:07:24 +01:00
CraftingRecipeCategory category = input.readEnumConstant(CraftingRecipeCategory.class);
2020-10-02 21:09:14 +02:00
DefaultedList<Ingredient> ingredients = DefaultedList.ofSize(input.readVarInt(), Ingredient.EMPTY);
for(int j = 0; j < ingredients.size(); ++j) {
ingredients.set(j, Ingredient.fromPacket(input));
}
2022-12-18 22:07:24 +01:00
return new ZapAppleRecipe(identifier, group, category, input.readItemStack(), ingredients);
2020-10-02 21:09:14 +02:00
}
}
}