    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        public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    072        {
    073            return this.theMerchant.getCustomer() == par1EntityPlayer;
    074        }
    075
    076        @SideOnly(Side.CLIENT)
    077        public void updateProgressBar(int par1, int par2) {}
    078
    079        /**
    080         * Called to transfer a stack from one inventory to the other eg. when shift clicking.
    081         */
    082        public ItemStack transferStackInSlot(int par1)
    083        {
    084            ItemStack var2 = null;
    085            Slot var3 = (Slot)this.inventorySlots.get(par1);
    086
    087            if (var3 != null && var3.getHasStack())
    088            {
    089                ItemStack var4 = var3.getStack();
    090                var2 = var4.copy();
    091
    092                if (par1 == 2)
    093                {
    094                    if (!this.mergeItemStack(var4, 3, 39, true))
    095                    {
    096                        return null;
    097                    }
    098
    099                    var3.onSlotChange(var4, var2);
    100                }
    101                else if (par1 != 0 && par1 != 1)
    102                {
    103                    if (par1 >= 3 && par1 < 30)
    104                    {
    105                        if (!this.mergeItemStack(var4, 30, 39, false))
    106                        {
    107                            return null;
    108                        }
    109                    }
    110                    else if (par1 >= 30 && par1 < 39 && !this.mergeItemStack(var4, 3, 30, false))
    111                    {
    112                        return null;
    113                    }
    114                }
    115                else if (!this.mergeItemStack(var4, 3, 39, false))
    116                {
    117                    return null;
    118                }
    119
    120                if (var4.stackSize == 0)
    121                {
    122                    var3.putStack((ItemStack)null);
    123                }
    124                else
    125                {
    126                    var3.onSlotChanged();
    127                }
    128
    129                if (var4.stackSize == var2.stackSize)
    130                {
    131                    return null;
    132                }
    133
    134                var3.onPickupFromSlot(var4);
    135            }
    136
    137            return var2;
    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    }