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