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 /** 143 * If this returns false, the inventory name will be used as an unlocalized name, and translated into the player's 144 * language. Otherwise it will be used directly. 145 */ 146 public boolean isInvNameLocalized() 147 { 148 return false; 149 } 150 151 /** 152 * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't 153 * this more of a set than a get?* 154 */ 155 public int getInventoryStackLimit() 156 { 157 return 64; 158 } 159 160 /** 161 * Do not make give this method the name canInteractWith because it clashes with Container 162 */ 163 public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer) 164 { 165 return this.theMerchant.getCustomer() == par1EntityPlayer; 166 } 167 168 public void openChest() {} 169 170 public void closeChest() {} 171 172 /** 173 * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. 174 */ 175 public boolean isStackValidForSlot(int par1, ItemStack par2ItemStack) 176 { 177 return true; 178 } 179 180 /** 181 * Called when an the contents of an Inventory change, usually 182 */ 183 public void onInventoryChanged() 184 { 185 this.resetRecipeAndSlots(); 186 } 187 188 public void resetRecipeAndSlots() 189 { 190 this.currentRecipe = null; 191 ItemStack itemstack = this.theInventory[0]; 192 ItemStack itemstack1 = this.theInventory[1]; 193 194 if (itemstack == null) 195 { 196 itemstack = itemstack1; 197 itemstack1 = null; 198 } 199 200 if (itemstack == null) 201 { 202 this.setInventorySlotContents(2, (ItemStack)null); 203 } 204 else 205 { 206 MerchantRecipeList merchantrecipelist = this.theMerchant.getRecipes(this.thePlayer); 207 208 if (merchantrecipelist != null) 209 { 210 MerchantRecipe merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack, itemstack1, this.currentRecipeIndex); 211 212 if (merchantrecipe != null && !merchantrecipe.func_82784_g()) 213 { 214 this.currentRecipe = merchantrecipe; 215 this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy()); 216 } 217 else if (itemstack1 != null) 218 { 219 merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack1, itemstack, this.currentRecipeIndex); 220 221 if (merchantrecipe != null && !merchantrecipe.func_82784_g()) 222 { 223 this.currentRecipe = merchantrecipe; 224 this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy()); 225 } 226 else 227 { 228 this.setInventorySlotContents(2, (ItemStack)null); 229 } 230 } 231 else 232 { 233 this.setInventorySlotContents(2, (ItemStack)null); 234 } 235 } 236 } 237 } 238 239 public MerchantRecipe getCurrentRecipe() 240 { 241 return this.currentRecipe; 242 } 243 244 public void setCurrentRecipeIndex(int par1) 245 { 246 this.currentRecipeIndex = par1; 247 this.resetRecipeAndSlots(); 248 } 249}