    001    package net.minecraft.src;
    002
    003    public class ContainerPlayer extends Container
    004    {
    005        /** The crafting matrix inventory. */
    006        public InventoryCrafting craftMatrix;
    007        public IInventory craftResult;
    008
    009        /** Determines if inventory manipulation should be handled. */
    010        public boolean isLocalWorld;
    011
    012        public ContainerPlayer(InventoryPlayer par1InventoryPlayer)
    013        {
    014            this(par1InventoryPlayer, true);
    015        }
    016
    017        public ContainerPlayer(InventoryPlayer par1InventoryPlayer, boolean par2)
    018        {
    019            this.craftMatrix = new InventoryCrafting(this, 2, 2);
    020            this.craftResult = new InventoryCraftResult();
    021            this.isLocalWorld = false;
    022            this.isLocalWorld = par2;
    023            this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 144, 36));
    024            int var3;
    025            int var4;
    026
    027            for (var3 = 0; var3 < 2; ++var3)
    028            {
    029                for (var4 = 0; var4 < 2; ++var4)
    030                {
    031                    this.addSlotToContainer(new Slot(this.craftMatrix, var4 + var3 * 2, 88 + var4 * 18, 26 + var3 * 18));
    032                }
    033            }
    034
    035            for (var3 = 0; var3 < 4; ++var3)
    036            {
    037                this.addSlotToContainer(new SlotArmor(this, par1InventoryPlayer, par1InventoryPlayer.getSizeInventory() - 1 - var3, 8, 8 + var3 * 18, var3));
    038            }
    039
    040            for (var3 = 0; var3 < 3; ++var3)
    041            {
    042                for (var4 = 0; var4 < 9; ++var4)
    043                {
    044                    this.addSlotToContainer(new Slot(par1InventoryPlayer, var4 + (var3 + 1) * 9, 8 + var4 * 18, 84 + var3 * 18));
    045                }
    046            }
    047
    048            for (var3 = 0; var3 < 9; ++var3)
    049            {
    050                this.addSlotToContainer(new Slot(par1InventoryPlayer, var3, 8 + var3 * 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));
    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 to transfer a stack from one inventory to the other eg. when shift clicking.
    091         */
    092        public ItemStack transferStackInSlot(int par1)
    093        {
    094            ItemStack var2 = null;
    095            Slot var3 = (Slot)this.inventorySlots.get(par1);
    096
    097            if (var3 != null && var3.getHasStack())
    098            {
    099                ItemStack var4 = var3.getStack();
    100                var2 = var4.copy();
    101
    102                if (par1 == 0)
    103                {
    104                    if (!this.mergeItemStack(var4, 9, 45, true))
    105                    {
    106                        return null;
    107                    }
    108
    109                    var3.onSlotChange(var4, var2);
    110                }
    111                else if (par1 >= 1 && par1 < 5)
    112                {
    113                    if (!this.mergeItemStack(var4, 9, 45, false))
    114                    {
    115                        return null;
    116                    }
    117                }
    118                else if (par1 >= 5 && par1 < 9)
    119                {
    120                    if (!this.mergeItemStack(var4, 9, 45, false))
    121                    {
    122                        return null;
    123                    }
    124                }
    125                else if (var2.getItem() instanceof ItemArmor && !((Slot)this.inventorySlots.get(5 + ((ItemArmor)var2.getItem()).armorType)).getHasStack())
    126                {
    127                    int var5 = 5 + ((ItemArmor)var2.getItem()).armorType;
    128
    129                    if (!this.mergeItemStack(var4, var5, var5 + 1, false))
    130                    {
    131                        return null;
    132                    }
    133                }
    134                else if (par1 >= 9 && par1 < 36)
    135                {
    136                    if (!this.mergeItemStack(var4, 36, 45, false))
    137                    {
    138                        return null;
    139                    }
    140                }
    141                else if (par1 >= 36 && par1 < 45)
    142                {
    143                    if (!this.mergeItemStack(var4, 9, 36, false))
    144                    {
    145                        return null;
    146                    }
    147                }
    148                else if (!this.mergeItemStack(var4, 9, 45, false))
    149                {
    150                    return null;
    151                }
    152
    153                if (var4.stackSize == 0)
    154                {
    155                    var3.putStack((ItemStack)null);
    156                }
    157                else
    158                {
    159                    var3.onSlotChanged();
    160                }
    161
    162                if (var4.stackSize == var2.stackSize)
    163                {
    164                    return null;
    165                }
    166
    167                var3.onPickupFromSlot(var4);
    168            }
    169
    170            return var2;
    171        }
    172    }