A new dad caught trespassing at an apartment block had gone into the building to take drugs.
Tautvydas Tamulis (31) had no reason to be in the city centre complex when gardai arrested him.
Tamulis's lawyer told a court that he had a substance abuse problem at the time.
The defendant, who has several previous convictions, was told he could avoid a jail sentence if he was found suitable to carry out community service instead.
Judge Anthony Halpin adjourned his case for four weeks for a report to be produced.
Tamulis, a father-of-one of Ashwood Green, Ashbourne, Co Meath pleaded guilty to trespassing.
Dublin District Court heard that the incident happened at Temple Hall Apartments on Parnell Street in February.
Gardai responded to a report of an intruder at the apartment complex.
Tamulis said he had no reason to be there when questioned and was arrested for trespassing and brought to a garda station, where he was charged.
The court heard that he had a number of previous convictions for offences including possession of drugs, assault, theft and motoring offences.
The defendant had had difficulties with alcohol and drugs in the past but was now clean of all illicit substances, his solicitor, Niall O'Connor, said.
His partner had recently given birth to their first child, the court was told.
Tamulis, who is originally from Lithuania, was a labourer by trade and had recently been offered work in construction.
"Why did he do it?" Judge Halpin asked.
"He had a drug difficulty at the time," Mr O'Connor replied.
"He was trespassing in the apartments for the purpose of taking drugs. He is off all substances."
He asked the judge to be as lenient as he could in the circumstances and to take his client's guilty plea into consideration.
Judge Anthony Halpin said he would order Tamulis to carry out 100 hours of community service instead of a two-month prison sentence.