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 itemstack;
048
049            if (par1 == 2)
050            {
051                itemstack = this.theInventory[par1];
052                this.theInventory[par1] = null;
053                return itemstack;
054            }
055            else if (this.theInventory[par1].stackSize <= par2)
056            {
057                itemstack = this.theInventory[par1];
058                this.theInventory[par1] = null;
059
060                if (this.inventoryResetNeededOnSlotChange(par1))
061                {
062                    this.resetRecipeAndSlots();
063                }
064
065                return itemstack;
066            }
067            else
068            {
069                itemstack = 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 itemstack;
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 itemstack = this.theInventory[par1];
107            this.theInventory[par1] = null;
108            return itemstack;
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    public boolean func_94042_c()
143    {
144        return false;
145    }
146
147    /**
148     * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't
149     * this more of a set than a get?*
150     */
151    public int getInventoryStackLimit()
152    {
153        return 64;
154    }
155
156    /**
157     * Do not make give this method the name canInteractWith because it clashes with Container
158     */
159    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
160    {
161        return this.theMerchant.getCustomer() == par1EntityPlayer;
162    }
163
164    public void openChest() {}
165
166    public void closeChest() {}
167
168    public boolean func_94041_b(int par1, ItemStack par2ItemStack)
169    {
170        return true;
171    }
172
173    /**
174     * Called when an the contents of an Inventory change, usually
175     */
176    public void onInventoryChanged()
177    {
178        this.resetRecipeAndSlots();
179    }
180
181    public void resetRecipeAndSlots()
182    {
183        this.currentRecipe = null;
184        ItemStack itemstack = this.theInventory[0];
185        ItemStack itemstack1 = this.theInventory[1];
186
187        if (itemstack == null)
188        {
189            itemstack = itemstack1;
190            itemstack1 = null;
191        }
192
193        if (itemstack == null)
194        {
195            this.setInventorySlotContents(2, (ItemStack)null);
196        }
197        else
198        {
199            MerchantRecipeList merchantrecipelist = this.theMerchant.getRecipes(this.thePlayer);
200
201            if (merchantrecipelist != null)
202            {
203                MerchantRecipe merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack, itemstack1, this.currentRecipeIndex);
204
205                if (merchantrecipe != null && !merchantrecipe.func_82784_g())
206                {
207                    this.currentRecipe = merchantrecipe;
208                    this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy());
209                }
210                else if (itemstack1 != null)
211                {
212                    merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack1, itemstack, this.currentRecipeIndex);
213
214                    if (merchantrecipe != null && !merchantrecipe.func_82784_g())
215                    {
216                        this.currentRecipe = merchantrecipe;
217                        this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy());
218                    }
219                    else
220                    {
221                        this.setInventorySlotContents(2, (ItemStack)null);
222                    }
223                }
224                else
225                {
226                    this.setInventorySlotContents(2, (ItemStack)null);
227                }
228            }
229        }
230    }
231
232    public MerchantRecipe getCurrentRecipe()
233    {
234        return this.currentRecipe;
235    }
236
237    public void setCurrentRecipeIndex(int par1)
238    {
239        this.currentRecipeIndex = par1;
240        this.resetRecipeAndSlots();
241    }
242}