How can I execute when a certain player hits something in minecraft
I am aware that you can execute a command when a mob is hit, and utilize that command in my map, but I would need to know the player or team that it was hit by. I am making a tennis map where the chicken is the ball and it was picked up on reddit and someone asked me to implement it on their server.
The commands in the map know what block the chicken landed on, and whether that block is in or out, but doesn't adjust the score automatically as it doesn't know who hit the ball last, and what side the ball landed on. that would require the command blocks to know who hit the chicken last, or what team they were on. and that's where i'm stumped
Solution 1:
The only way to do this reliably seems to be the advancement criterion player_hurt_entity
(archive). An example of how to execute a function as a player who hits a chicken is this:
{
"display":{
"icon":{
"item":"air"
},
"hidden":true,
"show_toast":false,
"announce_to_chat":false,
"title":"",
"description":""
},
"criteria":{
"":{
"trigger":"player_hurt_entity",
"conditions":{
"entity":{
"type":"chicken"
}
}
}
},
"rewards":{
"function":"test:test"
}
}
That function can then first revoke that advancement again and then do something based on the team that the player is in. @s
in the function will be the punching player.
Example path: saves/world_name/datapacks/test/data/test/advancements/tab/test.json
I don't know in which order the function executes for who if multiple players hit a chicken in the same tick, it probably depends on the order in which they joined, but you probably don't mind it either way in that case anyway.