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