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