An NYPD sergeant urged a Midtown Starbucks to stop giving free food to the homeless after dozens of disturbance calls at the location.

But the coffeehouse, not far from the Port Authority Bus Terminal, ignored the warning and that same cop wound up being assaulted by a vagrant — who was angry over the quality of his free croissant, the officer charges in a lawsuit.

“I warned that someone would end up getting hurt — it was an unsafe environment. It turned out to be me,” Sgt. Timothy Wall told The Post.

And despite 80 disturbances involving vagrants at the latte palace in 2017 alone, according to Wall’s suit, Starbucks says the cop has no one to blame but himself.

Starbucks lawyers say Wall brought on his injuries through his own “culpable conduct . . . and assumption of risk,” according to response papers filed in February.

Wall, a community affairs officer with the Midtown South Precinct — whose job is advising businesses on crime reduction — said he and colleagues repeatedly told managers at the branch at Eighth Avenue and West 39th Street to stop giving away food.

They said it was attracting emotionally disturbed individuals who urinated on cafe seats, got high in a second-floor seating area and assaulted patrons.

In fact, cops were called to that Starbucks for such disturbances 80 times in 2017, the suit alleges.

Midtown South officers including Wall met with local and regional Starbucks managers no fewer than five times over the course of 2015, 2016 and 2017 to advise them on alternative ways to help the needy — such as donating to food banks or setting up a distribution center off site, the suit states.

“I want to help people in need as much as anyone else, but I explained to [Starbucks] that there was a time and place to do it,” said Wall, a second-generation officer and 10-year NYPD veteran.

“They call us for help, and when we gave them advice, they brushed it aside. It went in one ear and out the other.”

Then Wall became a victim.

He and his partner responded to the Starbucks around 3 p.m. Sept. 18 as 24-year-old Montclair Mobley was throwing a temper tantrum over the French breakfast roll the store gave him.

Mobley was so put off by the on-the-house croissant that he whined about wanting a “time machine” so he could go back to earlier in the day — when, he said, Starbucks was giving away better grub, said the suit, filed in December by Wall’s attorney, Andrew Carboy.

Starbucks’ hired security cowered at the far end of the store and would only point cops in Mobley’s direction, the suit claims.

When Wall and his partner tried to get the man to leave, he threatened to “kill everyone” in the restaurant, the court papers state.

Wall tried to detain Mobley, but the nut shoved him into a wall, dislocating the cop’s right shoulder, according to police and court documents.

The attack reinflamed a 2014 line-of-duty shoulder injury that required surgery, according to a doctor’s report.

The injuries sidelined Wall from work for months. He needed surgery in February to repair torn shoulder cartilage, and seven months after the attack, he still has to go to physical therapy.

Plus he’s been stuck behind a desk while he continues to heal.

Workers at the store Wednesday were unaware of any food giveaway, but said there had been a problem with loitering and “fights” six months ago.

“The food donation program described in the complaint does not exist,” said company spokesman Reggie Borges.

Additional reporting by Kevin Fasick