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