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