Minecraft – How to use an index with /execute with Vanilla Minecraft Commands

minecraft-commandsminecraft-java-edition

I am trying to create a way for players on my vanilla server to teleport to each other without giving them op. My current idea is to have a list of clickable player names that appear in the chat with a unique click event. I currently have the following command in my datapack:

execute as @a[sort=nearest] run tellraw @p {
        "text":"","color":"gold",
        "clickEvent":{"action":"run_command","value":"/trigger objective set 1"},
        "extra":[{"selector":"@s"}]
    }

This works and outputs all the players in the chat. My issue is that I cannot differentiate between the players. If I could replace /trigger objective set 1 with /trigger objective set i this would work. From my experience there is no way to do this.

The one solution that I came up with would be to embed an execute command in the click event to run \trigger object add 1 the same number of times the execute ran before. It might look something like this:

/execute as @a[sort=nearest] if entity distance <= @s distance run /trigger object add 1

Basically it would ideally loop until it hit the player that was clicked. This would allow me to reverse engineer the score to teleport the correct player to the correct target.

Is there a way that I could somehow do this, or would this be impossible because @s would technically be the player clicking and not the player clicked.

Best Answer

I think I can see two ways to do this. One is more simple but does not give you exactly what you want, whereas the other is more complex but I think it does do what you want.

The simple way is to give every player a unique id on login, and have that id displayed next to each players' name in tablist (using scoreboard setdisplay sidebar). Then, you teleport to a player using trigger objective set [ID of person you want to TP to] and then the command logic should be pretty straightforward from there.

The second method is to assign these IDs on the fly. When a player runs the command to open the menu, you assign the nearest 10 players a unique TPA-ID-score between 1-10. Then, you have a mcfunction with 10 of your trigger commands hardcorded 1-10, where you do trigger objective set 1, trigger objective set 2 and so on. (you would also change the selector to make sure the numbers line up). If you want an additional "page" of results, give all players with TPA-ID-score 1-10 some sort of tpa-excluded tag, and assign the next 10 players a unique TPA-ID-score 1-10. Then print out your 10 tellraw messages again.

This will allow you to scroll through every player on the server, and when someone chooses to click on someone to teleport to, reset by removing everyone's tpa-exclude tag and set all TPA-ID-scores back to zero.

The downside of this second method is that multiple players would not be able to use it at the same time (you'd have to prevent players from opening the tp menu while another player is using it), whereas with the first method is fully functional in multiplayer.

Related Topic