package net.gamelog.suntrot.recipe; import com.google.gson.Gson; import com.google.gson.JsonObject; import net.minecraft.inventory.Inventory; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.network.PacketByteBuf; import net.minecraft.recipe.*; import net.minecraft.registry.DynamicRegistryManager; import net.minecraft.registry.Registries; import net.minecraft.util.Identifier; import net.minecraft.world.World; import org.spongepowered.include.com.google.gson.JsonSyntaxException; public class SewingRecipe implements Recipe { private final Ingredient item; private final Ingredient dye; private final ItemStack result; private final Identifier id; public SewingRecipe(Ingredient item, Ingredient dye, ItemStack result, Identifier id) { this.item = item; this.dye = dye; this.result = result; this.id = id; } public Ingredient getItem() { return item; } public Ingredient getDye() { return dye; } @Override public boolean matches(Inventory inventory, World world) { if (inventory.size() < 2) return false; return item.test(inventory.getStack(0)) && dye.test(inventory.getStack(1)); } @Override public ItemStack craft(Inventory inventory, DynamicRegistryManager registryManager) { return ItemStack.EMPTY; } @Override public boolean fits(int var1, int var2) { return false; } @Override public ItemStack getOutput(DynamicRegistryManager registryManager) { return result; } @Override public Identifier getId() { return id; } public static class Type implements RecipeType { // Define ExampleRecipe.Type as a singleton by making its constructor private and exposing an instance. private Type() {} public static final Type INSTANCE = new Type(); public static final String ID = "two_slot_recipe"; } @Override public RecipeType getType() { return Type.INSTANCE; } class SewingRecipeJsonFormat{ JsonObject item; JsonObject dye; String result; int count; } public static class SewingRecipeSerializer implements RecipeSerializer { private SewingRecipeSerializer() {} public static final SewingRecipeSerializer INSTANCE = new SewingRecipeSerializer(); // This will be the "type" field in the json public static final Identifier ID = new Identifier("suntrot:sewing_recipe"); @Override public SewingRecipe read(Identifier id, JsonObject json) { SewingRecipeJsonFormat recipeJson = new Gson().fromJson(json, SewingRecipeJsonFormat.class); // Validate all fields are there if (recipeJson.item == null || recipeJson.dye == null || recipeJson.result == null) { throw new JsonSyntaxException("A required attribute is missing!"); } // We'll allow to not specify the output, and default it to 1. if (recipeJson.count == 0) recipeJson.count = 1; Ingredient inputA = Ingredient.fromJson(recipeJson.item); Ingredient inputB = Ingredient.fromJson(recipeJson.dye); Item outputItem = Registries.ITEM.getOrEmpty(new Identifier(recipeJson.result)) // Validate the inputted item actually exists .orElseThrow(() -> new JsonSyntaxException("No such item " + recipeJson.result)); ItemStack output = new ItemStack(outputItem, recipeJson.count); return new SewingRecipe(inputA, inputB, output, id); } @Override public SewingRecipe read(Identifier recipeId, PacketByteBuf packetData) { // Make sure the read in the same order you have written! Ingredient inputA = Ingredient.fromPacket(packetData); Ingredient inputB = Ingredient.fromPacket(packetData); ItemStack output = packetData.readItemStack(); return new SewingRecipe(inputA, inputB, output, recipeId); } @Override public void write(PacketByteBuf packetData, SewingRecipe recipe) { recipe.getItem().write(packetData); recipe.getDye().write(packetData); packetData.writeItemStack(recipe.getOutput(null)); } } @Override public RecipeSerializer getSerializer() { return SewingRecipeSerializer.INSTANCE; } }