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