Minecraft 1.12 reliably detect player-player interaction

minecraft-commandsminecraft-java-edition

I am making this command block mini-game which requires two players to "interact" with each other. With interacting, I mean: two players meet each other, initiate the interaction, then command blocks compare a certain score of both players. If the score of player 1 is higher than player 2, then a command is executed at player 2 and vice versa. If the scores are equal, then a command is executed at both the players.

I know how to compare scores and how to use these to initiate commands, but I get stuck on the initiation of the interaction. I need a reliable way to initiate interaction between two players.

The first thing I tried was letting player 1 slap player 2 and then detect this by keeping a score for damage dealt and damaged taken. Then I can find an interaction by checking these scores. This is not reliable though, because:

  • If two interactions take place at the same time, the scoreboard doesn't show which player got slapped by which player and it might compare the wrong players. I thought to fix this by checking for the closest player that got slapped, but if another player stands closer than the player that I slapped, then the game picks the wrong pair.
  • The scoreboard doesn't differentiate between damage types. If somebody happens to fall next to me at the moment I try to slap somebody else, the game will choose the wrong player to interact with.

I thought I could just check for two players that are close together, but I cannot check for a distance between players lower than 1, which makes this method counter-intuitive, since you might interact with somebody by accident.

Perhaps I should check if a player holds a certain item and then perform the check that I described in the previous paragraph, but it's very easy to just leave the item in your hand and then the counter-intuitivity kicks in again.

I want to avoid long distance interactions, like shooting somebody with a bow or something.
Basically I want interaction similar to trading with a villager. You click on a player and then the interaction with that specific player takes place.
Note that the method must be easy expandable and should theoretically work for an infinite amount of interactions at the same time. This mini-game is played with potentially a lot of players at once, who can all have interactions with each other.

How can I make a reliable and intuitive player-player interaction with command blocks?

EDIT:

A player might not want to have interaction and will therefore try to flee. The commands should take that into consideration. Thanks @Fabian to point this out.

Best Answer

here's my current thought on how to do something like this:

first limitations: the people can't really be in a huge group, will need some distance between them

also snowballs.

here's my thought: every player has their own unique number, they all will need a snowball to "interact" with another player, or throw the snowball at the other player.

when a player throws the snowball (the use stat of snowballs goes up one), you take the closes snowball that doesn't have a number, apply it the players number, and attach an witherskull (/entitydata on the passengers tag) and also give it the number. when the snowball dies (hits someone) use the witherskull to chose the nearest player (@p[c=1,r=1] or something).

now you have the player that threw the snowball (player a), and the player with the witherskull near them (player b). after doing the test/interaction kill the witherskull.

hopefully you can follow that. now I'll see if I can get something to work. if I do I'll update my answer.