    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().func_82787_a(this.craftMatrix, this.worldObj));
    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        public ItemStack func_82846_b(EntityPlayer par1EntityPlayer, int par2)
    082        {
    083            ItemStack var3 = null;
    084            Slot var4 = (Slot)this.inventorySlots.get(par2);
    085
    086            if (var4 != null && var4.getHasStack())
    087            {
    088                ItemStack var5 = var4.getStack();
    089                var3 = var5.copy();
    090
    091                if (par2 == 0)
    092                {
    093                    if (!this.mergeItemStack(var5, 10, 46, true))
    094                    {
    095                        return null;
    096                    }
    097
    098                    var4.onSlotChange(var5, var3);
    099                }
    100                else if (par2 >= 10 && par2 < 37)
    101                {
    102                    if (!this.mergeItemStack(var5, 37, 46, false))
    103                    {
    104                        return null;
    105                    }
    106                }
    107                else if (par2 >= 37 && par2 < 46)
    108                {
    109                    if (!this.mergeItemStack(var5, 10, 37, false))
    110                    {
    111                        return null;
    112                    }
    113                }
    114                else if (!this.mergeItemStack(var5, 10, 46, false))
    115                {
    116                    return null;
    117                }
    118
    119                if (var5.stackSize == 0)
    120                {
    121                    var4.putStack((ItemStack)null);
    122                }
    123                else
    124                {
    125                    var4.onSlotChanged();
    126                }
    127
    128                if (var5.stackSize == var3.stackSize)
    129                {
    130                    return null;
    131                }
    132
    133                var4.func_82870_a(par1EntityPlayer, var5);
    134            }
    135
    136            return var3;
    137        }
    138    }