Snippets

Adam Walker Alloy Furnace

Created by Adam Walker
package com.hrznstudio.gadgetry.api;

import com.google.gson.JsonObject;
import com.hrznstudio.gadgetry.recipe.GadgetryRecipeSerializers;
import com.hrznstudio.gadgetry.recipe.GadgetryRecipeTypes;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.registry.IRegistry;
import net.minecraft.world.World;
import net.minecraftforge.common.crafting.RecipeType;

public class AlloyFurnaceRecipe implements IRecipe {
    private final ResourceLocation id;
    private final Ingredient input;
    private final Ingredient secondaryInput;
    private final ItemStack output;
    private final int cookingTime;

    public AlloyFurnaceRecipe(ResourceLocation id, Ingredient input, Ingredient secondaryInput, ItemStack output, int cookingTime) {
        this.id = id;
        this.input = input;
        this.secondaryInput = secondaryInput;
        this.output = output;
        this.cookingTime = cookingTime;
    }

    public Ingredient getInput() {
        return input;
    }

    public Ingredient getSecondaryInput() {
        return secondaryInput;
    }

    public int getCookingTime() {
        return cookingTime;
    }

    @Override
    public boolean matches(IInventory inv, World worldIn) {
        return input.test(inv.getStackInSlot(0)) && secondaryInput.test(inv.getStackInSlot(1));
    }

    @Override
    public ItemStack getCraftingResult(IInventory inv) {
        return output;
    }

    @Override
    public boolean canFit(int width, int height) {
        return width >= 2 || height >= 2;
    }

    @Override
    public ItemStack getRecipeOutput() {
        return output;
    }

    @Override
    public ResourceLocation getId() {
        return id;
    }

    @Override
    public IRecipeSerializer<?> getSerializer() {
        return GadgetryRecipeSerializers.ALLOY_FURNACE;
    }

    @Override
    public RecipeType<? extends IRecipe> getType() {
        return GadgetryRecipeTypes.ALLOY_FURNACE;
    }

    public static class Serializer implements IRecipeSerializer<AlloyFurnaceRecipe> {
        private static ResourceLocation NAME = new ResourceLocation("gadgetry", "alloy_furnace");

        public AlloyFurnaceRecipe read(ResourceLocation recipeId, JsonObject json) {
            String s = JsonUtils.getString(json, "group", "");
            Ingredient ingredient;
            if (JsonUtils.isJsonArray(json, "input")) {
                ingredient = Ingredient.fromJson(JsonUtils.getJsonArray(json, "input"));
            } else {
                ingredient = Ingredient.fromJson(JsonUtils.getJsonObject(json, "input"));
            }
            Ingredient ingredient2;
            if (JsonUtils.isJsonArray(json, "secondary")) {
                ingredient2 = Ingredient.fromJson(JsonUtils.getJsonArray(json, "secondary"));
            } else {
                ingredient2 = Ingredient.fromJson(JsonUtils.getJsonObject(json, "secondary"));
            }

            String s1 = JsonUtils.getString(json, "result");
            Item item = IRegistry.field_212630_s.func_212608_b(new ResourceLocation(s1));
            if (item != null) {
                ItemStack itemstack = new ItemStack(item);
                int lvt_9_1_ = JsonUtils.getInt(json, "cookingtime", 200);
                return new AlloyFurnaceRecipe(recipeId, ingredient, ingredient2, itemstack, lvt_9_1_);
            } else {
                throw new IllegalStateException(s1 + " did not exist");
            }
        }

        public AlloyFurnaceRecipe read(ResourceLocation recipeId, PacketBuffer buffer) {
            Ingredient ingredient = Ingredient.fromBuffer(buffer);
            Ingredient ingredient2 = Ingredient.fromBuffer(buffer);
            ItemStack itemstack = buffer.readItemStack();
            int i = buffer.readVarInt();
            return new AlloyFurnaceRecipe(recipeId, ingredient, ingredient2, itemstack, i);
        }

        public void write(PacketBuffer buffer, AlloyFurnaceRecipe recipe) {
            recipe.input.writeToBuffer(buffer);
            recipe.secondaryInput.writeToBuffer(buffer);
            buffer.writeItemStack(recipe.output);
            buffer.writeVarInt(recipe.cookingTime);
        }

        @Override
        public ResourceLocation getName() {
            return NAME;
        }
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.