001    package net.minecraft.src;
002    
003    import net.minecraftforge.common.ForgeHooks;
004    import net.minecraftforge.common.MinecraftForge;
005    import net.minecraftforge.event.entity.player.PlayerDestroyItemEvent;
006    
007    import cpw.mods.fml.common.registry.GameRegistry;
008    
009    public class SlotCrafting extends Slot
010    {
011        /** The craft matrix inventory linked to this result slot. */
012        private final IInventory craftMatrix;
013    
014        /** The player that is using the GUI where this slot resides. */
015        private EntityPlayer thePlayer;
016    
017        /**
018         * The number of items that have been crafted so far. Gets passed to ItemStack.onCrafting before being reset.
019         */
020        private int amountCrafted;
021    
022        public SlotCrafting(EntityPlayer par1EntityPlayer, IInventory par2IInventory, IInventory par3IInventory, int par4, int par5, int par6)
023        {
024            super(par3IInventory, par4, par5, par6);
025            this.thePlayer = par1EntityPlayer;
026            this.craftMatrix = par2IInventory;
027        }
028    
029        /**
030         * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
031         */
032        public boolean isItemValid(ItemStack par1ItemStack)
033        {
034            return false;
035        }
036    
037        /**
038         * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
039         * stack.
040         */
041        public ItemStack decrStackSize(int par1)
042        {
043            if (this.getHasStack())
044            {
045                this.amountCrafted += Math.min(par1, this.getStack().stackSize);
046            }
047    
048            return super.decrStackSize(par1);
049        }
050    
051        /**
052         * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
053         * internal count then calls onCrafting(item).
054         */
055        protected void onCrafting(ItemStack par1ItemStack, int par2)
056        {
057            this.amountCrafted += par2;
058            this.onCrafting(par1ItemStack);
059        }
060    
061        /**
062         * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
063         */
064        protected void onCrafting(ItemStack par1ItemStack)
065        {
066            par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.amountCrafted);
067            this.amountCrafted = 0;
068    
069            if (par1ItemStack.itemID == Block.workbench.blockID)
070            {
071                this.thePlayer.addStat(AchievementList.buildWorkBench, 1);
072            }
073            else if (par1ItemStack.itemID == Item.pickaxeWood.shiftedIndex)
074            {
075                this.thePlayer.addStat(AchievementList.buildPickaxe, 1);
076            }
077            else if (par1ItemStack.itemID == Block.stoneOvenIdle.blockID)
078            {
079                this.thePlayer.addStat(AchievementList.buildFurnace, 1);
080            }
081            else if (par1ItemStack.itemID == Item.hoeWood.shiftedIndex)
082            {
083                this.thePlayer.addStat(AchievementList.buildHoe, 1);
084            }
085            else if (par1ItemStack.itemID == Item.bread.shiftedIndex)
086            {
087                this.thePlayer.addStat(AchievementList.makeBread, 1);
088            }
089            else if (par1ItemStack.itemID == Item.cake.shiftedIndex)
090            {
091                this.thePlayer.addStat(AchievementList.bakeCake, 1);
092            }
093            else if (par1ItemStack.itemID == Item.pickaxeStone.shiftedIndex)
094            {
095                this.thePlayer.addStat(AchievementList.buildBetterPickaxe, 1);
096            }
097            else if (par1ItemStack.itemID == Item.swordWood.shiftedIndex)
098            {
099                this.thePlayer.addStat(AchievementList.buildSword, 1);
100            }
101            else if (par1ItemStack.itemID == Block.enchantmentTable.blockID)
102            {
103                this.thePlayer.addStat(AchievementList.enchantments, 1);
104            }
105            else if (par1ItemStack.itemID == Block.bookShelf.blockID)
106            {
107                this.thePlayer.addStat(AchievementList.bookcase, 1);
108            }
109        }
110    
111        public void func_82870_a(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
112        {
113            GameRegistry.onItemCrafted(par1EntityPlayer, par2ItemStack, craftMatrix);
114            this.onCrafting(par2ItemStack);
115    
116            for (int var3 = 0; var3 < this.craftMatrix.getSizeInventory(); ++var3)
117            {
118                ItemStack var4 = this.craftMatrix.getStackInSlot(var3);
119    
120                if (var4 != null)
121                {
122                    this.craftMatrix.decrStackSize(var3, 1);
123    
124                    if (var4.getItem().hasContainerItem())
125                    {
126                        ItemStack var5 = var4.getItem().getContainerItemStack(var4);
127                        
128                        if (var5.isItemStackDamageable() && var5.getItemDamage() > var5.getMaxDamage())
129                        {
130                            MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(thePlayer, var5));
131                            var4 = null;
132                        }
133    
134                        if (var5 != null && (!var4.getItem().doesContainerItemLeaveCraftingGrid(var4) || !this.thePlayer.inventory.addItemStackToInventory(var5)))
135                        {
136                            if (this.craftMatrix.getStackInSlot(var3) == null)
137                            {
138                                this.craftMatrix.setInventorySlotContents(var3, var5);
139                            }
140                            else
141                            {
142                                this.thePlayer.dropPlayerItem(var5);
143                            }
144                        }
145                    }
146                }
147            }
148        }
149    }