I’ve had this exact same issue. You just have to replace the innards.If the flapper (which closes and stops water for moving down) is not in good shape,It will lead to the hammering noise. It wasn’t just the flapper but it was part of it.
I had to replace all the insides twice. Don’t go cheap.
The first time I had a handyman come out and take a look at it. He couldn’t fix it. Then I had a plumber come out and do it and I haven’t had an issue since.
It’s not difficult to do – pretty simple. The only reason I had somebody come out is I didn’t know why it was making the sound. But simply, gut the inside of the tank and replace everything.