    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 thePlayer;
    012
    013        public ContainerPlayer(InventoryPlayer par1InventoryPlayer, boolean par2, EntityPlayer par3EntityPlayer)
    014        {
    015            this.isLocalWorld = par2;
    016            this.thePlayer = 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().findMatchingRecipe(this.craftMatrix, this.thePlayer.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        /**
    084         * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    085         */
    086        public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    087        {
    088            ItemStack var3 = null;
    089            Slot var4 = (Slot)this.inventorySlots.get(par2);
    090
    091            if (var4 != null && var4.getHasStack())
    092            {
    093                ItemStack var5 = var4.getStack();
    094                var3 = var5.copy();
    095
    096                if (par2 == 0)
    097                {
    098                    if (!this.mergeItemStack(var5, 9, 45, true))
    099                    {
    100                        return null;
    101                    }
    102
    103                    var4.onSlotChange(var5, var3);
    104                }
    105                else if (par2 >= 1 && par2 < 5)
    106                {
    107                    if (!this.mergeItemStack(var5, 9, 45, false))
    108                    {
    109                        return null;
    110                    }
    111                }
    112                else if (par2 >= 5 && par2 < 9)
    113                {
    114                    if (!this.mergeItemStack(var5, 9, 45, false))
    115                    {
    116                        return null;
    117                    }
    118                }
    119                else if (var3.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)var3.getItem()).armorType)).getHasStack())
    120                {
    121                    int var6 = 5 + ((ItemArmor)var3.getItem()).armorType;
    122
    123                    if (!this.mergeItemStack(var5, var6, var6 + 1, false))
    124                    {
    125                        return null;
    126                    }
    127                }
    128                else if (par2 >= 9 && par2 < 36)
    129                {
    130                    if (!this.mergeItemStack(var5, 36, 45, false))
    131                    {
    132                        return null;
    133                    }
    134                }
    135                else if (par2 >= 36 && par2 < 45)
    136                {
    137                    if (!this.mergeItemStack(var5, 9, 36, false))
    138                    {
    139                        return null;
    140                    }
    141                }
    142                else if (!this.mergeItemStack(var5, 9, 45, false))
    143                {
    144                    return null;
    145                }
    146
    147                if (var5.stackSize == 0)
    148                {
    149                    var4.putStack((ItemStack)null);
    150                }
    151                else
    152                {
    153                    var4.onSlotChanged();
    154                }
    155
    156                if (var5.stackSize == var3.stackSize)
    157                {
    158                    return null;
    159                }
    160
    161                var4.onPickupFromSlot(par1EntityPlayer, var5);
    162            }
    163
    164            return var3;
    165        }
    166    }