2

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.

  • I don't think it's possible how you imagine it, but I might be wrong. What I would try would be a custom ID system and then letting the player click "next" or "previous", increase or decrease a score for them and then teleport to a player with that ID when they click "OK". Or you could prepare one command for the indices 1 to 100 or so and when there is a player with that ID online, print their clickable event to chat. You would write a different ID into each of those commands. Or you do nothing of that and just temporarily put a player into Spectator mode, summon a marker amour stand and … – Fabian Röling Dec 18 '19 at 2:39
  • … teleport them back to it whenever they move away from it (within 10 blocks or so), and then, when they have teleported to another player using the Spectator menu, put them back into Survival mode. – Fabian Röling Dec 18 '19 at 2:40
  • To sum up your question: You want to generate a list of chat lines with click events where your commands can differentiate between the click events, right? – Fabian Röling Dec 18 '19 at 2:41
  • @FabianRöling, thanks for your reply. Because I expect fewer than 10 players at a time on this server, your first suggestions could work. Yes, I would like to generator a line of commands with a way to differentiate the click events from one another. Another idea I had would be to somehow nest the click events. For example, the last chat line would run all the previous click events while the first would only run the frist click event. – Jack Dec 18 '19 at 20:35
1

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.

|improve this answer|||||
  • Thanks, I definitely prefer the 2nd method. My only question is, how do I get the ID of the clicked player to the mcfunction for testing? – Jack Dec 19 '19 at 2:56
  • You don't, you make 10 distinct click events with a different number in them. – Fabian Röling Dec 19 '19 at 14:46
  • Ahh, ok that makes sense now. Just had to think it through farther – Jack Dec 19 '19 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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