FARMINGTON, Maine — A Chesterville man pleaded guilty Friday to shooting at his roommate inside a home Nov. 19, 2012. State police previously said that the bullet just missed the roommate.

Philip Caldwell Jr., 29, pleaded guilty at Franklin County Superior Court to felony charges of reckless conduct with a firearm, criminal threatening with a dangerous weapon and trafficking in marijuana, Assistant District Attorney James Andrews said.

Justice Michaela Murphy accepted an agreement to defer sentencing for two years. During that time, Caldwell must undergo substance abuse evaluation and counseling, not possess or use dangerous weapons, including firearms, and adhere to other conditions in the agreement. He is subject to random search and testing, Andrews said.

State police conducted an investigation after a complaint was received from the roommate. They discovered the two had a dispute at the house, Sgt. Peter Michaud said in November. The roommate told police that Caldwell pointed the handgun at him and fired a single shot that nearly struck him, Michaud said then.

If Caldwell is successful in the two-year agreement, he will be allowed to withdraw his pleas to the felony charges and plead to misdemeanor charges. If he violates the agreement, there is a minimum mandatory one-year sentence.