    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, 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        /**
    082         * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    083         */
    084        public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    085        {
    086            ItemStack var3 = null;
    087            Slot var4 = (Slot)this.inventorySlots.get(par2);
    088
    089            if (var4 != null && var4.getHasStack())
    090            {
    091                ItemStack var5 = var4.getStack();
    092                var3 = var5.copy();
    093
    094                if (par2 == 0)
    095                {
    096                    if (!this.mergeItemStack(var5, 10, 46, true))
    097                    {
    098                        return null;
    099                    }
    100
    101                    var4.onSlotChange(var5, var3);
    102                }
    103                else if (par2 >= 10 && par2 < 37)
    104                {
    105                    if (!this.mergeItemStack(var5, 37, 46, false))
    106                    {
    107                        return null;
    108                    }
    109                }
    110                else if (par2 >= 37 && par2 < 46)
    111                {
    112                    if (!this.mergeItemStack(var5, 10, 37, false))
    113                    {
    114                        return null;
    115                    }
    116                }
    117                else if (!this.mergeItemStack(var5, 10, 46, false))
    118                {
    119                    return null;
    120                }
    121
    122                if (var5.stackSize == 0)
    123                {
    124                    var4.putStack((ItemStack)null);
    125                }
    126                else
    127                {
    128                    var4.onSlotChanged();
    129                }
    130
    131                if (var5.stackSize == var3.stackSize)
    132                {
    133                    return null;
    134                }
    135
    136                var4.onPickupFromSlot(par1EntityPlayer, var5);
    137            }
    138
    139            return var3;
    140        }
    141    }