001    package net.minecraft.inventory;
002    
003    import net.minecraft.entity.IMerchant;
004    import net.minecraft.entity.player.EntityPlayer;
005    import net.minecraft.item.ItemStack;
006    import net.minecraft.village.MerchantRecipe;
007    
008    public class SlotMerchantResult extends Slot
009    {
010        /** Merchant's inventory. */
011        private final InventoryMerchant theMerchantInventory;
012    
013        /** The Player whos trying to buy/sell stuff. */
014        private EntityPlayer thePlayer;
015        private int field_75231_g;
016    
017        /** "Instance" of the Merchant. */
018        private final IMerchant theMerchant;
019    
020        public SlotMerchantResult(EntityPlayer par1EntityPlayer, IMerchant par2IMerchant, InventoryMerchant par3InventoryMerchant, int par4, int par5, int par6)
021        {
022            super(par3InventoryMerchant, par4, par5, par6);
023            this.thePlayer = par1EntityPlayer;
024            this.theMerchant = par2IMerchant;
025            this.theMerchantInventory = par3InventoryMerchant;
026        }
027    
028        /**
029         * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
030         */
031        public boolean isItemValid(ItemStack par1ItemStack)
032        {
033            return false;
034        }
035    
036        /**
037         * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
038         * stack.
039         */
040        public ItemStack decrStackSize(int par1)
041        {
042            if (this.getHasStack())
043            {
044                this.field_75231_g += Math.min(par1, this.getStack().stackSize);
045            }
046    
047            return super.decrStackSize(par1);
048        }
049    
050        /**
051         * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
052         * internal count then calls onCrafting(item).
053         */
054        protected void onCrafting(ItemStack par1ItemStack, int par2)
055        {
056            this.field_75231_g += par2;
057            this.onCrafting(par1ItemStack);
058        }
059    
060        /**
061         * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
062         */
063        protected void onCrafting(ItemStack par1ItemStack)
064        {
065            par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.field_75231_g);
066            this.field_75231_g = 0;
067        }
068    
069        public void onPickupFromSlot(EntityPlayer par1EntityPlayer, ItemStack par2ItemStack)
070        {
071            this.onCrafting(par2ItemStack);
072            MerchantRecipe var3 = this.theMerchantInventory.getCurrentRecipe();
073    
074            if (var3 != null)
075            {
076                ItemStack var4 = this.theMerchantInventory.getStackInSlot(0);
077                ItemStack var5 = this.theMerchantInventory.getStackInSlot(1);
078    
079                if (this.func_75230_a(var3, var4, var5) || this.func_75230_a(var3, var5, var4))
080                {
081                    if (var4 != null && var4.stackSize <= 0)
082                    {
083                        var4 = null;
084                    }
085    
086                    if (var5 != null && var5.stackSize <= 0)
087                    {
088                        var5 = null;
089                    }
090    
091                    this.theMerchantInventory.setInventorySlotContents(0, var4);
092                    this.theMerchantInventory.setInventorySlotContents(1, var5);
093                    this.theMerchant.useRecipe(var3);
094                }
095            }
096        }
097    
098        private boolean func_75230_a(MerchantRecipe par1MerchantRecipe, ItemStack par2ItemStack, ItemStack par3ItemStack)
099        {
100            ItemStack var4 = par1MerchantRecipe.getItemToBuy();
101            ItemStack var5 = par1MerchantRecipe.getSecondItemToBuy();
102    
103            if (par2ItemStack != null && par2ItemStack.itemID == var4.itemID)
104            {
105                if (var5 != null && par3ItemStack != null && var5.itemID == par3ItemStack.itemID)
106                {
107                    par2ItemStack.stackSize -= var4.stackSize;
108                    par3ItemStack.stackSize -= var5.stackSize;
109                    return true;
110                }
111    
112                if (var5 == null && par3ItemStack == null)
113                {
114                    par2ItemStack.stackSize -= var4.stackSize;
115                    return true;
116                }
117            }
118    
119            return false;
120        }
121    }