    001package net.minecraft.item.crafting;
    002
    003import java.util.ArrayList;
    004import java.util.Collections;
    005import java.util.HashMap;
    006import java.util.List;
    007import net.minecraft.block.Block;
    008import net.minecraft.inventory.InventoryCrafting;
    009import net.minecraft.item.Item;
    010import net.minecraft.item.ItemStack;
    011import net.minecraft.world.World;
    012
    013public class CraftingManager
    014{
    015    /** The static instance of this class */
    016    private static final CraftingManager instance = new CraftingManager();
    017
    018    /** A list of all the recipes added */
    019    private List recipes = new ArrayList();
    020
    021    /**
    022     * Returns the static instance of this class
    023     */
    024    public static final CraftingManager getInstance()
    025    {
    026        return instance;
    027    }
    028
    029    private CraftingManager()
    030    {
    031        (new RecipesTools()).addRecipes(this);
    032        (new RecipesWeapons()).addRecipes(this);
    033        (new RecipesIngots()).addRecipes(this);
    034        (new RecipesFood()).addRecipes(this);
    035        (new RecipesCrafting()).addRecipes(this);
    036        (new RecipesArmor()).addRecipes(this);
    037        (new RecipesDyes()).addRecipes(this);
    038        this.recipes.add(new RecipesArmorDyes());
    039        this.recipes.add(new RecipesMapCloning());
    040        this.recipes.add(new RecipesMapExtending());
    041        this.recipes.add(new RecipeFireworks());
    042        this.addRecipe(new ItemStack(Item.paper, 3), new Object[] {"###", '#', Item.reed});
    043        this.addShapelessRecipe(new ItemStack(Item.book, 1), new Object[] {Item.paper, Item.paper, Item.paper, Item.leather});
    044        this.addShapelessRecipe(new ItemStack(Item.writableBook, 1), new Object[] {Item.book, new ItemStack(Item.dyePowder, 1, 0), Item.feather});
    045        this.addRecipe(new ItemStack(Block.fence, 2), new Object[] {"###", "###", '#', Item.stick});
    046        this.addRecipe(new ItemStack(Block.cobblestoneWall, 6, 0), new Object[] {"###", "###", '#', Block.cobblestone});
    047        this.addRecipe(new ItemStack(Block.cobblestoneWall, 6, 1), new Object[] {"###", "###", '#', Block.cobblestoneMossy});
    048        this.addRecipe(new ItemStack(Block.netherFence, 6), new Object[] {"###", "###", '#', Block.netherBrick});
    049        this.addRecipe(new ItemStack(Block.fenceGate, 1), new Object[] {"#W#", "#W#", '#', Item.stick, 'W', Block.planks});
    050        this.addRecipe(new ItemStack(Block.jukebox, 1), new Object[] {"###", "#X#", "###", '#', Block.planks, 'X', Item.diamond});
    051        this.addRecipe(new ItemStack(Block.music, 1), new Object[] {"###", "#X#", "###", '#', Block.planks, 'X', Item.redstone});
    052        this.addRecipe(new ItemStack(Block.bookShelf, 1), new Object[] {"###", "XXX", "###", '#', Block.planks, 'X', Item.book});
    053        this.addRecipe(new ItemStack(Block.blockSnow, 1), new Object[] {"##", "##", '#', Item.snowball});
    054        this.addRecipe(new ItemStack(Block.snow, 6), new Object[] {"###", '#', Block.blockSnow});
    055        this.addRecipe(new ItemStack(Block.blockClay, 1), new Object[] {"##", "##", '#', Item.clay});
    056        this.addRecipe(new ItemStack(Block.brick, 1), new Object[] {"##", "##", '#', Item.brick});
    057        this.addRecipe(new ItemStack(Block.glowStone, 1), new Object[] {"##", "##", '#', Item.lightStoneDust});
    058        this.addRecipe(new ItemStack(Block.blockNetherQuartz, 1), new Object[] {"##", "##", '#', Item.netherQuartz});
    059        this.addRecipe(new ItemStack(Block.cloth, 1), new Object[] {"##", "##", '#', Item.silk});
    060        this.addRecipe(new ItemStack(Block.tnt, 1), new Object[] {"X#X", "#X#", "X#X", 'X', Item.gunpowder, '#', Block.sand});
    061        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 3), new Object[] {"###", '#', Block.cobblestone});
    062        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 0), new Object[] {"###", '#', Block.stone});
    063        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 1), new Object[] {"###", '#', Block.sandStone});
    064        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 4), new Object[] {"###", '#', Block.brick});
    065        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 5), new Object[] {"###", '#', Block.stoneBrick});
    066        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 6), new Object[] {"###", '#', Block.netherBrick});
    067        this.addRecipe(new ItemStack(Block.stoneSingleSlab, 6, 7), new Object[] {"###", '#', Block.blockNetherQuartz});
    068        this.addRecipe(new ItemStack(Block.woodSingleSlab, 6, 0), new Object[] {"###", '#', new ItemStack(Block.planks, 1, 0)});
    069        this.addRecipe(new ItemStack(Block.woodSingleSlab, 6, 2), new Object[] {"###", '#', new ItemStack(Block.planks, 1, 2)});
    070        this.addRecipe(new ItemStack(Block.woodSingleSlab, 6, 1), new Object[] {"###", '#', new ItemStack(Block.planks, 1, 1)});
    071        this.addRecipe(new ItemStack(Block.woodSingleSlab, 6, 3), new Object[] {"###", '#', new ItemStack(Block.planks, 1, 3)});
    072        this.addRecipe(new ItemStack(Block.ladder, 3), new Object[] {"# #", "###", "# #", '#', Item.stick});
    073        this.addRecipe(new ItemStack(Item.doorWood, 1), new Object[] {"##", "##", "##", '#', Block.planks});
    074        this.addRecipe(new ItemStack(Block.trapdoor, 2), new Object[] {"###", "###", '#', Block.planks});
    075        this.addRecipe(new ItemStack(Item.doorIron, 1), new Object[] {"##", "##", "##", '#', Item.ingotIron});
    076        this.addRecipe(new ItemStack(Item.sign, 3), new Object[] {"###", "###", " X ", '#', Block.planks, 'X', Item.stick});
    077        this.addRecipe(new ItemStack(Item.cake, 1), new Object[] {"AAA", "BEB", "CCC", 'A', Item.bucketMilk, 'B', Item.sugar, 'C', Item.wheat, 'E', Item.egg});
    078        this.addRecipe(new ItemStack(Item.sugar, 1), new Object[] {"#", '#', Item.reed});
    079        this.addRecipe(new ItemStack(Block.planks, 4, 0), new Object[] {"#", '#', new ItemStack(Block.wood, 1, 0)});
    080        this.addRecipe(new ItemStack(Block.planks, 4, 1), new Object[] {"#", '#', new ItemStack(Block.wood, 1, 1)});
    081        this.addRecipe(new ItemStack(Block.planks, 4, 2), new Object[] {"#", '#', new ItemStack(Block.wood, 1, 2)});
    082        this.addRecipe(new ItemStack(Block.planks, 4, 3), new Object[] {"#", '#', new ItemStack(Block.wood, 1, 3)});
    083        this.addRecipe(new ItemStack(Item.stick, 4), new Object[] {"#", "#", '#', Block.planks});
    084        this.addRecipe(new ItemStack(Block.torchWood, 4), new Object[] {"X", "#", 'X', Item.coal, '#', Item.stick});
    085        this.addRecipe(new ItemStack(Block.torchWood, 4), new Object[] {"X", "#", 'X', new ItemStack(Item.coal, 1, 1), '#', Item.stick});
    086        this.addRecipe(new ItemStack(Item.bowlEmpty, 4), new Object[] {"# #", " # ", '#', Block.planks});
    087        this.addRecipe(new ItemStack(Item.glassBottle, 3), new Object[] {"# #", " # ", '#', Block.glass});
    088        this.addRecipe(new ItemStack(Block.rail, 16), new Object[] {"X X", "X#X", "X X", 'X', Item.ingotIron, '#', Item.stick});
    089        this.addRecipe(new ItemStack(Block.railPowered, 6), new Object[] {"X X", "X#X", "XRX", 'X', Item.ingotGold, 'R', Item.redstone, '#', Item.stick});
    090        this.addRecipe(new ItemStack(Block.railActivator, 6), new Object[] {"XSX", "X#X", "XSX", 'X', Item.ingotIron, '#', Block.torchRedstoneActive, 'S', Item.stick});
    091        this.addRecipe(new ItemStack(Block.railDetector, 6), new Object[] {"X X", "X#X", "XRX", 'X', Item.ingotIron, 'R', Item.redstone, '#', Block.pressurePlateStone});
    092        this.addRecipe(new ItemStack(Item.minecartEmpty, 1), new Object[] {"# #", "###", '#', Item.ingotIron});
    093        this.addRecipe(new ItemStack(Item.cauldron, 1), new Object[] {"# #", "# #", "###", '#', Item.ingotIron});
    094        this.addRecipe(new ItemStack(Item.brewingStand, 1), new Object[] {" B ", "###", '#', Block.cobblestone, 'B', Item.blazeRod});
    095        this.addRecipe(new ItemStack(Block.pumpkinLantern, 1), new Object[] {"A", "B", 'A', Block.pumpkin, 'B', Block.torchWood});
    096        this.addRecipe(new ItemStack(Item.minecartCrate, 1), new Object[] {"A", "B", 'A', Block.chest, 'B', Item.minecartEmpty});
    097        this.addRecipe(new ItemStack(Item.minecartPowered, 1), new Object[] {"A", "B", 'A', Block.furnaceIdle, 'B', Item.minecartEmpty});
    098        this.addRecipe(new ItemStack(Item.tntMinecart, 1), new Object[] {"A", "B", 'A', Block.tnt, 'B', Item.minecartEmpty});
    099        this.addRecipe(new ItemStack(Item.hopperMinecart, 1), new Object[] {"A", "B", 'A', Block.hopperBlock, 'B', Item.minecartEmpty});
    100        this.addRecipe(new ItemStack(Item.boat, 1), new Object[] {"# #", "###", '#', Block.planks});
    101        this.addRecipe(new ItemStack(Item.bucketEmpty, 1), new Object[] {"# #", " # ", '#', Item.ingotIron});
    102        this.addRecipe(new ItemStack(Item.flowerPot, 1), new Object[] {"# #", " # ", '#', Item.brick});
    103        this.addRecipe(new ItemStack(Item.flintAndSteel, 1), new Object[] {"A ", " B", 'A', Item.ingotIron, 'B', Item.flint});
    104        this.addRecipe(new ItemStack(Item.bread, 1), new Object[] {"###", '#', Item.wheat});
    105        this.addRecipe(new ItemStack(Block.stairsWoodOak, 4), new Object[] {"#  ", "## ", "###", '#', new ItemStack(Block.planks, 1, 0)});
    106        this.addRecipe(new ItemStack(Block.stairsWoodBirch, 4), new Object[] {"#  ", "## ", "###", '#', new ItemStack(Block.planks, 1, 2)});
    107        this.addRecipe(new ItemStack(Block.stairsWoodSpruce, 4), new Object[] {"#  ", "## ", "###", '#', new ItemStack(Block.planks, 1, 1)});
    108        this.addRecipe(new ItemStack(Block.stairsWoodJungle, 4), new Object[] {"#  ", "## ", "###", '#', new ItemStack(Block.planks, 1, 3)});
    109        this.addRecipe(new ItemStack(Item.fishingRod, 1), new Object[] {"  #", " #X", "# X", '#', Item.stick, 'X', Item.silk});
    110        this.addRecipe(new ItemStack(Item.carrotOnAStick, 1), new Object[] {"# ", " X", '#', Item.fishingRod, 'X', Item.carrot}).func_92100_c();
    111        this.addRecipe(new ItemStack(Block.stairsCobblestone, 4), new Object[] {"#  ", "## ", "###", '#', Block.cobblestone});
    112        this.addRecipe(new ItemStack(Block.stairsBrick, 4), new Object[] {"#  ", "## ", "###", '#', Block.brick});
    113        this.addRecipe(new ItemStack(Block.stairsStoneBrick, 4), new Object[] {"#  ", "## ", "###", '#', Block.stoneBrick});
    114        this.addRecipe(new ItemStack(Block.stairsNetherBrick, 4), new Object[] {"#  ", "## ", "###", '#', Block.netherBrick});
    115        this.addRecipe(new ItemStack(Block.stairsSandStone, 4), new Object[] {"#  ", "## ", "###", '#', Block.sandStone});
    116        this.addRecipe(new ItemStack(Block.stairsNetherQuartz, 4), new Object[] {"#  ", "## ", "###", '#', Block.blockNetherQuartz});
    117        this.addRecipe(new ItemStack(Item.painting, 1), new Object[] {"###", "#X#", "###", '#', Item.stick, 'X', Block.cloth});
    118        this.addRecipe(new ItemStack(Item.itemFrame, 1), new Object[] {"###", "#X#", "###", '#', Item.stick, 'X', Item.leather});
    119        this.addRecipe(new ItemStack(Item.appleGold, 1, 0), new Object[] {"###", "#X#", "###", '#', Item.goldNugget, 'X', Item.appleRed});
    120        this.addRecipe(new ItemStack(Item.appleGold, 1, 1), new Object[] {"###", "#X#", "###", '#', Block.blockGold, 'X', Item.appleRed});
    121        this.addRecipe(new ItemStack(Item.goldenCarrot, 1, 0), new Object[] {"###", "#X#", "###", '#', Item.goldNugget, 'X', Item.carrot});
    122        this.addRecipe(new ItemStack(Block.lever, 1), new Object[] {"X", "#", '#', Block.cobblestone, 'X', Item.stick});
    123        this.addRecipe(new ItemStack(Block.tripWireSource, 2), new Object[] {"I", "S", "#", '#', Block.planks, 'S', Item.stick, 'I', Item.ingotIron});
    124        this.addRecipe(new ItemStack(Block.torchRedstoneActive, 1), new Object[] {"X", "#", '#', Item.stick, 'X', Item.redstone});
    125        this.addRecipe(new ItemStack(Item.redstoneRepeater, 1), new Object[] {"#X#", "III", '#', Block.torchRedstoneActive, 'X', Item.redstone, 'I', Block.stone});
    126        this.addRecipe(new ItemStack(Item.comparator, 1), new Object[] {" # ", "#X#", "III", '#', Block.torchRedstoneActive, 'X', Item.netherQuartz, 'I', Block.stone});
    127        this.addRecipe(new ItemStack(Item.pocketSundial, 1), new Object[] {" # ", "#X#", " # ", '#', Item.ingotGold, 'X', Item.redstone});
    128        this.addRecipe(new ItemStack(Item.compass, 1), new Object[] {" # ", "#X#", " # ", '#', Item.ingotIron, 'X', Item.redstone});
    129        this.addRecipe(new ItemStack(Item.emptyMap, 1), new Object[] {"###", "#X#", "###", '#', Item.paper, 'X', Item.compass});
    130        this.addRecipe(new ItemStack(Block.stoneButton, 1), new Object[] {"#", '#', Block.stone});
    131        this.addRecipe(new ItemStack(Block.woodenButton, 1), new Object[] {"#", '#', Block.planks});
    132        this.addRecipe(new ItemStack(Block.pressurePlateStone, 1), new Object[] {"##", '#', Block.stone});
    133        this.addRecipe(new ItemStack(Block.pressurePlatePlanks, 1), new Object[] {"##", '#', Block.planks});
    134        this.addRecipe(new ItemStack(Block.pressurePlateIron, 1), new Object[] {"##", '#', Item.ingotIron});
    135        this.addRecipe(new ItemStack(Block.pressurePlateGold, 1), new Object[] {"##", '#', Item.ingotGold});
    136        this.addRecipe(new ItemStack(Block.dispenser, 1), new Object[] {"###", "#X#", "#R#", '#', Block.cobblestone, 'X', Item.bow, 'R', Item.redstone});
    137        this.addRecipe(new ItemStack(Block.dropper, 1), new Object[] {"###", "# #", "#R#", '#', Block.cobblestone, 'R', Item.redstone});
    138        this.addRecipe(new ItemStack(Block.pistonBase, 1), new Object[] {"TTT", "#X#", "#R#", '#', Block.cobblestone, 'X', Item.ingotIron, 'R', Item.redstone, 'T', Block.planks});
    139        this.addRecipe(new ItemStack(Block.pistonStickyBase, 1), new Object[] {"S", "P", 'S', Item.slimeBall, 'P', Block.pistonBase});
    140        this.addRecipe(new ItemStack(Item.bed, 1), new Object[] {"###", "XXX", '#', Block.cloth, 'X', Block.planks});
    141        this.addRecipe(new ItemStack(Block.enchantmentTable, 1), new Object[] {" B ", "D#D", "###", '#', Block.obsidian, 'B', Item.book, 'D', Item.diamond});
    142        this.addRecipe(new ItemStack(Block.anvil, 1), new Object[] {"III", " i ", "iii", 'I', Block.blockIron, 'i', Item.ingotIron});
    143        this.addShapelessRecipe(new ItemStack(Item.eyeOfEnder, 1), new Object[] {Item.enderPearl, Item.blazePowder});
    144        this.addShapelessRecipe(new ItemStack(Item.fireballCharge, 3), new Object[] {Item.gunpowder, Item.blazePowder, Item.coal});
    145        this.addShapelessRecipe(new ItemStack(Item.fireballCharge, 3), new Object[] {Item.gunpowder, Item.blazePowder, new ItemStack(Item.coal, 1, 1)});
    146        this.addRecipe(new ItemStack(Block.daylightSensor), new Object[] {"GGG", "QQQ", "WWW", 'G', Block.glass, 'Q', Item.netherQuartz, 'W', Block.woodSingleSlab});
    147        this.addRecipe(new ItemStack(Block.hopperBlock), new Object[] {"I I", "ICI", " I ", 'I', Item.ingotIron, 'C', Block.chest});
    148        Collections.sort(this.recipes, new RecipeSorter(this));
    149        System.out.println(this.recipes.size() + " recipes");
    150    }
    151
    152    public ShapedRecipes addRecipe(ItemStack par1ItemStack, Object ... par2ArrayOfObj)
    153    {
    154        String s = "";
    155        int i = 0;
    156        int j = 0;
    157        int k = 0;
    158
    159        if (par2ArrayOfObj[i] instanceof String[])
    160        {
    161            String[] astring = (String[])((String[])par2ArrayOfObj[i++]);
    162
    163            for (int l = 0; l < astring.length; ++l)
    164            {
    165                String s1 = astring[l];
    166                ++k;
    167                j = s1.length();
    168                s = s + s1;
    169            }
    170        }
    171        else
    172        {
    173            while (par2ArrayOfObj[i] instanceof String)
    174            {
    175                String s2 = (String)par2ArrayOfObj[i++];
    176                ++k;
    177                j = s2.length();
    178                s = s + s2;
    179            }
    180        }
    181
    182        HashMap hashmap;
    183
    184        for (hashmap = new HashMap(); i < par2ArrayOfObj.length; i += 2)
    185        {
    186            Character character = (Character)par2ArrayOfObj[i];
    187            ItemStack itemstack1 = null;
    188
    189            if (par2ArrayOfObj[i + 1] instanceof Item)
    190            {
    191                itemstack1 = new ItemStack((Item)par2ArrayOfObj[i + 1]);
    192            }
    193            else if (par2ArrayOfObj[i + 1] instanceof Block)
    194            {
    195                itemstack1 = new ItemStack((Block)par2ArrayOfObj[i + 1], 1, 32767);
    196            }
    197            else if (par2ArrayOfObj[i + 1] instanceof ItemStack)
    198            {
    199                itemstack1 = (ItemStack)par2ArrayOfObj[i + 1];
    200            }
    201
    202            hashmap.put(character, itemstack1);
    203        }
    204
    205        ItemStack[] aitemstack = new ItemStack[j * k];
    206
    207        for (int i1 = 0; i1 < j * k; ++i1)
    208        {
    209            char c0 = s.charAt(i1);
    210
    211            if (hashmap.containsKey(Character.valueOf(c0)))
    212            {
    213                aitemstack[i1] = ((ItemStack)hashmap.get(Character.valueOf(c0))).copy();
    214            }
    215            else
    216            {
    217                aitemstack[i1] = null;
    218            }
    219        }
    220
    221        ShapedRecipes shapedrecipes = new ShapedRecipes(j, k, aitemstack, par1ItemStack);
    222        this.recipes.add(shapedrecipes);
    223        return shapedrecipes;
    224    }
    225
    226    public void addShapelessRecipe(ItemStack par1ItemStack, Object ... par2ArrayOfObj)
    227    {
    228        ArrayList arraylist = new ArrayList();
    229        Object[] aobject = par2ArrayOfObj;
    230        int i = par2ArrayOfObj.length;
    231
    232        for (int j = 0; j < i; ++j)
    233        {
    234            Object object1 = aobject[j];
    235
    236            if (object1 instanceof ItemStack)
    237            {
    238                arraylist.add(((ItemStack)object1).copy());
    239            }
    240            else if (object1 instanceof Item)
    241            {
    242                arraylist.add(new ItemStack((Item)object1));
    243            }
    244            else
    245            {
    246                if (!(object1 instanceof Block))
    247                {
    248                    throw new RuntimeException("Invalid shapeless recipy!");
    249                }
    250
    251                arraylist.add(new ItemStack((Block)object1));
    252            }
    253        }
    254
    255        this.recipes.add(new ShapelessRecipes(par1ItemStack, arraylist));
    256    }
    257
    258    public ItemStack findMatchingRecipe(InventoryCrafting par1InventoryCrafting, World par2World)
    259    {
    260        int i = 0;
    261        ItemStack itemstack = null;
    262        ItemStack itemstack1 = null;
    263        int j;
    264
    265        for (j = 0; j < par1InventoryCrafting.getSizeInventory(); ++j)
    266        {
    267            ItemStack itemstack2 = par1InventoryCrafting.getStackInSlot(j);
    268
    269            if (itemstack2 != null)
    270            {
    271                if (i == 0)
    272                {
    273                    itemstack = itemstack2;
    274                }
    275
    276                if (i == 1)
    277                {
    278                    itemstack1 = itemstack2;
    279                }
    280
    281                ++i;
    282            }
    283        }
    284
    285        if (i == 2 && itemstack.itemID == itemstack1.itemID && itemstack.stackSize == 1 && itemstack1.stackSize == 1 && Item.itemsList[itemstack.itemID].isRepairable())
    286        {
    287            Item item = Item.itemsList[itemstack.itemID];
    288            int k = item.getMaxDamage() - itemstack.getItemDamageForDisplay();
    289            int l = item.getMaxDamage() - itemstack1.getItemDamageForDisplay();
    290            int i1 = k + l + item.getMaxDamage() * 5 / 100;
    291            int j1 = item.getMaxDamage() - i1;
    292
    293            if (j1 < 0)
    294            {
    295                j1 = 0;
    296            }
    297
    298            return new ItemStack(itemstack.itemID, 1, j1);
    299        }
    300        else
    301        {
    302            for (j = 0; j < this.recipes.size(); ++j)
    303            {
    304                IRecipe irecipe = (IRecipe)this.recipes.get(j);
    305
    306                if (irecipe.matches(par1InventoryCrafting, par2World))
    307                {
    308                    return irecipe.getCraftingResult(par1InventoryCrafting);
    309                }
    310            }
    311
    312            return null;
    313        }
    314    }
    315
    316    /**
    317     * returns the List<> of all recipes
    318     */
    319    public List getRecipeList()
    320    {
    321        return this.recipes;
    322    }
    323}