/******************************************************************************* * Copyright (c) 2015, Superloup10 * * Wolf's Addons is distributed under the terms of the Minecraft Mod Public * License 1.0, or MMPL. Please check the contents of the license located in * https://www.dropbox.com/s/6l16rc7b1aks211/MMPL-1.0.txt ******************************************************************************/ package fr.wolf.addons.common.recipe; import fr.wolf.addons.common.item.WolfItemList; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import net.minecraft.block.Block; import net.minecraft.init.Items; 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; public final class RecipesCompressor { private static final RecipesCompressor instance = new RecipesCompressor(); private List recipes = new ArrayList(); private RecipesCompressor() { this.addRecipe(new ItemStack(WolfItemList.redstoneIngot, 1), new Object[] {"XXX", 'X', Items.redstone}); this.addRecipe(new ItemStack(WolfItemList.lapisIngot, 1), new Object[] {"XXX", 'X', new ItemStack(Items.dye, 1, 4)}); this.addRecipe(new ItemStack(WolfItemList.emeraldIngot, 1), new Object[] {"XXX", 'X', Items.emerald}); this.addRecipe(new ItemStack(WolfItemList.silverIngot, 1), new Object[] {"XXX", 'X', WolfItemList.silverDust}); Collections.sort(this.recipes, new RecipesSorterCompressor(this)); } public static RecipesCompressor getInstance() { return instance; } void addRecipe(ItemStack par1ItemStack, Object... par2ArrayOfObj) { String s = ""; int i = 0; int j = 0; int k = 0; if(par2ArrayOfObj[i] instanceof String[]) { String[] astring = ((String[])par2ArrayOfObj[i++]); for(int l = 0; l < astring.length; ++l) { String s1 = astring[l]; ++k; j = s1.length(); s = s + s1; } } else { while(par2ArrayOfObj[i] instanceof String) { String s2 = (String)par2ArrayOfObj[i++]; ++k; j = s2.length(); s = s + s2; } } HashMap hashmap; for(hashmap = new HashMap(); i < par2ArrayOfObj.length; i += 2) { Character character = (Character)par2ArrayOfObj[i]; ItemStack itemstack1 = null; if(par2ArrayOfObj[i + 1] instanceof Item) { itemstack1 = new ItemStack((Item)par2ArrayOfObj[i + 1]); } else if(par2ArrayOfObj[i + 1] instanceof Block) { itemstack1 = new ItemStack((Block)par2ArrayOfObj[i + 1], 1, 32767); } else if(par2ArrayOfObj[i + 1] instanceof ItemStack) { itemstack1 = (ItemStack)par2ArrayOfObj[i + 1]; } hashmap.put(character, itemstack1); } ItemStack[] aitemstack = new ItemStack[j * k]; for(int i1 = 0; i1 < j * k; ++i1) { char c0 = s.charAt(i1); if(hashmap.containsKey(Character.valueOf(c0))) { aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c0))).copy(); } else { aitemstack[i1] = null; } } this.recipes.add(new ShapedRecipesCompressor(j, k, aitemstack, par1ItemStack)); } void addShapelessRecipe(ItemStack par1ItemStack, Object... par2ArrayOfObj) { ArrayList arraylist = new ArrayList(); Object[] aobject = par2ArrayOfObj; int i = par2ArrayOfObj.length; for(int j = 0; j < i; ++j) { Object object1 = aobject[j]; if(object1 instanceof ItemStack) { arraylist.add(((ItemStack)object1).copy()); } else if(object1 instanceof Item) { arraylist.add(new ItemStack((Item)object1)); } else { if(!(object1 instanceof Block)) { throw new RuntimeException("Invalid shapeless recipy!"); } arraylist.add(new ItemStack((Block)object1)); } } this.recipes.add(new ShapelessRecipes(par1ItemStack, arraylist)); } public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World) { int i = 0; ItemStack itemstack = null; ItemStack itemstack1 = null; int j; for(j = 0; j < par1InventoryCrafting.getSizeInventory(); ++j) { ItemStack itemstack2 = par1InventoryCrafting.getStackInSlot(j); if(itemstack2 != null) { if(i == 0) { itemstack = itemstack2; } if(i == 1) { itemstack1 = itemstack2; } ++i; } } if(i == 2 && itemstack.getItem() == itemstack1.getItem() && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && itemstack.getItem().isRepairable()) { Item item = itemstack.getItem(); int j1 = item.getMaxDamage() - itemstack.getItemDamage(); int k = item.getMaxDamage() - itemstack1.getItemDamage(); int l = j1 + k + item.getMaxDamage() * 5 / 100; int i1 = item.getMaxDamage() - l; if(i1 < 0) { i1 = 0; } return new ItemStack(itemstack.getItem(), 1, i1); } else { for(j = 0; j < this.recipes.size(); ++j) { IRecipe irecipe = (IRecipe)this.recipes.get(j); if(irecipe.matches(par1InventoryCrafting, par2World)) { return irecipe.getCraftingResult(par1InventoryCrafting); } } return null; } } public List getRecipeList() { return this.recipes; } }