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 i;
029
030        for (i = 0; i < 3; ++i)
031        {
032            for (int j = 0; j < 9; ++j)
033            {
034                this.addSlotToContainer(new Slot(par1InventoryPlayer, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
035            }
036        }
037
038        for (i = 0; i < 9; ++i)
039        {
040            this.addSlotToContainer(new Slot(par1InventoryPlayer, i, 8 + i * 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 itemstack = null;
090        Slot slot = (Slot)this.inventorySlots.get(par2);
091
092        if (slot != null && slot.getHasStack())
093        {
094            ItemStack itemstack1 = slot.getStack();
095            itemstack = itemstack1.copy();
096
097            if (par2 == 2)
098            {
099                if (!this.mergeItemStack(itemstack1, 3, 39, true))
100                {
101                    return null;
102                }
103
104                slot.onSlotChange(itemstack1, itemstack);
105            }
106            else if (par2 != 0 && par2 != 1)
107            {
108                if (par2 >= 3 && par2 < 30)
109                {
110                    if (!this.mergeItemStack(itemstack1, 30, 39, false))
111                    {
112                        return null;
113                    }
114                }
115                else if (par2 >= 30 && par2 < 39 && !this.mergeItemStack(itemstack1, 3, 30, false))
116                {
117                    return null;
118                }
119            }
120            else if (!this.mergeItemStack(itemstack1, 3, 39, false))
121            {
122                return null;
123            }
124
125            if (itemstack1.stackSize == 0)
126            {
127                slot.putStack((ItemStack)null);
128            }
129            else
130            {
131                slot.onSlotChanged();
132            }
133
134            if (itemstack1.stackSize == itemstack.stackSize)
135            {
136                return null;
137            }
138
139            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
140        }
141
142        return itemstack;
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 itemstack = this.merchantInventory.getStackInSlotOnClosing(0);
157
158            if (itemstack != null)
159            {
160                par1EntityPlayer.dropPlayerItem(itemstack);
161            }
162
163            itemstack = this.merchantInventory.getStackInSlotOnClosing(1);
164
165            if (itemstack != null)
166            {
167                par1EntityPlayer.dropPlayerItem(itemstack);
168            }
169        }
170    }
171}