Skip to main content

getStackInSlot

multicraft.Crafting;

import com.google.common.collect.Lists;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import io.github.lukas2005.multicraft.items.ModItems;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.init.Items;
import net.minecraft.inventory.*;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.util.JsonUtils;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fluids.UniversalBucket;

import java.util.List;

public class ShapelessCrafting extends net.minecraftforge.registries.IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {
    /**
     * Is the ItemStack that you get when craft the recipe.
     */
    private final ItemStack recipeOutput;
    /**
     * Is a List of ItemStack that composes the recipe.
     */
    public final NonNullList<Ingredient> recipeItems;
    private final String group;
    private final boolean isSimple;

    int nbtCopyTargetSlot = -1;
    int toolDamageSlot = -1;
    private ItemStack itemstack;
    private BlockPos pos;


    public ShapelessCrafting(String group, ItemStack output, NonNullList<Ingredient> ingredients) {

        this.group = group;
        this.recipeOutput = output;
        this.recipeItems = ingredients;
        boolean simple = true;
        for (Ingredient i : ingredients)
            simple &= i.isSimple();
        this.isSimple = simple;
    }


    public ShapelessCrafting setNBTCopyTargetRecipe(int slot) {
        this.nbtCopyTargetSlot = slot;
        return this;
    }

    public ShapelessCrafting setToolDamageRecipe(int slot) {
        this.toolDamageSlot = slot;
        return this;
    }

    /**
     * Recipes with equal group are combined into one button in the recipe book
     */
    public String getGroup() {
        return this.group;
    }

    /**
     * Get the result of this recipe, usually for display purposes (e.g. recipe book). If your recipe has more than one
     * possible result (e.g. it's dynamic and depends on its inputs), then return an empty stack.
     */
    public ItemStack getRecipeOutput() {
        /*if (itemstack == new ItemStack(Items.PAPER, 3)) {
            Minecraft.getMinecraft().world.spawnEntity(new EntityItem(Minecraft.getMinecraft().world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(ModItems.LEATHER_SCRAP, 2, 0)));
        }*/
        return this.recipeOutput;
    }

    public NonNullList<Ingredient> getIngredients() {
        return this.recipeItems;
    }

    @Override
    public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
        NonNullList<ItemStack> remains = getRemainingItems(inv);
        for (int i = 0; i < remains.size(); i++) {
            ItemStack s = inv.getStackInSlot(i);
            ItemStack remain = remains.get(i);
            if (toolDamageSlot >= 0 && toolDamageSlot < getIngredients().size()) {
                ItemStack tool = ItemStack.EMPTY;
                if (remain.isEmpty() && !s.isEmpty() && getIngredients().get(toolDamageSlot).apply(s))
                    tool = s.copy();
                else if (!remain.isEmpty() && getIngredients().get(toolDamageSlot).apply(remain))
                    tool = remain;
                if (!tool.isEmpty() && tool.isItemStackDamageable()) {
                    tool.setItemDamage(tool.getItemDamage() + 1);
                    if (tool.getItemDamage() > tool.getMaxDamage())
                        tool = ItemStack.EMPTY;
                    remains.set(i, tool);
                }
            }
            if (!s.isEmpty() && remain.isEmpty() && s.getItem() instanceof UniversalBucket) {
                ItemStack empty = ((UniversalBucket) s.getItem()).getEmpty();
                if (!empty.isEmpty())
                    remains.set(i, empty.copy());
            }
        }
        return remains;
    }

    /**
     * Used to check if a recipe matches current crafting inventory
     */
    public boolean matches(InventoryCrafting inv, World worldIn) {
        int ingredientCount = 0;
        net.minecraft.client.util.RecipeItemHelper recipeItemHelper = new net.minecraft.client.util.RecipeItemHelper();
        List<ItemStack> inputs = Lists.newArrayList();

        for (int i = 0; i < inv.getHeight(); ++i) {
            for (int j = 0; j < inv.getWidth(); ++j) {
                ItemStack itemstack = inv.getStackInRowAndColumn(j, i);

                if (!itemstack.isEmpty()) {
                    ++ingredientCount;
                    if (this.isSimple)
                        recipeItemHelper.accountStack(itemstack, 1);
                    else
                        inputs.add(itemstack);
                }
            }
        }

        if (ingredientCount != this.recipeItems.size())
            return false;

        if (this.isSimple)
            return recipeItemHelper.canCraft(this, null);

        return net.minecraftforge.common.util.RecipeMatcher.findMatches(inputs, this.recipeItems) != null;
    }

    /**
     * Returns an Item that is the result of this recipe
     */
    public ItemStack getCraftingResult(InventoryCrafting inv) {
        return this.recipeOutput.copy();
    }

    public static ShapelessRecipes deserialize(JsonObject json) {
        String s = JsonUtils.getString(json, "group", "");
        NonNullList<Ingredient> nonnulllist = deserializeIngredients(JsonUtils.getJsonArray(json, "ingredients"));

        if (nonnulllist.isEmpty()) {
            throw new JsonParseException("No ingredients for shapeless recipe");
        } else if (nonnulllist.size() > 9) {
            throw new JsonParseException("Too many ingredients for shapeless recipe");
        } else {
            ItemStack itemstack = ShapedRecipes.deserializeItem(JsonUtils.getJsonObject(json, "result"), true);
            return new ShapelessRecipes(s, itemstack, nonnulllist);
        }
    }

    private static NonNullList<Ingredient> deserializeIngredients(JsonArray array) {