In my (unfortunately .. far too extensive) experience .. compuilsive drug use is all about triggers. Your mental thought patterns sound just like that sort of trigger. You've been clean no problems for ages but now it's back and the trigger has restarted.
It's hard dealing with that shit and the best advice I can give is likely very personal. I recommend finding something compulsive you'll enjoy and hammering it till the feeling goes. Computer games are awseome (avoid that free to play take all your money crap) as they use the same conditioned response mechanisms .. but if you don't do that .. just being self indulgent can work. Watch you're favorite film / TV series repeatedly ... go for rigorous excersise (a great one) .. though be careful you're fit enough. The endorphins from cycling flat out up a 1 in 3 hill will I guarantee get your mind off it .. and for longer than you're lying in the ditch trying to get your breath back.
Beyond that it's "healthy," lifestyle activities that are productive. But when you're in the hard part like you are now ... they can just be too hard to do. I like to code .. so I'll start contributing to a free softrware project when I get well. Each to their own - but that sort of thing drives you to wellness.
And now you know opioids are bad for you. Don't use them for pain except in extreme need. If you're terminal cancer go nuts - as you'll be on em for life anyway .. but unless it's gonna be permanent ask for something else. If the pain won't pass .. then .. well .. good luck to you as you're gonna need it .. opioids are a good solution but it's likely they won't keep giving them to you and you'll wind up on smack or something.
And for full self disclosure .. I'm a smack addict .. have an amphetmaine habit .. and won't live much past 50. That leaves me peculiarly well placed to help .. but also to harm.