How to detect if a mob has been hit in minecraft with command blocks?

How to detect if a mob has been hit in minecraft with command blocks? - White Metal High-rise Viewing Deck Building

i am making a boss battle for my minecraft adventure map, and using a mob spawned with commands as the boss. When the mob gets hit, some command blocks will give the player a potion effect, but I can't figure out how to activate the command block when the mob gets hit. I am in the latest snapshot (16w03a). please help me (the person who answers my question will be given credit for it in my map)



Best Answer

Since automated scoreboard statistics (namely stat.damageTaken) only increments for players, you will need to rely on NBT data for non-player entities.

The HurtTime tag will be set to 10 when a mob is struck, decreasing by 1 per tick until it reaches 0 again.

  1. Adding the label for mobs that were struck.

    /scoreboard players tag @e[type=Creeper,tag=!Hurt] add Hurt {HurtTime:10s}
    
  2. Remove the label when the value is 9.

    /scoreboard players tag @e[type=Creeper,tag=Hurt] remove Hurt {HurtTime:9s}
    
  3. Target the mob based on the "Hurt" tag.

    /execute @e[type=Creeper,tag=Hurt] ~ ~ ~ /effect @a minecraft:slowness
    



Pictures about "How to detect if a mob has been hit in minecraft with command blocks?"

How to detect if a mob has been hit in minecraft with command blocks? - Black and White Dartboard
How to detect if a mob has been hit in minecraft with command blocks? - Red and White Dart on Darts Board
How to detect if a mob has been hit in minecraft with command blocks? - Person Writing While Using Phone



How do you check entity hits in Minecraft?

detectPos is the x y z coordinate to detect the block. command is any Minecraft command that you want the entity to execute such as summon, give, fill, clone, setblock, tp, xp, kill, etc.

Is there a detect command in Minecraft?

One way to do this involves summoning mobs with armor stands riding them. When the armor stand can no longer detect the mob (because it is dead), then it is tagged. You can do whatever you want by executing all armor stands with that tag and then kill.

How do you know when a mob is killed in Minecraft?

Open the chat box by pressing T on your keyboard, or the right d-pad button on a controller. In place of "[your username]," insert your Minecraft username. 4. The command block will appear in your inventory.



Minecraft: How to detect when a mob dies [1.16+]




More answers regarding how to detect if a mob has been hit in minecraft with command blocks?

Answer 2

1.13 Update

The answer in 1.12 and below still applies. Note that commands were overhauled in 1.13. Please note the following:

  • /scoreboard players tag is now /tag.
  • /testfor is now a branch of /execute, using the (if|unless) parameters.
  • The NBT selector has been moved inside the target selector, using the nbt argument.
  • This answer was written before the entity IDs were changed in 1.11. In this change, we see that ZombieVillager and Creeper are now zombie_villager and creeper.

Here are the old commands that used to work but now don't.

/scoreboard players tag @e[type=Creeper,tag=!Hurt] add Hurt {HurtTime:10s}
/scoreboard players tag @e[type=Creeper,tag=Hurt] remove Hurt {HurtTime:9s}
/execute @e[type=Creeper,tag=Hurt] ~ ~ ~ /effect @a minecraft:slowness

And here are the new 1.13+ commands!

/tag @e[type=creeper,tag=!hurt,nbt={HurtTime:10s}] add hurt
/scoreboard players tag @e[type=creeper,tag=hurt,nbt={HurtTime:9s}] remove hurt
/execute if entity @e[type=creeper,tag=hurt] run say No Hurting Creepers!

Answer 3

You can use this command to test for the creature's health. To make this work properly, several command blocks connected to redstone circuits will be necessary. /testfor @e[type=Zombie] {Health:19s} Of course, Zombie can be replaced with any mob name (first letter capital) You would also have to have a redstone comparator heading out of the command block(s) to the command block with the command you want executed after the creature is hit. You would have to have 19 command block circuits, one for each number from 1-19 to make a fully functional circuit/system. If you don't want this, then just find out how much damage should result from the zombie being hit with the available weapon (remember, your fist is always available)

Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Pixabay, Engin Akyurt, Pixabay, Pew Nguyen