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