TOPEKA, Kan. — A Kansas man whose lawyer says he is mentally ill pleaded guilty Wednesday in connection with a plot to plant a bomb outside an Army post, acknowledging that he wanted to aid the Islamic State of Iraq and Syria (ISIS).

John T. Booker Jr. of Topeka admitted in court to trying to set off what he thought was a 1,000-pound bomb in a van outside Fort Riley, about 60 miles west of Topeka. He signed a plea agreement with prosecutors that recommends he spend 30 years in prison.

Booker, 21, was arrested outside the post in April 2015 as he was trying to arm what he believed was the bomb. He had been plotting the bombing with two contacts who actually were confidential FBI sources.

“If this defendant had succeeded, American soldiers would have died,” U.S. Attorney Barry Grissom said in a news release, CBS affiliate WIBW reported. “The investigators and the attorneys who worked on this case were our line of defense against terrorism. They kept us safe.”

During the hearing, defense attorney Kirk Redmond told U.S. District Judge Carlos Murguia that Booker has been diagnosed with bipolar disorder, which is characterized by mood swings that can affect functioning. Redmond said Booker is taking medication for the disorder, which Booker confirmed in court.

Neither Redmond nor Booker said whether the disorder was a factor in Booker’s actions. A Muslim cleric who counseled Booker before his arrest said at the time that Booker was acting strangely in the days leading up to it.

Booker pleaded guilty to one count of attempting to use a weapon of mass destruction and one count of attempting to destroy government property with an explosive device. The plea agreement recommended a sentence of 30 years on the first charge and 20 years on the second, to be served at the same time. A conviction on the weapons-of-mass-destruction charge could have led to life in prison.

As part of the plea deal, prosecutors dropped a charge of attempting to provide material support to ISIS.