From 395b6d0491d505c6dba55ac57a84767d7e3a2663 Mon Sep 17 00:00:00 2001 From: canitzp Date: Tue, 6 Oct 2020 12:10:09 +0200 Subject: [PATCH] Recipe Factory changes Signed-off-by: canitzp --- .../common/recipes/CrusherRecipe.java | 56 ++++++++++- .../common/recipes/CrusherRecipeFactory.java | 95 ------------------- .../common/recipes/RecipeFactoryBase.java | 82 ++++++++++++++++ .../actuallyadditions/recipes/_factories.json | 2 +- 4 files changed, 136 insertions(+), 99 deletions(-) delete mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/RecipeFactoryBase.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java index 3128ec5fd..0365a18da 100644 --- a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java @@ -1,16 +1,21 @@ package de.ellpeck.actuallyadditions.common.recipes; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipeSerializer; import net.minecraft.item.crafting.IRecipeType; import net.minecraft.item.crafting.Ingredient; +import net.minecraft.network.PacketBuffer; import net.minecraft.util.ResourceLocation; import javax.annotation.Nonnull; public class CrusherRecipe implements IDummyRecipe { - public static final IRecipeType CRUSHER_RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher"); + public static final IRecipeType RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher"); + public static final CrusherRecipeFactory FACTORY = IRecipeSerializer.register("actuallyadditions:crusher", new CrusherRecipeFactory()); @Nonnull private final ResourceLocation recipeId; @@ -55,12 +60,57 @@ public class CrusherRecipe implements IDummyRecipe { @Nonnull @Override public IRecipeSerializer getSerializer(){ - return CrusherRecipeFactory.INSTANCE; + return FACTORY; } @Nonnull @Override public IRecipeType getType(){ - return CRUSHER_RECIPE_TYPE; + return RECIPE_TYPE; } + + static class CrusherRecipeFactory extends RecipeFactoryBase { + + @Nonnull + @Override + public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json){ + Ingredient input = this.readIngredient(json, "input"); + ItemStack firstOutput = this.readItemStack(json, "output"); + ItemStack secondOutput = ItemStack.EMPTY; + int secondaryOutputChance = 0; + + if(json.has("secondary")){ // Optional + JsonElement secondaryElement = json.get("secondary"); + if(secondaryElement.isJsonObject()){ + JsonObject secondary = secondaryElement.getAsJsonObject(); + secondOutput = this.readItemStack(secondary, "output", ItemStack.EMPTY); + secondaryOutputChance = this.readInt(secondary, "chance", 0); + } else { + throw new JsonSyntaxException("Secondary is not valid!"); + } + } + + return new CrusherRecipe(recipeId, input, firstOutput, secondOutput, secondaryOutputChance); + } + + @Nonnull + @Override + public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer){ + Ingredient input = Ingredient.read(buffer); + ItemStack output = buffer.readItemStack(); + ItemStack secondaryOutput = buffer.readItemStack(); + int chance = buffer.readVarInt(); + + return new CrusherRecipe(recipeId, input, output, secondaryOutput, chance); + } + + @Override + public void write(@Nonnull PacketBuffer buffer, @Nonnull CrusherRecipe recipe){ + recipe.getInput().write(buffer); + buffer.writeItemStack(recipe.getOutput()); + buffer.writeItemStack(recipe.getSecondaryOutput()); + buffer.writeVarInt(recipe.getOutputChance()); + } + } + } diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java deleted file mode 100644 index 908dbd68d..000000000 --- a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java +++ /dev/null @@ -1,95 +0,0 @@ -package de.ellpeck.actuallyadditions.common.recipes; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSyntaxException; -import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.IRecipeSerializer; -import net.minecraft.item.crafting.Ingredient; -import net.minecraft.item.crafting.ShapedRecipe; -import net.minecraft.network.PacketBuffer; -import net.minecraft.util.ResourceLocation; -import net.minecraftforge.registries.ForgeRegistryEntry; - -import javax.annotation.Nonnull; - -public class CrusherRecipeFactory extends ForgeRegistryEntry> implements IRecipeSerializer { - - public static final CrusherRecipeFactory INSTANCE = IRecipeSerializer.register("actuallyadditions:crusher", new CrusherRecipeFactory()); - - @Nonnull - @Override - public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json){ - Ingredient input; - ItemStack firstOutput; - ItemStack secondOutput = ItemStack.EMPTY; - int secondaryOutputChance = 0; - - if(json.has("input")){ - input = Ingredient.deserialize(json.get("input")); - } else { - throw new JsonSyntaxException("Input is not given for the recipe!"); - } - - if(json.has("output")){ - JsonElement outputElement = json.get("output"); - if(outputElement.isJsonObject()){ - firstOutput = ShapedRecipe.deserializeItem(outputElement.getAsJsonObject()); - } else { - throw new JsonSyntaxException("Output is not valid!"); - } - } else { - throw new JsonSyntaxException("Output is not given for the recipe!"); - } - - if(json.has("secondary")){ // Optional - JsonElement secondaryElement = json.get("secondary"); - if(secondaryElement.isJsonObject()){ - JsonObject secondary = secondaryElement.getAsJsonObject(); - if(secondary.has("output")){ - JsonElement outputElement = json.get("output"); - if(outputElement.isJsonObject()){ - secondOutput = ShapedRecipe.deserializeItem(outputElement.getAsJsonObject()); - } else { - throw new JsonSyntaxException("Secondary output is not valid!"); - } - } else { - throw new JsonSyntaxException("Secondary output is not given for the recipe!"); - } - if(secondary.has("chance")){ - JsonElement chanceElement = secondary.get("chance"); - if(chanceElement.isJsonPrimitive()){ - secondaryOutputChance = chanceElement.getAsInt(); - } else { - throw new JsonSyntaxException("Secondary chance is not valid!"); - } - } else { - throw new JsonSyntaxException("Secondary chance is not given for the recipe!"); - } - } else { - throw new JsonSyntaxException("Secondary is not valid!"); - } - } - - return new CrusherRecipe(recipeId, input, firstOutput, secondOutput, secondaryOutputChance); - } - - @Nonnull - @Override - public CrusherRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer){ - Ingredient input = Ingredient.read(buffer); - ItemStack output = buffer.readItemStack(); - ItemStack secondaryOutput = buffer.readItemStack(); - int chance = buffer.readVarInt(); - - return new CrusherRecipe(recipeId, input, output, secondaryOutput, chance); - } - - @Override - public void write(@Nonnull PacketBuffer buffer, @Nonnull CrusherRecipe recipe){ - recipe.getInput().write(buffer); - buffer.writeItemStack(recipe.getOutput()); - buffer.writeItemStack(recipe.getSecondaryOutput()); - buffer.writeVarInt(recipe.getOutputChance()); - } -} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/RecipeFactoryBase.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/RecipeFactoryBase.java new file mode 100644 index 000000000..56c3c8d30 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/RecipeFactoryBase.java @@ -0,0 +1,82 @@ +package de.ellpeck.actuallyadditions.common.recipes; + +import com.google.gson.JsonObject; +import com.google.gson.JsonSyntaxException; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipeSerializer; +import net.minecraft.item.crafting.Ingredient; +import net.minecraft.item.crafting.ShapedRecipe; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.registries.ForgeRegistries; +import net.minecraftforge.registries.ForgeRegistryEntry; + +public abstract class RecipeFactoryBase extends ForgeRegistryEntry> implements IRecipeSerializer { + + protected Ingredient readIngredient(JsonObject json, String key){ + if(json.has(key)){ + return Ingredient.deserialize(json.get(key)); + } else { + throw new JsonSyntaxException(String.format("Json element does not contain any element with the key: '%s'", key)); + } + } + + protected Ingredient readIngredient(JsonObject json, String key, Ingredient alternative){ + try{ + return this.readIngredient(json, key); + } catch(JsonSyntaxException e){ + return alternative; + } + } + + protected ItemStack readItemStack(JsonObject json, String key){ + if(json.has(key)){ + if(json.get(key).isJsonObject()){ + return ShapedRecipe.deserializeItem(json.get(key).getAsJsonObject()); + } else if(json.get(key).isJsonPrimitive()){ + ResourceLocation itemKey = new ResourceLocation(json.get(key).getAsString()); + if(ForgeRegistries.ITEMS.containsKey(itemKey)){ + return new ItemStack(ForgeRegistries.ITEMS.getValue(itemKey)); + } else { + throw new JsonSyntaxException(String.format("Item with the key: '%s' is not registered!", key)); + } + } else { + throw new JsonSyntaxException(String.format("Json element with the key: '%s' is neither a object nor a string!", key)); + } + } else { + throw new JsonSyntaxException(String.format("Json element does not contain any element with the key: '%s'!", key)); + } + } + + protected ItemStack readItemStack(JsonObject json, String key, ItemStack alternative){ + try{ + return this.readItemStack(json, key); + } catch(JsonSyntaxException e){ + return alternative; + } + } + + protected int readInt(JsonObject json, String key){ + if(json.has(key)){ + if(json.get(key).isJsonPrimitive()){ + try{ + return json.get(key).getAsJsonPrimitive().getAsInt(); + } catch(NumberFormatException e){ + throw new JsonSyntaxException(String.format("Json element '%s' is not a valid integer!", key), e); + } + } else { + throw new JsonSyntaxException(String.format("Json element '%s' is not a valid integer!", key)); + } + } else { + throw new JsonSyntaxException(String.format("Json element '%s' does not exist!", key)); + } + } + + protected int readInt(JsonObject json, String key, int alternative){ + try{ + return this.readInt(json, key); + } catch(Exception e){ + return alternative; + } + } + +} \ No newline at end of file diff --git a/src/main/resources/assets/actuallyadditions/recipes/_factories.json b/src/main/resources/assets/actuallyadditions/recipes/_factories.json index f589c7c26..ef0e56c52 100644 --- a/src/main/resources/assets/actuallyadditions/recipes/_factories.json +++ b/src/main/resources/assets/actuallyadditions/recipes/_factories.json @@ -4,6 +4,6 @@ "ball_of_fur": "de.ellpeck.actuallyadditions.common.recipes.BallOfFurRecipeFactory", "coffee_machine_ingredient": "de.ellpeck.actuallyadditions.common.recipes.CoffeeMachineIngredientFactory", "compost": "de.ellpeck.actuallyadditions.common.recipes.CompostRecipeFactory", - "crusher": "de.eppleck.actuallyadditions.common.recipes.CrusherRecipeFactory" + "crusher": "de.ellpeck.actuallyadditions.common.recipes.CrusherRecipe$CrusherRecipeFactory" } } \ No newline at end of file