Disclaimer: I did not test this, but precluding Minecraft weirdness/bugs, this method should work.
@Kcats was on a very good track regarding
stat.damageDealt, but there is one more step to it than just looking at this.
First, set up some scoreboard objectives:
/scoreboard objectives add damageDealt stat.damageDealt
/scoreboard objectives add hits dummy
Now make a fast clock (setblock/fill clock works best) and run the following in order
/scoreboard players add @a[score_damageDealt_min=1] hits 1
/scoreboard players set @a damageDealt 0
By using a second objective, we can decouple hits from damage done. If a player has dealt any kind of damage to anything in the last clock period, it is counted as a hit.
Due to the way this is set up, there are some quirks however:
- A hit that doesn't deal any damage due to armor or resistance effects or whatnot does not count.
- If you set someone on fire, poison or wither someone, every tick of damage probably counts as a hit (here's where I'm unsure of the exact workings of the