    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        public ItemStack func_82846_b(EntityPlayer par1EntityPlayer, int par2)
    080        {
    081            ItemStack var3 = null;
    082            Slot var4 = (Slot)this.inventorySlots.get(par2);
    083
    084            if (var4 != null && var4.getHasStack())
    085            {
    086                ItemStack var5 = var4.getStack();
    087                var3 = var5.copy();
    088
    089                if (par2 == 2)
    090                {
    091                    if (!this.mergeItemStack(var5, 3, 39, true))
    092                    {
    093                        return null;
    094                    }
    095
    096                    var4.onSlotChange(var5, var3);
    097                }
    098                else if (par2 != 0 && par2 != 1)
    099                {
    100                    if (par2 >= 3 && par2 < 30)
    101                    {
    102                        if (!this.mergeItemStack(var5, 30, 39, false))
    103                        {
    104                            return null;
    105                        }
    106                    }
    107                    else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(var5, 3, 30, false))
    108                    {
    109                        return null;
    110                    }
    111                }
    112                else if (!this.mergeItemStack(var5, 3, 39, false))
    113                {
    114                    return null;
    115                }
    116
    117                if (var5.stackSize == 0)
    118                {
    119                    var4.putStack((ItemStack)null);
    120                }
    121                else
    122                {
    123                    var4.onSlotChanged();
    124                }
    125
    126                if (var5.stackSize == var3.stackSize)
    127                {
    128                    return null;
    129                }
    130
    131                var4.func_82870_a(par1EntityPlayer, var5);
    132            }
    133
    134            return var3;
    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.theMerchant.setCustomer((EntityPlayer)null);
    144            super.onCraftGuiClosed(par1EntityPlayer);
    145
    146            if (!this.theWorld.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    }