Matt Barnes is getting court-ordered protection from his fiancée's ex-husband ... after the former NBA champ said the guy had terrorized him in the lead-up to a wild spitting incident at the 49ers game on Sunday.
Barnes got a judge to sign off on a temporary restraining order against David Patterson Jr. on Wednesday ... just days after he spat on the man in a concourse at Levi's Stadium before the S.F. vs Dallas playoff game.
Barnes' TRO is slated to last until Feb. 16 -- when a hearing on the matter will go down in Los Angeles.
As part of the order, 37-year-old Patterson Jr. is required to have no contact with Barnes ... and must stay at least 100 yards away from the ex-Golden State Warriors forward. Patterson Jr. has also been ordered to have no firearms.
As we reported, 42-year-old Barnes initially filed for the TRO on Tuesday -- after he alleged Patterson Jr. had made his life hell the past few months.
Barnes claimed in his TRO application that Patterson Jr. was "jealous of my relationship with my ex-wife" ... and had harassed him repeatedly. At one point, according to Barnes, Patterson Jr. had threatened to shoot him with a gun.
Barnes also claimed in the documents that Patterson Jr. "hunted" him down at the 49ers game on Sunday ... and "aggressively confronted" him. Barnes said the guy yelled profanities and threats, and shoved him. It all ultimately led to Barnes spitting on him.
Barnes said in the documents he was worried Patterson Jr. has been "trying to bait me to damage my reputation and hurt my career."