    001    package net.minecraft.src;
    002
    003    public class ContainerWorkbench extends Container
    004    {
    005        /** The crafting matrix inventory (3x3). */
    006        public InventoryCrafting craftMatrix = new InventoryCrafting(this, 3, 3);
    007        public IInventory craftResult = new InventoryCraftResult();
    008        private World worldObj;
    009        private int posX;
    010        private int posY;
    011        private int posZ;
    012
    013        public ContainerWorkbench(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
    014        {
    015            this.worldObj = par2World;
    016            this.posX = par3;
    017            this.posY = par4;
    018            this.posZ = par5;
    019            this.addSlotToContainer(new SlotCrafting(par1InventoryPlayer.player, this.craftMatrix, this.craftResult, 0, 124, 35));
    020            int var6;
    021            int var7;
    022
    023            for (var6 = 0; var6 < 3; ++var6)
    024            {
    025                for (var7 = 0; var7 < 3; ++var7)
    026                {
    027                    this.addSlotToContainer(new Slot(this.craftMatrix, var7 + var6 * 3, 30 + var7 * 18, 17 + var6 * 18));
    028                }
    029            }
    030
    031            for (var6 = 0; var6 < 3; ++var6)
    032            {
    033                for (var7 = 0; var7 < 9; ++var7)
    034                {
    035                    this.addSlotToContainer(new Slot(par1InventoryPlayer, var7 + var6 * 9 + 9, 8 + var7 * 18, 84 + var6 * 18));
    036                }
    037            }
    038
    039            for (var6 = 0; var6 < 9; ++var6)
    040            {
    041                this.addSlotToContainer(new Slot(par1InventoryPlayer, var6, 8 + var6 * 18, 142));
    042            }
    043
    044            this.onCraftMatrixChanged(this.craftMatrix);
    045        }
    046
    047        /**
    048         * Callback for when the crafting matrix is changed.
    049         */
    050        public void onCraftMatrixChanged(IInventory par1IInventory)
    051        {
    052            this.craftResult.setInventorySlotContents(0, CraftingManager.getInstance().findMatchingRecipe(this.craftMatrix));
    053        }
    054
    055        /**
    056         * Callback for when the crafting gui is closed.
    057         */
    058        public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    059        {
    060            super.onCraftGuiClosed(par1EntityPlayer);
    061
    062            if (!this.worldObj.isRemote)
    063            {
    064                for (int var2 = 0; var2 < 9; ++var2)
    065                {
    066                    ItemStack var3 = this.craftMatrix.getStackInSlotOnClosing(var2);
    067
    068                    if (var3 != null)
    069                    {
    070                        par1EntityPlayer.dropPlayerItem(var3);
    071                    }
    072                }
    073            }
    074        }
    075
    076        public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    077        {
    078            return this.worldObj.getBlockId(this.posX, this.posY, this.posZ) != Block.workbench.blockID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
    079        }
    080
    081        /**
    082         * Called to transfer a stack from one inventory to the other eg. when shift clicking.
    083         */
    084        public ItemStack transferStackInSlot(int par1)
    085        {
    086            ItemStack var2 = null;
    087            Slot var3 = (Slot)this.inventorySlots.get(par1);
    088
    089            if (var3 != null && var3.getHasStack())
    090            {
    091                ItemStack var4 = var3.getStack();
    092                var2 = var4.copy();
    093
    094                if (par1 == 0)
    095                {
    096                    if (!this.mergeItemStack(var4, 10, 46, true))
    097                    {
    098                        return null;
    099                    }
    100
    101                    var3.onSlotChange(var4, var2);
    102                }
    103                else if (par1 >= 10 && par1 < 37)
    104                {
    105                    if (!this.mergeItemStack(var4, 37, 46, false))
    106                    {
    107                        return null;
    108                    }
    109                }
    110                else if (par1 >= 37 && par1 < 46)
    111                {
    112                    if (!this.mergeItemStack(var4, 10, 37, false))
    113                    {
    114                        return null;
    115                    }
    116                }
    117                else if (!this.mergeItemStack(var4, 10, 46, false))
    118                {
    119                    return null;
    120                }
    121
    122                if (var4.stackSize == 0)
    123                {
    124                    var3.putStack((ItemStack)null);
    125                }
    126                else
    127                {
    128                    var3.onSlotChanged();
    129                }
    130
    131                if (var4.stackSize == var2.stackSize)
    132                {
    133                    return null;
    134                }
    135
    136                var3.onPickupFromSlot(var4);
    137            }
    138
    139            return var2;
    140        }
    141    }