Detecting an arrow in a mob?

Currently, I have 3 bows that shoot arrows, and when each arrow strikes the ground (detects if in ground by inGround=1b tag), it will summon lightning, tnt, anvils, or whatever doohickey thing I'm able to think about.

This works pretty well, but sometimes, when I want to "target" a mob, I would have to shoot it at it's feet. And that's pretty annoying when sometimes, it will hit the mob, causing nothing to happen.

Is there a tag that detects if an arrow is in a mob? I remember there was a tag for detecting if an arrow is in a player (maybe player:1b?), but is there one for mobs?

If there isn't, is there a way to detect if an arrow is near a mob? That would be the closest thing I need for my project.

Thanks!

3

2 Answers

When the arrow is shot, summon an identical arrow with a marker riding it. Copy over the relevant nbt data, like motion. Now, you have replaced the original arrow with an arrow that has a marker riding it.

When the arrow hits the mob, the marker will be left over. You can use a predicate to check if your marker is no longer riding. Then, you can execute your commands with 100% certainty that it hit a target, and where.


A possible solution might look like this:

Repeating every tick:

#Replace arrow
execute as @e[type=arrow,tag=!new_arrow] at @s run summon arrow ~ ~ ~ {Tags:["new_arrow"],Passengers:[{id:"minecraft:marker",Tags:["arrow_rider"]}]}
execute as @e[type=arrow,tag=!new_arrow] at @s run data modify entity @e[type=arrow,tag=new_arrow,limit=1] Motion set from entity @s
kill @e[type=arrow,tag=!new_arrow]
#Event
execute as @e[type=marker,predicate=!<namespace>:is_riding_arrow] run summon lightning_bolt ~ ~ ~
execute as @e[type=marker,predicate=!<namespace>:is_riding_arrow] run playsound entity.wolf.hurt master @p
kill @e[type=marker,predicate=!<namespace>:is_riding_arrow]

Predicate file named is_riding_arrow.json located in <datapackname>/data/<namespace>/predicates/:

{ "condition": "minecraft:entity_properties", "entity": "this", "predicate": { "nbt": "{Tags:[\"arrow_rider\"]}", "vehicle": { "type": "minecraft:arrow" } }
}
2

We are gonna:

  • Detect an entity who was hurt and has tag
  • Remove all tags
  • Tag all entities who are nearby an arrow

Which are translated to:

execute as @e[tag=possible,nbt={HurtTime:10s}] run <your command>
tag @e remove possible
execute as @e[type=minecraft:arrow,nbt={inGround:0b}] at @s run tag @e[distance=..4] add possible

Note here, at the last command, we have @e[distance=..4]. The reason we use 4 (a big number) is that arrows travel too fast. If you lower that number, arrows shot at max speed may not trigger this.

The problem with having such a big number, is that any mob hurt while an arrow is flying by will activate it. This requires timing, but it is easy to do on purpose.

You Might Also Like