0

I'm trying to create a mob hit detection system. I have researched online beforehand to this question, however I haven't come to any conclusion on my success yet.

I'm simply after, when a mob is hit, it summons 1 orb of experience so the players can use their mending, as I have mob drops off so they do not drop xp upon death.

If you have any way to do this, I'd love to hear it, any help is gladly appreciated. I'm on Minecraft ForgeOptiFine 1.14.4, hopefully that helps! I've got a feeling it's about the HurtTime NBT, I just can't get my head around it.

1
  • You can also use the player_hurt_entity advancement trigger, but that executes as and at the player, not the mob, so it's useful (and better for performance, reliability, etc.) if you want to directly give the player XP or anything like that, but not if you want to do stuff at the mob's position or from its execution context. There MegaCrafter10's answer is indeed the best (except that functions give you better performance). Commented Apr 13, 2020 at 13:28

1 Answer 1

1

HurtTime increases once the mob is hit. So detecting a hit is as simple as running a command when its the value is greater than 0.

Place down a repeating command block and attach 2 chain command blocks it to. Set them all to always active and paste these in (in the exact same order):

/execute as @e[nbt=!{HurtTime:0s},tag=!ignore] at @s if data entity @s HurtTime run summon minecraft:experience_orb
/execute as @e[nbt=!{HurtTime:0s}] if data entity @s HurtTime run tag @s add ignore
/tag @e[tag=ignore,nbt={HurtTime:0s}] remove ignore

Also, in case you are wondering what the 2 other commands are, it takes a few seconds for the HurtTime value to go back to 0 so those commands make sure that you only get 1 XP orb during that time.

Command block layout: enter image description here

3
  • Thank you very much! This works perfectly. I presume I can change the "cow" section to "!player" to identify any mob, much appreciated! Commented Apr 13, 2020 at 13:57
  • Okay, heads up for anyone that may see this, don't change it to "!player" because you will destroy the server. I had to turn off command blocks and destroy the command block as it was constantly summoning orbs and overloading everything. So be careful with this command. Commented Apr 13, 2020 at 14:07
  • @BigSmoke3326 Entities such as item frames or arrows don't have the HurtTime NBT tag so that's why the command thinks it should summon XP orbs. I edited my answer and fixed it by adding a check for the HurtTime NBT tag. Thanks for reporting this. Commented Apr 13, 2020 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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