package WayofTime.alchemicalWizardry.common.compress; import java.util.LinkedList; import java.util.List; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.inventory.Container; import net.minecraft.inventory.InventoryCrafting; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.item.crafting.CraftingManager; import net.minecraft.item.crafting.IRecipe; import net.minecraft.item.crafting.ShapedRecipes; import net.minecraft.item.crafting.ShapelessRecipes; import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import WayofTime.alchemicalWizardry.AlchemicalWizardry; import WayofTime.alchemicalWizardry.common.spell.complex.effect.SpellHelper; public class StorageBlockCraftingManager { private static final StorageBlockCraftingManager instance = new StorageBlockCraftingManager(); private List recipes = new LinkedList(); public static StorageBlockCraftingManager getInstance() { return instance; } public void addStorageBlockRecipes() { List list = CraftingManager.getInstance().getRecipeList(); List tempRecipeList = new LinkedList(); World world = DimensionManager.getWorld(0); for(Object obj : list) { if(obj instanceof IRecipe) { IRecipe recipe = (IRecipe)obj; ItemStack outputStack = recipe.getRecipeOutput(); if(outputStack == null || outputStack.getItem() == null) { continue; } if(recipe instanceof ShapedRecipes) { ShapedRecipes sRecipe = (ShapedRecipes)recipe; ItemStack[] input = sRecipe.recipeItems; if(outputStack.stackSize == 1 && (input.length == 9 || input.length == 4)) { tempRecipeList.add(recipe); }else if((outputStack.stackSize == 9 || outputStack.stackSize == 4) && input.length == 1) { tempRecipeList.add(recipe); } } else if(recipe instanceof ShapelessRecipes) { ShapelessRecipes sRecipe = (ShapelessRecipes)recipe; List input = sRecipe.recipeItems; if(outputStack.stackSize == 1 && (input.size() == 9 || input.size() == 4)) { Object obj1 = input.get(0); if(obj1 != null) { boolean allMatch = true; for(Object obj2 : input) { if(obj2 == null || !obj2.equals(obj1)) { allMatch = false; break; } } if(allMatch) { tempRecipeList.add(recipe); } } }else if((outputStack.stackSize == 9 || outputStack.stackSize == 4) && input.size() == 1) { tempRecipeList.add(recipe); } } else if((outputStack.stackSize == 1 && (recipe.getRecipeSize() == 9 || recipe.getRecipeSize() == 4)) || ((outputStack.stackSize == 9 || outputStack.stackSize == 4) && recipe.getRecipeSize() == 1)) { tempRecipeList.add(recipe); continue; } } } for(Object obj : tempRecipeList) { if(obj instanceof IRecipe) { IRecipe recipe = (IRecipe)obj; ItemStack outputStack = recipe.getRecipeOutput(); if(outputStack == null || outputStack.getItem() == null) { continue; } if(isResultStackReversible(outputStack, 2, world, tempRecipeList) || isResultStackReversible(outputStack, 3, world, tempRecipeList)) { recipes.add(recipe); AlchemicalWizardry.logger.info("Now adding recipe for " + outputStack + " to the compression handler."); } } } } public static boolean isResultStackReversible(ItemStack stack, int gridSize, World world, List list) { if(stack == null) { return false; } InventoryCrafting inventory = new InventoryCrafting(new Container() { public boolean canInteractWith(EntityPlayer player) { return false; } }, 2, 2); inventory.setInventorySlotContents(0, stack); ItemStack returnStack = StorageBlockCraftingManager.getInstance().findMatchingRecipe(inventory, world, list); if(returnStack == null || returnStack.getItem() == null) { return false; } ItemStack compressedStack = null; switch(gridSize) { case 2: compressedStack = get22Recipe(returnStack, world, list); break; case 3: compressedStack = get33Recipe(returnStack, world, list); break; } if(compressedStack == null) { return false; }else { return SpellHelper.areItemStacksEqual(stack, compressedStack); } } public static ItemStack getRecipe(ItemStack stack, World world, int gridSize, List list) { InventoryCrafting inventory = new InventoryCrafting(new Container() { public boolean canInteractWith(EntityPlayer player) { return false; } }, gridSize, gridSize); for(int i=0; i