@Override
    public ItemStack transferStackInSlot(EntityPlayer player, int slot)
    {
        ItemStack stack = null;
        Slot slotObj = (Slot) inventorySlots.get(slot);

        if (slotObj != null && slotObj.getHasStack())
        {
            ItemStack stackInSlot = slotObj.getStack();
            stack = stackInSlot.copy();

            if (slot < 4)
            {
                if (!mergeItemStack(stackInSlot, 4, 40, true))
                    return null;

            } else if (slot < 39)
            {
                if (!mergeItemStack(stackInSlot, 0, 3, false))
                    return null;
            }

            if (stackInSlot.stackSize == 0)
                slotObj.putStack(null);
            else
                slotObj.onSlotChanged();
        }

        return stack;
    }