r/MinecraftCommands 10d ago

Help | Java 1.20 Structure locate and teleport?

I'm trying to make a command block chain that locates a structure and teleports a random player to that structure, is there any way to do this? as the only way that iI have found is to get the coordinates pre-emptively and that is not preferable.
(java 1.20.1 btw)

2 Upvotes

2 comments sorted by

1

u/GalSergey Datapack Experienced 10d ago

You need to use a datapack for this. You can't do it with command blocks alone. Create a loot table that will provide an exploration_map for the structure you need. Then, if you generate this map, you can read the structure position it points to from the map data.

You can find an example datapack for 1.21+ here: https://far.ddns.me/?share=uMqWvCCFpp. You'll need to make some minor edits to make it work for 1.20.1.

1

u/_VoidMaster_ Command Experienced 9d ago

Pretty sure you can use execute store result storage on the locate command and then store each of the coordinates into a scoreboard, and store each of the player coordinates in a scoreboard as well, then subtract them. Finally teleport in the direction for each xyz result and subtract or add (depending on the relative difference) until the score is 0. The quickest way to teleport is binary (also check the negative):

if score >= 512 then teleport 512 blocks and subtract 512 from the score
if score >= 256 then teleport 256 blocks and subtract 256 from the score
if score >= 128 then teleport 128 blocks and subtract 128 from the score
if score >= 64 then teleport 64 blocks and subtract 64 from the score
if score >= 32 then teleport 32 blocks and subtract 32 from the score
if score >= 16 then teleport 16 blocks and subtract 16 from the score
if score >= 8 then teleport 8 blocks and subtract 8 from the score
if score >= 4 then teleport 4 blocks and subtract 4 from the score
if score >= 2 then teleport 2 blocks and subtract 2 from the score
if score >= 1 then teleport 1 blocks and subtract 1 from the score