001 package net.minecraft.src;
002
003 public class SlotMerchantResult extends Slot
004 {
005 /** Merchant's inventory. */
006 private final InventoryMerchant theMerchantInventory;
007
008 /** The Player whos trying to buy/sell stuff. */
009 private EntityPlayer thePlayer;
010 private int field_75231_g;
011
012 /** "Instance" of the Merchant. */
013 private final IMerchant theMerchant;
014
015 public SlotMerchantResult(EntityPlayer par1EntityPlayer, IMerchant par2IMerchant, InventoryMerchant par3InventoryMerchant, int par4, int par5, int par6)
016 {
017 super(par3InventoryMerchant, par4, par5, par6);
018 this.thePlayer = par1EntityPlayer;
019 this.theMerchant = par2IMerchant;
020 this.theMerchantInventory = par3InventoryMerchant;
021 }
022
023 /**
024 * Check if the stack is a valid item for this slot. Always true beside for the armor slots.
025 */
026 public boolean isItemValid(ItemStack par1ItemStack)
027 {
028 return false;
029 }
030
031 /**
032 * Decrease the size of the stack in slot (first int arg) by the amount of the second int arg. Returns the new
033 * stack.
034 */
035 public ItemStack decrStackSize(int par1)
036 {
037 if (this.getHasStack())
038 {
039 this.field_75231_g += Math.min(par1, this.getStack().stackSize);
040 }
041
042 return super.decrStackSize(par1);
043 }
044
045 /**
046 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood. Typically increases an
047 * internal count then calls onCrafting(item).
048 */
049 protected void onCrafting(ItemStack par1ItemStack, int par2)
050 {
051 this.field_75231_g += par2;
052 this.onCrafting(par1ItemStack);
053 }
054
055 /**
056 * the itemStack passed in is the output - ie, iron ingots, and pickaxes, not ore and wood.
057 */
058 protected void onCrafting(ItemStack par1ItemStack)
059 {
060 par1ItemStack.onCrafting(this.thePlayer.worldObj, this.thePlayer, this.field_75231_g);
061 this.field_75231_g = 0;
062 }
063
064 /**
065 * Called when the player picks up an item from an inventory slot
066 */
067 public void onPickupFromSlot(ItemStack par1ItemStack)
068 {
069 this.onCrafting(par1ItemStack);
070 MerchantRecipe var2 = this.theMerchantInventory.getCurrentRecipe();
071
072 if (var2 != null)
073 {
074 ItemStack var3 = this.theMerchantInventory.getStackInSlot(0);
075 ItemStack var4 = this.theMerchantInventory.getStackInSlot(1);
076
077 if (this.func_75230_a(var2, var3, var4) || this.func_75230_a(var2, var4, var3))
078 {
079 if (var3 != null && var3.stackSize <= 0)
080 {
081 var3 = null;
082 }
083
084 if (var4 != null && var4.stackSize <= 0)
085 {
086 var4 = null;
087 }
088
089 this.theMerchantInventory.setInventorySlotContents(0, var3);
090 this.theMerchantInventory.setInventorySlotContents(1, var4);
091 this.theMerchant.useRecipe(var2);
092 }
093 }
094 }
095
096 private boolean func_75230_a(MerchantRecipe par1MerchantRecipe, ItemStack par2ItemStack, ItemStack par3ItemStack)
097 {
098 ItemStack var4 = par1MerchantRecipe.getItemToBuy();
099 ItemStack var5 = par1MerchantRecipe.getSecondItemToBuy();
100
101 if (par2ItemStack != null && par2ItemStack.itemID == var4.itemID)
102 {
103 if (var5 != null && par3ItemStack != null && var5.itemID == par3ItemStack.itemID)
104 {
105 par2ItemStack.stackSize -= var4.stackSize;
106 par3ItemStack.stackSize -= var5.stackSize;
107 return true;
108 }
109
110 if (var5 == null && par3ItemStack == null)
111 {
112 par2ItemStack.stackSize -= var4.stackSize;
113 return true;
114 }
115 }
116
117 return false;
118 }
119 }