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