From 2c57a7641a7d57c00113d16a6684464782cc933a Mon Sep 17 00:00:00 2001 From: canitzp Date: Fri, 25 Sep 2020 08:30:38 +0200 Subject: [PATCH] Ball of Fur Recipe Factory --- .../common/recipes/BallOfFurRecipe.java | 45 +++++++++++++ .../recipes/BallOfFurRecipeFactory.java | 65 +++++++++++++++++++ .../actuallyadditions/recipes/_factories.json | 1 + 3 files changed, 111 insertions(+) create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipe.java create mode 100644 src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipeFactory.java diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipe.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipe.java new file mode 100644 index 000000000..c938bbcb7 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipe.java @@ -0,0 +1,45 @@ +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.util.ResourceLocation; + +public class BallOfFurRecipe implements IDummyRecipe { + + public static final IRecipeType BALL_OF_FUR_RECIPE_TYPE = IRecipeType.register("actuallyadditions:ball_of_fur"); + + private final ResourceLocation recipeId; + + private final ItemStack returnItem; + private final int chance; + + public BallOfFurRecipe(ResourceLocation recipeId, ItemStack returnItem, int chance){ + this.recipeId = recipeId; + this.returnItem = returnItem; + this.chance = chance; + } + + public ItemStack getReturnItem(){ + return this.returnItem; + } + + public int getChance(){ + return this.chance; + } + + @Override + public ResourceLocation getId(){ + return this.recipeId; + } + + @Override + public IRecipeSerializer getSerializer(){ + return BallOfFurRecipeFactory.INSTANCE; + } + + @Override + public IRecipeType getType(){ + return BALL_OF_FUR_RECIPE_TYPE; + } +} diff --git a/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipeFactory.java b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipeFactory.java new file mode 100644 index 000000000..e713b5b33 --- /dev/null +++ b/src/main/java/de/ellpeck/actuallyadditions/common/recipes/BallOfFurRecipeFactory.java @@ -0,0 +1,65 @@ +package de.ellpeck.actuallyadditions.common.recipes; + +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.IRecipeSerializer; +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; +import javax.annotation.Nullable; + +public class BallOfFurRecipeFactory extends ForgeRegistryEntry> implements IRecipeSerializer { + + public static final BallOfFurRecipeFactory INSTANCE = IRecipeSerializer.register("actuallyadditions:ball_of_fur", new BallOfFurRecipeFactory()); + + @Nonnull + @Override + public BallOfFurRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull JsonObject json){ + ItemStack stack; + int chance; + + if(json.has("stack")){ + JsonElement stackJsonElement = json.get("stack"); + if(stackJsonElement.isJsonObject()){ + stack = ShapedRecipe.deserializeItem(stackJsonElement.getAsJsonObject()); + } else { + throw new JsonParseException("BallOfFurRecipe stack has to be a json object!"); + } + } else { + throw new JsonParseException("BallOfFurRecipe has no json stack object!"); + } + + if(json.has("chance")){ + JsonElement chanceJsonElement = json.get("chance"); + if(chanceJsonElement.isJsonPrimitive()){ + chance = chanceJsonElement.getAsInt(); + } else { + throw new JsonParseException("BallOfFurRecipe chance is not a json integer!"); + } + } else { + throw new JsonParseException("BallOfFurRecipe has no chance integer!"); + } + + return new BallOfFurRecipe(recipeId, stack, chance); + } + + @Nullable + @Override + public BallOfFurRecipe read(@Nonnull ResourceLocation recipeId, @Nonnull PacketBuffer buffer){ + ItemStack stack = buffer.readItemStack(); + int chance = buffer.readInt(); + return new BallOfFurRecipe(recipeId, stack, chance); + } + + @Override + public void write(@Nonnull PacketBuffer buffer, @Nonnull BallOfFurRecipe recipe){ + buffer.writeItemStack(recipe.getReturnItem()); + buffer.writeInt(recipe.getChance()); + } + +} diff --git a/src/main/resources/assets/actuallyadditions/recipes/_factories.json b/src/main/resources/assets/actuallyadditions/recipes/_factories.json index ea2e4d9c8..0da2e2c46 100644 --- a/src/main/resources/assets/actuallyadditions/recipes/_factories.json +++ b/src/main/resources/assets/actuallyadditions/recipes/_factories.json @@ -1,6 +1,7 @@ { "recipes": { "atomic_reconstructor": "de.ellpeck.actuallyadditions.recipes.AtomicReconstructorRecipeFactory", + "ball_of_fur": "de.ellpeck.actuallyadditions.recipes.BallOfFurRecipeFactory", "coffee_machine_ingredient": "de.ellpeck.actuallyadditions.recipes.CoffeeMachineIngredientFactory" } } \ No newline at end of file