mojira.dev
MC-273155

No way to use the output of /locate in any other function or command

I'm not sure if this is intended behavior or if there's some technical aspect I'm missing, but as far as I can tell there's no way to use the output of /locate besides running it as a player and clicking on the output in chat. Running it as a function or via a command block doesn't produce a usable chat output even if the function technically executes without error.

EDIT: I have found one limited way to use it:

  1. function
    scoreboard objectives add d2 dummy
    execute store result score @a d2 run locate biome jungle
    tellraw @a ["Distance: ",{"score":{"name":"@a","objective":"d2"}}]

This outputs the distance to a set locate target but there's no way to get the coordinates.

Comments 3

This is a feature request and not a bug.

That said, I too once found myself wanting to teleport to the nearest structure of a given type using commands. I made an algorithm which was kind of like ray marching to update the execution position to the structure's x and z then teleported the player to that location, just below the bedrock, with levitation. A tick (or several later), I detected the structure's y position and teleported the player to the appropriate location. It usually worked quite well, but when MC-138887 occurred during the ray marching I just had to print a message saying "sorry, we can't teleport you from your current location."

The addition of macros in recent versions means getting the x/z with a single locate probably is possible now, if you run the command from a command block and parse the output line. I'm actually working on this right now, just for fun, and will let you know if I can get it working successfully.

I considered it might be viewed as a feature request, but I thought it could be potentially a bug because the execute stored output stores the distance and not the coordinates, which the player sees when the player personally runs it- which seems like an oversight to me if the intended data wasn't the thing being stored.
If you can get parsing working, what I'd really like to do is put the X and Z values into the lodestone position item component of a compass- no need for teleporting. My goal is to create enchantable compasses to point toward biomes or structures.

Command result is always an integer. For some commands this is fine, but for /locate it would be much handier to work with the actual coordinates. I think it's unlikely that Mojang will rewrite the command system just for the benefit of just a few commands, so most likely we'll have to stick with this workaround for a while.
Anyway, I asked about this on the Minecraft Commands Discord, which you should definitely join if you're making a datapack. I'm making a simple proof-of-concept datapack that will put the coordinates into a spreadplayers command to teleport the player to the structure; once I have that working, I'll adapt it to put them into a lodestone compass instead. Feel free to comment on that forum post so we can do further communication through Discord instead of on here.

josh_crafts

(Unassigned)

Unconfirmed

(Unassigned)

1.21 Pre-Release 4

Retrieved