001package net.minecraft.inventory;
002
003import net.minecraft.entity.IMerchant;
004import net.minecraft.entity.player.EntityPlayer;
005import net.minecraft.item.ItemStack;
006import net.minecraft.village.MerchantRecipe;
007import net.minecraft.village.MerchantRecipeList;
008
009public class InventoryMerchant implements IInventory
010{
011    private final IMerchant theMerchant;
012    private ItemStack[] theInventory = new ItemStack[3];
013    private final EntityPlayer thePlayer;
014    private MerchantRecipe currentRecipe;
015    private int currentRecipeIndex;
016
017    public InventoryMerchant(EntityPlayer par1EntityPlayer, IMerchant par2IMerchant)
018    {
019        this.thePlayer = par1EntityPlayer;
020        this.theMerchant = par2IMerchant;
021    }
022
023    /**
024     * Returns the number of slots in the inventory.
025     */
026    public int getSizeInventory()
027    {
028        return this.theInventory.length;
029    }
030
031    /**
032     * Returns the stack in slot i
033     */
034    public ItemStack getStackInSlot(int par1)
035    {
036        return this.theInventory[par1];
037    }
038
039    /**
040     * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a
041     * new stack.
042     */
043    public ItemStack decrStackSize(int par1, int par2)
044    {
045        if (this.theInventory[par1] != null)
046        {
047            ItemStack var3;
048
049            if (par1 == 2)
050            {
051                var3 = this.theInventory[par1];
052                this.theInventory[par1] = null;
053                return var3;
054            }
055            else if (this.theInventory[par1].stackSize <= par2)
056            {
057                var3 = this.theInventory[par1];
058                this.theInventory[par1] = null;
059
060                if (this.inventoryResetNeededOnSlotChange(par1))
061                {
062                    this.resetRecipeAndSlots();
063                }
064
065                return var3;
066            }
067            else
068            {
069                var3 = this.theInventory[par1].splitStack(par2);
070
071                if (this.theInventory[par1].stackSize == 0)
072                {
073                    this.theInventory[par1] = null;
074                }
075
076                if (this.inventoryResetNeededOnSlotChange(par1))
077                {
078                    this.resetRecipeAndSlots();
079                }
080
081                return var3;
082            }
083        }
084        else
085        {
086            return null;
087        }
088    }
089
090    /**
091     * if par1 slot has changed, does resetRecipeAndSlots need to be called?
092     */
093    private boolean inventoryResetNeededOnSlotChange(int par1)
094    {
095        return par1 == 0 || par1 == 1;
096    }
097
098    /**
099     * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem -
100     * like when you close a workbench GUI.
101     */
102    public ItemStack getStackInSlotOnClosing(int par1)
103    {
104        if (this.theInventory[par1] != null)
105        {
106            ItemStack var2 = this.theInventory[par1];
107            this.theInventory[par1] = null;
108            return var2;
109        }
110        else
111        {
112            return null;
113        }
114    }
115
116    /**
117     * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections).
118     */
119    public void setInventorySlotContents(int par1, ItemStack par2ItemStack)
120    {
121        this.theInventory[par1] = par2ItemStack;
122
123        if (par2ItemStack != null && par2ItemStack.stackSize > this.getInventoryStackLimit())
124        {
125            par2ItemStack.stackSize = this.getInventoryStackLimit();
126        }
127
128        if (this.inventoryResetNeededOnSlotChange(par1))
129        {
130            this.resetRecipeAndSlots();
131        }
132    }
133
134    /**
135     * Returns the name of the inventory.
136     */
137    public String getInvName()
138    {
139        return "mob.villager";
140    }
141
142    /**
143     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
144     * this more of a set than a get?*
145     */
146    public int getInventoryStackLimit()
147    {
148        return 64;
149    }
150
151    /**
152     * Do not make give this method the name canInteractWith because it clashes with Container
153     */
154    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
155    {
156        return this.theMerchant.getCustomer() == par1EntityPlayer;
157    }
158
159    public void openChest() {}
160
161    public void closeChest() {}
162
163    /**
164     * Called when an the contents of an Inventory change, usually
165     */
166    public void onInventoryChanged()
167    {
168        this.resetRecipeAndSlots();
169    }
170
171    public void resetRecipeAndSlots()
172    {
173        this.currentRecipe = null;
174        ItemStack var1 = this.theInventory[0];
175        ItemStack var2 = this.theInventory[1];
176
177        if (var1 == null)
178        {
179            var1 = var2;
180            var2 = null;
181        }
182
183        if (var1 == null)
184        {
185            this.setInventorySlotContents(2, (ItemStack)null);
186        }
187        else
188        {
189            MerchantRecipeList var3 = this.theMerchant.getRecipes(this.thePlayer);
190
191            if (var3 != null)
192            {
193                MerchantRecipe var4 = var3.canRecipeBeUsed(var1, var2, this.currentRecipeIndex);
194
195                if (var4 != null && !var4.func_82784_g())
196                {
197                    this.currentRecipe = var4;
198                    this.setInventorySlotContents(2, var4.getItemToSell().copy());
199                }
200                else if (var2 != null)
201                {
202                    var4 = var3.canRecipeBeUsed(var2, var1, this.currentRecipeIndex);
203
204                    if (var4 != null && !var4.func_82784_g())
205                    {
206                        this.currentRecipe = var4;
207                        this.setInventorySlotContents(2, var4.getItemToSell().copy());
208                    }
209                    else
210                    {
211                        this.setInventorySlotContents(2, (ItemStack)null);
212                    }
213                }
214                else
215                {
216                    this.setInventorySlotContents(2, (ItemStack)null);
217                }
218            }
219        }
220    }
221
222    public MerchantRecipe getCurrentRecipe()
223    {
224        return this.currentRecipe;
225    }
226
227    public void setCurrentRecipeIndex(int par1)
228    {
229        this.currentRecipeIndex = par1;
230        this.resetRecipeAndSlots();
231    }
232}