package net.minecraft.inventory; import net.minecraft.entity.IMerchant; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.item.ItemStack; import net.minecraft.village.MerchantRecipe; import net.minecraft.village.MerchantRecipeList; public class InventoryMerchant implements IInventory { private final IMerchant theMerchant; private ItemStack[] theInventory = new ItemStack[3]; private final EntityPlayer thePlayer; private MerchantRecipe currentRecipe; private int currentRecipeIndex; private static final String __OBFID = "CL_00001756"; public InventoryMerchant(EntityPlayer p_i1820_1_, IMerchant p_i1820_2_) { this.thePlayer = p_i1820_1_; this.theMerchant = p_i1820_2_; } /** * Returns the number of slots in the inventory. */ public int getSizeInventory() { return this.theInventory.length; } /** * Returns the stack in slot i */ public ItemStack getStackInSlot(int slotIn) { return this.theInventory[slotIn]; } /** * Removes from an inventory slot (first arg) up to a specified number (second arg) of items and returns them in a * new stack. */ public ItemStack decrStackSize(int index, int count) { if (this.theInventory[index] != null) { ItemStack itemstack; if (index == 2) { itemstack = this.theInventory[index]; this.theInventory[index] = null; return itemstack; } else if (this.theInventory[index].stackSize <= count) { itemstack = this.theInventory[index]; this.theInventory[index] = null; if (this.inventoryResetNeededOnSlotChange(index)) { this.resetRecipeAndSlots(); } return itemstack; } else { itemstack = this.theInventory[index].splitStack(count); if (this.theInventory[index].stackSize == 0) { this.theInventory[index] = null; } if (this.inventoryResetNeededOnSlotChange(index)) { this.resetRecipeAndSlots(); } return itemstack; } } else { return null; } } /** * if par1 slot has changed, does resetRecipeAndSlots need to be called? */ private boolean inventoryResetNeededOnSlotChange(int p_70469_1_) { return p_70469_1_ == 0 || p_70469_1_ == 1; } /** * When some containers are closed they call this on each slot, then drop whatever it returns as an EntityItem - * like when you close a workbench GUI. */ public ItemStack getStackInSlotOnClosing(int index) { if (this.theInventory[index] != null) { ItemStack itemstack = this.theInventory[index]; this.theInventory[index] = null; return itemstack; } else { return null; } } /** * Sets the given item stack to the specified slot in the inventory (can be crafting or armor sections). */ public void setInventorySlotContents(int index, ItemStack stack) { this.theInventory[index] = stack; if (stack != null && stack.stackSize > this.getInventoryStackLimit()) { stack.stackSize = this.getInventoryStackLimit(); } if (this.inventoryResetNeededOnSlotChange(index)) { this.resetRecipeAndSlots(); } } /** * Returns the name of the inventory */ public String getInventoryName() { return "mob.villager"; } /** * Returns if the inventory is named */ public boolean isCustomInventoryName() { return false; } /** * Returns the maximum stack size for a inventory slot. Seems to always be 64, possibly will be extended. *Isn't * this more of a set than a get?* */ public int getInventoryStackLimit() { return 64; } /** * Do not make give this method the name canInteractWith because it clashes with Container */ public boolean isUseableByPlayer(EntityPlayer player) { return this.theMerchant.getCustomer() == player; } public void openChest() {} public void closeChest() {} /** * Returns true if automation is allowed to insert the given stack (ignoring stack size) into the given slot. */ public boolean isItemValidForSlot(int index, ItemStack stack) { return true; } /** * For tile entities, ensures the chunk containing the tile entity is saved to disk later - the game won't think it * hasn't changed and skip it. */ public void markDirty() { this.resetRecipeAndSlots(); } public void resetRecipeAndSlots() { this.currentRecipe = null; ItemStack itemstack = this.theInventory[0]; ItemStack itemstack1 = this.theInventory[1]; if (itemstack == null) { itemstack = itemstack1; itemstack1 = null; } if (itemstack == null) { this.setInventorySlotContents(2, (ItemStack)null); } else { MerchantRecipeList merchantrecipelist = this.theMerchant.getRecipes(this.thePlayer); if (merchantrecipelist != null) { MerchantRecipe merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack, itemstack1, this.currentRecipeIndex); if (merchantrecipe != null && !merchantrecipe.isRecipeDisabled()) { this.currentRecipe = merchantrecipe; this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy()); } else if (itemstack1 != null) { merchantrecipe = merchantrecipelist.canRecipeBeUsed(itemstack1, itemstack, this.currentRecipeIndex); if (merchantrecipe != null && !merchantrecipe.isRecipeDisabled()) { this.currentRecipe = merchantrecipe; this.setInventorySlotContents(2, merchantrecipe.getItemToSell().copy()); } else { this.setInventorySlotContents(2, (ItemStack)null); } } else { this.setInventorySlotContents(2, (ItemStack)null); } } } this.theMerchant.verifySellingItem(this.getStackInSlot(2)); } public MerchantRecipe getCurrentRecipe() { return this.currentRecipe; } public void setCurrentRecipeIndex(int p_70471_1_) { this.currentRecipeIndex = p_70471_1_; this.resetRecipeAndSlots(); } }