From 35ff26221ad76ba330db97f428050eb91e93e944 Mon Sep 17 00:00:00 2001 From: canitzp Date: Tue, 6 Oct 2020 09:04:43 +0200 Subject: [PATCH] Added CrusherRecipe and Factory Signed-off-by: canitzp --- .../common/recipes/CrusherRecipe.java | 66 +++++++++++++ .../common/recipes/CrusherRecipeFactory.java | 95 +++++++++++++++++++ .../actuallyadditions/recipes/_factories.json | 3 +- 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.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 new file mode 100644 index 000000000..3128ec5fd --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipe.java @@ -0,0 +1,66 @@ +package de.ellpeck.actuallyadditions.common.recipes; + +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.util.ResourceLocation; + +import javax.annotation.Nonnull; + +public class CrusherRecipe implements IDummyRecipe { + + public static final IRecipeType CRUSHER_RECIPE_TYPE = IRecipeType.register("actuallyadditions:crusher"); + + @Nonnull private final ResourceLocation recipeId; + + @Nonnull private final Ingredient input; + @Nonnull private final ItemStack output; + @Nonnull private final ItemStack secondaryOutput; + private final int outputChance; + + public CrusherRecipe(@Nonnull ResourceLocation recipeId, @Nonnull Ingredient input, @Nonnull ItemStack output, @Nonnull ItemStack secondaryOutput, int outputChance){ + this.recipeId = recipeId; + this.input = input; + this.output = output; + this.secondaryOutput = secondaryOutput; + this.outputChance = outputChance; + } + + @Nonnull + public Ingredient getInput(){ + return input; + } + + @Nonnull + public ItemStack getOutput(){ + return output; + } + + @Nonnull + public ItemStack getSecondaryOutput(){ + return secondaryOutput; + } + + public int getOutputChance(){ + return outputChance; + } + + @Nonnull + @Override + public ResourceLocation getId(){ + return this.recipeId; + } + + @Nonnull + @Override + public IRecipeSerializer getSerializer(){ + return CrusherRecipeFactory.INSTANCE; + } + + @Nonnull + @Override + public IRecipeType getType(){ + return CRUSHER_RECIPE_TYPE; + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java new file mode 100644 index 000000000..908dbd68d --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/CrusherRecipeFactory.java @@ -0,0 +1,95 @@ +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/resources/assets/actuallyadditions/recipes/_factories.json b/src/main/resources/assets/actuallyadditions/recipes/_factories.json index 1b1b86408..f589c7c26 100644 --- a/src/main/resources/assets/actuallyadditions/recipes/_factories.json +++ b/src/main/resources/assets/actuallyadditions/recipes/_factories.json @@ -3,6 +3,7 @@ "atomic_reconstructor": "de.ellpeck.actuallyadditions.common.recipes.AtomicReconstructorRecipeFactory", "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" + "compost": "de.ellpeck.actuallyadditions.common.recipes.CompostRecipeFactory", + "crusher": "de.eppleck.actuallyadditions.common.recipes.CrusherRecipeFactory" } } \ No newline at end of file