How to teleport to the nearest player

Started by 0V3R10RD on

Topic category: Help with Minecraft modding (Java Edition)

Joined Sep 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
How to teleport to the nearest player

Hi there,

I'm looking for a way so that when an item is used, it will teleport you to the nearest player. (even better if it's across dimensions)

Or alternatively, upon when the item is used, a GUI can appear where the player can enter into an input field the username of the player and have it teleport them to it.

I have some ideas on how this could be done, but haven't seen how. Anyone got any ideas?

Joined Oct 2021
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
First one is doable but I'm…
Thu, 07/20/2023 - 18:25

First one is doable but I'm afraid you need custom code. 

To find nearest player in dimension where they are , you'd need to loop through all players in level and calculate for each distance and get the one with lowest. 

For between dimensions you can get player list from server and loop through this. 

Distance formula for 3 coordinates that you could use is `distance = (x2 - x1)² + (y2 - y1)² + (z2 - z1)²`.

For real distance this value should be squared, but since you need to find the lowest value, we can skip this step to fasten up calculations a bit cause you don't need exact value (square rooting is expensive operation, but it might affect more older pc's, not modern, but I prefer avoiding it anyway when it's not necessary, still less operations). 

I think it may be important to add small check if entity (we are checking distance to) is the one we check distance from (to not teleport Player Entity to itself). 

I think I can write for you the code for a code snippet if you want. 

Ps. This is be server sided calculation. 

 

Second one looks easier. 

Joined Sep 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
@Azzier Hey there! Yeah, let…
Thu, 07/20/2023 - 19:17

@Azzier Hey there! Yeah, let's go with the second option since overall that'll be fine as well. If you have a code snippit you want to send, go for it. Otherwise I'll figure that out on my own.

I was always hopeful MCreator would have an in-built way to read from an input field in the GUI and then let's say put that into a command. (Example: /tp @s UsernameVar) or something like that.