package com.zivilon.cinder_loe; import java.util.List; import java.util.ArrayList; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraft.world.World; import net.minecraft.item.crafting.IRecipe; public class ShapelessDurabilityRecipe implements IRecipe { private final ItemStack recipeOutput; private final List recipeItems; private final Item toolItem; public ShapelessDurabilityRecipe(ItemStack result, Item toolItem, ItemStack... ingredients) { this.recipeOutput = result; this.recipeItems = new ArrayList<>(ingredients.length); for (ItemStack ingredient : ingredients) { this.recipeItems.add(ingredient); } this.toolItem = toolItem; System.out.println("[CinderLoE] Created ShapelessDurabilityRecipe for: " + result.getUnlocalizedName()); System.out.println("[CinderLoE] Ingredients: " + this.recipeItems); } @Override public boolean matches(InventoryCrafting inv, World worldIn) { System.out.println("[CinderLoE] Checking matches for ShapelessDurabilityRecipe"); boolean hasTool = false; List ingredientsCopy = new ArrayList<>(this.recipeItems); for (int i = 0; i < inv.getSizeInventory(); ++i) { ItemStack stack = inv.getStackInSlot(i); if (stack != null) { if (stack.getItem() == toolItem) { hasTool = true; } else { boolean matched = false; for (ItemStack recipeStack : ingredientsCopy) { if (stack.getItem() == recipeStack.getItem() && (recipeStack.getItemDamage() == 32767 || stack.getItemDamage() == recipeStack.getItemDamage())) { matched = true; ingredientsCopy.remove(recipeStack); break; } } if (!matched) { System.out.println("[CinderLoE] Ingredient did not match: " + stack); return false; } } } } boolean matches = hasTool && ingredientsCopy.isEmpty(); System.out.println("[CinderLoE] ShapelessDurabilityRecipe match result: " + matches + ", has tool: " + hasTool); return matches; } @Override public ItemStack getCraftingResult(InventoryCrafting inv) { System.out.println("[CinderLoE] Getting crafting result for ShapelessDurabilityRecipe"); return this.recipeOutput.copy(); } @Override public int getRecipeSize() { return this.recipeItems.size(); } @Override public ItemStack getRecipeOutput() { return this.recipeOutput; } public ItemStack[] getRemainingItems(InventoryCrafting inv) { System.out.println("[CinderLoE] Getting remaining items for ShapelessDurabilityRecipe"); ItemStack[] remainingItems = new ItemStack[inv.getSizeInventory()]; for (int i = 0; i < remainingItems.length; ++i) { ItemStack itemstack = inv.getStackInSlot(i); if (itemstack != null && itemstack.getItem() == toolItem) { ItemStack tool = itemstack.copy(); tool.setItemDamage(tool.getItemDamage() + 1); System.out.println("[CinderLoE] Damaging tool: " + tool.getUnlocalizedName() + " | New Damage: " + tool.getItemDamage()); if (tool.getItemDamage() >= tool.getMaxDamage()) { System.out.println("[CinderLoE] Tool is out of durability, breaking: " + tool.getUnlocalizedName()); tool = null; } remainingItems[i] = tool; } else if (itemstack != null && itemstack.getItem().hasContainerItem(itemstack)) { System.out.println("[CinderLoE] Consuming non-tool item " + itemstack.getUnlocalizedName()); remainingItems[i] = itemstack.getItem().getContainerItem(itemstack); } else { remainingItems[i] = itemstack; } } return remainingItems; } }