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        /**
112         * Called when the player picks up an item from an inventory slot
113         */
114        public void onPickupFromSlot(ItemStack par1ItemStack)
115        {
116            GameRegistry.onItemCrafted(thePlayer, par1ItemStack, craftMatrix);
117            this.onCrafting(par1ItemStack);
118    
119            for (int var2 = 0; var2 < this.craftMatrix.getSizeInventory(); ++var2)
120            {
121                ItemStack var3 = this.craftMatrix.getStackInSlot(var2);
122    
123                if (var3 != null)
124                {
125                    this.craftMatrix.decrStackSize(var2, 1);
126    
127                    if (var3.getItem().hasContainerItem())
128                    {
129                        ItemStack var4 = var3.getItem().getContainerItemStack(var3);
130                        
131                        if (var4.isItemStackDamageable() && var4.getItemDamage() > var4.getMaxDamage())
132                        {
133                            MinecraftForge.EVENT_BUS.post(new PlayerDestroyItemEvent(thePlayer, var4));
134                            var4 = null;
135                        }
136    
137                        if (var4 != null && (!var3.getItem().doesContainerItemLeaveCraftingGrid(var3) || !this.thePlayer.inventory.addItemStackToInventory(var4)))
138                        {
139                            if (this.craftMatrix.getStackInSlot(var2) == null)
140                            {
141                                this.craftMatrix.setInventorySlotContents(var2, var4);
142                            }
143                            else
144                            {
145                                this.thePlayer.dropPlayerItem(var4);
146                            }
147                        }
148                    }
149                }
150            }
151        }
152    }