He became abusive towards Corrections officers and shimmied up the basketball pole where he remained, the Dunedin District Court heard yesterday.
By 6.30pm, staff decided the best course was to climb a ladder and unscrew the backboard to which Cavenagh clung.
Mattresses were placed as a makeshift landing pad in case the prisoner took a tumble.
He was, however, in no mood to take the easy option.
Once an officer was within a metre, Cavenagh spat at the man three times, hitting him on his hair, safety glasses and left cheek.
Before he could be forcibly removed from the hoop, the defendant leapt down to the mattresses where he was quickly restrained by staff.
Cavenagh admitted afterwards that spitting was "gross" and that he would not do so again.
The country was approaching the end of the Covid-19 lockdown at the time but the man did not have the virus, the court heard.
Defence counsel Sophia Thorburn asked Judge Peter Rollo to sentence her client yesterday for the assault.
But Cavenagh had a more serious violence charge hanging over him.
He will be sentenced on both matters in the Christchurch District Court in September.