7

I would like to detect when a player attacks/hits/hurts another player or entity. I have tried to use the AttackTime tag, but it doesn't work. Here is what I have tried (on a 20HZ clock):

First command block:

scoreboard players set @a Attack 0 {AttackTime:0s}

Second command block:

scoreboard players set @a Attack 1 {AttackTime:1s}

Third command block:

tellraw @a[score_Attack_min=1] "Nice shot!"
10

I figured out how to do this. I use the stat.damageDealt scoreboard objective. First I run the following command once:

/scoreboard objectives add hit stat.damageDealt

Then on the 20Hz clock I do the following:

  • First command block:

    /tellraw @a[score_hit_min=1] "Nice Hit!"

  • Second command block:

    /scoreboard players reset @a[score_hit_min=1] hit

How this works is that points is added to the stat.damageDealt objective each time they hit/attack/hurt an entity or player. Then it runs the command on every player that has hurt another entity or player. When that command is done, the objective gets reset.

How I found this is that on the statistics menu from the pause menu, there is a statistic called Damage Dealt.

EDIT:

In Minecraft 1.9, use the repeating command block instead of a 20Hz clock.

  • I've been working on a different solution, which is much more complicated. I feel stupid now. – MrLemon Jul 6 '15 at 11:41
  • 2
    @MrLemon Oh, well you can post it, maybe it will be helpful to some players. – Jacques Marais Jul 6 '15 at 11:42
  • @MrLemon That is if it is done. – Jacques Marais Jul 6 '15 at 11:42
  • I ended up using something I found out while fiddling around with my solution (which was just awful to be hones) to upgrade your solution. – MrLemon Jul 6 '15 at 12:12
  • I'm a little surprised that stat.damageDealt isn't further subdivided into the various mobs and players. – MBraedley Jul 6 '15 at 21:07
3

This answer is based on @CommandFox's answer and my own (totally awful) idea for a solution. However, I found that combination of the two adds a possible limitation to hurting specific entities.

First, create two scoreboard objectives: didDamage and hurtTime

scoreboard objectives add didDamage stat.damageDealt
scoreboard objectives add hurtTime dummy

Then, create a fill clock and run the following commands:

scoreboard players add @e hurtTime 1 
scoreboard players set @e hurtTime 0 {HurtTime:0s}

execute @e[score_hurtTime_min=1,score_hurtTime=1] ~ ~ ~ tellraw @a[score_didDamage=1] "Nice hit!"
scoreboard player set @a didDamage 0

So far, we have gained nothing by using the hurtTime objective. The main benefit of it is that it can be limited using its target selector. For example, using

scoreboard players add @e[type=Zombie] hurtTime 1 
scoreboard players set @e[type=Zombie] hurtTime 0 {HurtTime:0s}

makes it so that the message only appears when you actually hit a zombie. Using multiples of this command block pair makes it possible to select multiple entities.

  • This solution is great for when you want to limit the mobs or players that the player may hit. Thank you. – Jacques Marais Jul 6 '15 at 12:20
  • @CommandFox I'm just glad I was able to salvage something useful from my attempt after all. For players, you could also use stat.damageTaken instead, but stat-objectives do not work for other entities. – MrLemon Jul 6 '15 at 12:26
  • Incidentally, I found out that after hitting a mob, it is invincible (glowing red) for exactly 10 ticks (1/2 sec). – MrLemon Jul 6 '15 at 12:27
  • Yes, thank you for your help. I have a use for this on one of my command block minigames. – Jacques Marais Jul 6 '15 at 12:27
  • Wow, I didn't even know that. Thanks :D – Jacques Marais Jul 6 '15 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.