package WayofTime.alchemicalWizardry.api.items; import java.util.ArrayList; import java.util.Map; import java.util.Map.Entry; import net.minecraft.block.Block; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraft.world.World; import net.minecraftforge.oredict.OreDictionary; import WayofTime.alchemicalWizardry.api.items.interfaces.IBloodOrb; /** * Shapeless Blood Orb Recipe Handler by joshie * */ public class ShapelessBloodOrbRecipe implements IRecipe { private final ItemStack output; private final ArrayList input = new ArrayList<>(); public ShapelessBloodOrbRecipe(Block result, Object... recipe) { this(new ItemStack(result), recipe); } public ShapelessBloodOrbRecipe(Item result, Object... recipe) { this(new ItemStack(result), recipe); } public ShapelessBloodOrbRecipe(ItemStack result, Object... recipe) { output = result.copy(); for (Object in : recipe) { switch (in) { case IBloodOrb orb -> input.add(orb.getOrbLevel()); case ItemStack stack when stack.getItem() instanceof IBloodOrb orb -> input.add(orb.getOrbLevel()); case Integer i -> input.add(i); case ItemStack stack -> input.add(stack.copy()); case Item item -> input.add(new ItemStack(item)); case Block block -> input.add(new ItemStack(block, 1, OreDictionary.WILDCARD_VALUE)); case String string -> input.add(OreDictionary.getOres(string)); case null, default -> { StringBuilder ret = new StringBuilder("Invalid shapeless ore recipe: "); for (Object tmp : recipe) { ret.append(tmp).append(", "); } ret.append(output); throw new RuntimeException(ret.toString()); } } } } ShapelessBloodOrbRecipe(ShapelessRecipes recipe, Map replacements) { output = recipe.getRecipeOutput(); for (ItemStack ingred : recipe.recipeItems) { Object finalObj = ingred; for (Entry replace : replacements.entrySet()) { if (OreDictionary.itemMatches(replace.getKey(), ingred, false)) { finalObj = OreDictionary.getOres(replace.getValue()); break; } } input.add(finalObj); } } @Override public int getRecipeSize() { return input.size(); } @Override public ItemStack getRecipeOutput() { return output; } @Override public ItemStack getCraftingResult(InventoryCrafting var1) { return output.copy(); } @SuppressWarnings("unchecked") @Override public boolean matches(InventoryCrafting inv, World world) { ArrayList required = new ArrayList<>(input); for (int x = 0; x < inv.getSizeInventory(); x++) { ItemStack slot = inv.getStackInSlot(x); if (slot != null) { boolean inRecipe = false; for (Object next : required) { boolean match = false; // If target is integer, then we should check the blood orb value of the item instead if (next instanceof Integer orbLevel) { if (!(slot.getItem() instanceof IBloodOrb orb)) { return false; } if (orb.getOrbLevel() < orbLevel) { return false; } match = true; } else if (next instanceof ItemStack item) { match = OreDictionary.itemMatches(item, slot, false); } else if (next instanceof ArrayList) { for (ItemStack item : ((ArrayList) next)) { if (OreDictionary.itemMatches(item, slot, false)) { match = true; break; } } } if (match) { inRecipe = true; required.remove(next); break; } } if (!inRecipe) { return false; } } } return required.isEmpty(); } public ArrayList getInput() { return this.input; } }