001    package net.minecraft.src;
002    
003    import cpw.mods.fml.common.Side;
004    import cpw.mods.fml.common.asm.SideOnly;
005    
006    public class ContainerMerchant extends Container
007    {
008        private IMerchant field_75178_e;
009        private InventoryMerchant merchantInventory;
010        private final World field_75177_g;
011    
012        public ContainerMerchant(InventoryPlayer par1InventoryPlayer, IMerchant par2IMerchant, World par3World)
013        {
014            this.field_75178_e = par2IMerchant;
015            this.field_75177_g = par3World;
016            this.merchantInventory = new InventoryMerchant(par1InventoryPlayer.player, par2IMerchant);
017            this.addSlotToContainer(new Slot(this.merchantInventory, 0, 36, 53));
018            this.addSlotToContainer(new Slot(this.merchantInventory, 1, 62, 53));
019            this.addSlotToContainer(new SlotMerchantResult(par1InventoryPlayer.player, par2IMerchant, this.merchantInventory, 2, 120, 53));
020            int var4;
021    
022            for (var4 = 0; var4 < 3; ++var4)
023            {
024                for (int var5 = 0; var5 < 9; ++var5)
025                {
026                    this.addSlotToContainer(new Slot(par1InventoryPlayer, var5 + var4 * 9 + 9, 8 + var5 * 18, 84 + var4 * 18));
027                }
028            }
029    
030            for (var4 = 0; var4 < 9; ++var4)
031            {
032                this.addSlotToContainer(new Slot(par1InventoryPlayer, var4, 8 + var4 * 18, 142));
033            }
034        }
035    
036        public InventoryMerchant getMerchantInventory()
037        {
038            return this.merchantInventory;
039        }
040    
041        public void addCraftingToCrafters(ICrafting par1ICrafting)
042        {
043            super.addCraftingToCrafters(par1ICrafting);
044        }
045    
046        /**
047         * Updates crafting matrix; called from onCraftMatrixChanged. Args: none
048         */
049        public void updateCraftingResults()
050        {
051            super.updateCraftingResults();
052        }
053    
054        /**
055         * Callback for when the crafting matrix is changed.
056         */
057        public void onCraftMatrixChanged(IInventory par1IInventory)
058        {
059            this.merchantInventory.resetRecipeAndSlots();
060            super.onCraftMatrixChanged(par1IInventory);
061        }
062    
063        public void setCurrentRecipeIndex(int par1)
064        {
065            this.merchantInventory.setCurrentRecipeIndex(par1);
066        }
067    
068        public boolean canInteractWith(EntityPlayer par1EntityPlayer)
069        {
070            return this.field_75178_e.getCustomer() == par1EntityPlayer;
071        }
072    
073        @SideOnly(Side.CLIENT)
074        public void updateProgressBar(int par1, int par2) {}
075    
076        /**
077         * Called to transfer a stack from one inventory to the other eg. when shift clicking.
078         */
079        public ItemStack transferStackInSlot(int par1)
080        {
081            ItemStack var2 = null;
082            Slot var3 = (Slot)this.inventorySlots.get(par1);
083    
084            if (var3 != null && var3.getHasStack())
085            {
086                ItemStack var4 = var3.getStack();
087                var2 = var4.copy();
088    
089                if (par1 == 2)
090                {
091                    if (!this.mergeItemStack(var4, 3, 39, true))
092                    {
093                        return null;
094                    }
095    
096                    var3.onSlotChange(var4, var2);
097                }
098                else if (par1 != 0 && par1 != 1)
099                {
100                    if (par1 >= 3 && par1 < 30)
101                    {
102                        if (!this.mergeItemStack(var4, 30, 39, false))
103                        {
104                            return null;
105                        }
106                    }
107                    else if (par1 >= 30 && par1 < 39 && !this.mergeItemStack(var4, 3, 30, false))
108                    {
109                        return null;
110                    }
111                }
112                else if (!this.mergeItemStack(var4, 3, 39, false))
113                {
114                    return null;
115                }
116    
117                if (var4.stackSize == 0)
118                {
119                    var3.putStack((ItemStack)null);
120                }
121                else
122                {
123                    var3.onSlotChanged();
124                }
125    
126                if (var4.stackSize == var2.stackSize)
127                {
128                    return null;
129                }
130    
131                var3.onPickupFromSlot(var4);
132            }
133    
134            return var2;
135        }
136    
137        /**
138         * Callback for when the crafting gui is closed.
139         */
140        public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
141        {
142            super.onCraftGuiClosed(par1EntityPlayer);
143            this.field_75178_e.setCustomer((EntityPlayer)null);
144            super.onCraftGuiClosed(par1EntityPlayer);
145    
146            if (!this.field_75177_g.isRemote)
147            {
148                ItemStack var2 = this.merchantInventory.getStackInSlotOnClosing(0);
149    
150                if (var2 != null)
151                {
152                    par1EntityPlayer.dropPlayerItem(var2);
153                }
154    
155                var2 = this.merchantInventory.getStackInSlotOnClosing(1);
156    
157                if (var2 != null)
158                {
159                    par1EntityPlayer.dropPlayerItem(var2);
160                }
161            }
162        }
163    }