    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 var4;
    025        int var5;
    026
    027        for (var4 = 0; var4 < 2; ++var4)
    028        {
    029            for (var5 = 0; var5 < 2; ++var5)
    030            {
    031                this.addSlotToContainer(new Slot(this.craftMatrix, var5 + var4 * 2, 88 + var5 * 18, 26 + var4 * 18));
    032            }
    033        }
    034
    035        for (var4 = 0; var4 < 4; ++var4)
    036        {
    037            this.addSlotToContainer(new SlotArmor(this, par1InventoryPlayer, par1InventoryPlayer.getSizeInventory() - 1 - var4, 8, 8 + var4 * 18, var4));
    038        }
    039
    040        for (var4 = 0; var4 < 3; ++var4)
    041        {
    042            for (var5 = 0; var5 < 9; ++var5)
    043            {
    044                this.addSlotToContainer(new Slot(par1InventoryPlayer, var5 + (var4 + 1) * 9, 8 + var5 * 18, 84 + var4 * 18));
    045            }
    046        }
    047
    048        for (var4 = 0; var4 < 9; ++var4)
    049        {
    050            this.addSlotToContainer(new Slot(par1InventoryPlayer, var4, 8 + var4 * 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 var2 = 0; var2 < 4; ++var2)
    072        {
    073            ItemStack var3 = this.craftMatrix.getStackInSlotOnClosing(var2);
    074
    075            if (var3 != null)
    076            {
    077                par1EntityPlayer.dropPlayerItem(var3);
    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 var3 = null;
    095        Slot var4 = (Slot)this.inventorySlots.get(par2);
    096
    097        if (var4 != null && var4.getHasStack())
    098        {
    099            ItemStack var5 = var4.getStack();
    100            var3 = var5.copy();
    101
    102            if (par2 == 0)
    103            {
    104                if (!this.mergeItemStack(var5, 9, 45, true))
    105                {
    106                    return null;
    107                }
    108
    109                var4.onSlotChange(var5, var3);
    110            }
    111            else if (par2 >= 1 && par2 < 5)
    112            {
    113                if (!this.mergeItemStack(var5, 9, 45, false))
    114                {
    115                    return null;
    116                }
    117            }
    118            else if (par2 >= 5 && par2 < 9)
    119            {
    120                if (!this.mergeItemStack(var5, 9, 45, false))
    121                {
    122                    return null;
    123                }
    124            }
    125            else if (var3.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)var3.getItem()).armorType)).getHasStack())
    126            {
    127                int var6 = 5 + ((ItemArmor)var3.getItem()).armorType;
    128
    129                if (!this.mergeItemStack(var5, var6, var6 + 1, false))
    130                {
    131                    return null;
    132                }
    133            }
    134            else if (par2 >= 9 && par2 < 36)
    135            {
    136                if (!this.mergeItemStack(var5, 36, 45, false))
    137                {
    138                    return null;
    139                }
    140            }
    141            else if (par2 >= 36 && par2 < 45)
    142            {
    143                if (!this.mergeItemStack(var5, 9, 36, false))
    144                {
    145                    return null;
    146                }
    147            }
    148            else if (!this.mergeItemStack(var5, 9, 45, false))
    149            {
    150                return null;
    151            }
    152
    153            if (var5.stackSize == 0)
    154            {
    155                var4.putStack((ItemStack)null);
    156            }
    157            else
    158            {
    159                var4.onSlotChanged();
    160            }
    161
    162            if (var5.stackSize == var3.stackSize)
    163            {
    164                return null;
    165            }
    166
    167            var4.onPickupFromSlot(par1EntityPlayer, var5);
    168        }
    169
    170        return var3;
    171    }
    172}