    001package net.minecraft.inventory;
    002
    003import net.minecraft.entity.player.EntityPlayer;
    004import net.minecraft.entity.player.InventoryPlayer;
    005import net.minecraft.item.ItemArmor;
    006import net.minecraft.item.ItemStack;
    007import net.minecraft.item.crafting.CraftingManager;
    008
    009public class ContainerPlayer extends Container
    010{
    011    /** The crafting matrix inventory. */
    012    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
    013    public IInventory craftResult = new InventoryCraftResult();
    014
    015    /** Determines if inventory manipulation should be handled. */
    016    public boolean isLocalWorld = false;
    017    private final EntityPlayer thePlayer;
    018
    019    public ContainerPlayer(InventoryPlayer par1InventoryPlayer, boolean par2, EntityPlayer par3EntityPlayer)
    020    {
    021        this.isLocalWorld = par2;
    022        this.thePlayer = par3EntityPlayer;
    023        this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 144, 36));
    024        int i;
    025        int j;
    026
    027        for (i = 0; i < 2; ++i)
    028        {
    029            for (j = 0; j < 2; ++j)
    030            {
    031                this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2, 88 + j * 18, 26 + i * 18));
    032            }
    033        }
    034
    035        for (i = 0; i < 4; ++i)
    036        {
    037            this.addSlotToContainer(new SlotArmor(this, par1InventoryPlayer, par1InventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18, i));
    038        }
    039
    040        for (i = 0; i < 3; ++i)
    041        {
    042            for (j = 0; j < 9; ++j)
    043            {
    044                this.addSlotToContainer(new Slot(par1InventoryPlayer, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
    045            }
    046        }
    047
    048        for (i = 0; i < 9; ++i)
    049        {
    050            this.addSlotToContainer(new Slot(par1InventoryPlayer, i, 8 + i * 18, 142));
    051        }
    052
    053        this.onCraftMatrixChanged(this.craftMatrix);
    054    }
    055
    056    /**
    057     * Callback for when the crafting matrix is changed.
    058     */
    059    public void onCraftMatrixChanged(IInventory par1IInventory)
    060    {
    061        this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix, this.thePlayer.worldObj));
    062    }
    063
    064    /**
    065     * Callback for when the crafting gui is closed.
    066     */
    067    public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    068    {
    069        super.onCraftGuiClosed(par1EntityPlayer);
    070
    071        for (int i = 0; i < 4; ++i)
    072        {
    073            ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
    074
    075            if (itemstack != null)
    076            {
    077                par1EntityPlayer.dropPlayerItem(itemstack);
    078            }
    079        }
    080
    081        this.craftResult.setInventorySlotContents(0, (ItemStack)null);
    082    }
    083
    084    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    085    {
    086        return true;
    087    }
    088
    089    /**
    090     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    091     */
    092    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    093    {
    094        ItemStack itemstack = null;
    095        Slot slot = (Slot)this.inventorySlots.get(par2);
    096
    097        if (slot != null && slot.getHasStack())
    098        {
    099            ItemStack itemstack1 = slot.getStack();
    100            itemstack = itemstack1.copy();
    101
    102            if (par2 == 0)
    103            {
    104                if (!this.mergeItemStack(itemstack1, 9, 45, true))
    105                {
    106                    return null;
    107                }
    108
    109                slot.onSlotChange(itemstack1, itemstack);
    110            }
    111            else if (par2 >= 1 && par2 < 5)
    112            {
    113                if (!this.mergeItemStack(itemstack1, 9, 45, false))
    114                {
    115                    return null;
    116                }
    117            }
    118            else if (par2 >= 5 && par2 < 9)
    119            {
    120                if (!this.mergeItemStack(itemstack1, 9, 45, false))
    121                {
    122                    return null;
    123                }
    124            }
    125            else if (itemstack.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)itemstack.getItem()).armorType)).getHasStack())
    126            {
    127                int j = 5 + ((ItemArmor)itemstack.getItem()).armorType;
    128
    129                if (!this.mergeItemStack(itemstack1, j, j + 1, false))
    130                {
    131                    return null;
    132                }
    133            }
    134            else if (par2 >= 9 && par2 < 36)
    135            {
    136                if (!this.mergeItemStack(itemstack1, 36, 45, false))
    137                {
    138                    return null;
    139                }
    140            }
    141            else if (par2 >= 36 && par2 < 45)
    142            {
    143                if (!this.mergeItemStack(itemstack1, 9, 36, false))
    144                {
    145                    return null;
    146                }
    147            }
    148            else if (!this.mergeItemStack(itemstack1, 9, 45, false))
    149            {
    150                return null;
    151            }
    152
    153            if (itemstack1.stackSize == 0)
    154            {
    155                slot.putStack((ItemStack)null);
    156            }
    157            else
    158            {
    159                slot.onSlotChanged();
    160            }
    161
    162            if (itemstack1.stackSize == itemstack.stackSize)
    163            {
    164                return null;
    165            }
    166
    167            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
    168        }
    169
    170        return itemstack;
    171    }
    172
    173    public boolean func_94530_a(ItemStack par1ItemStack, Slot par2Slot)
    174    {
    175        return par2Slot.inventory != this.craftResult && super.func_94530_a(par1ItemStack, par2Slot);
    176    }
    177}