Reproduction steps
To reproduce, use the seed -5957804310435822198 and use the locate command and teleport to the first ocean ruin at 32 ~ -160. When you teleport to -18 ~ -160, the locate command will locate a different ocean ruin even though the first structure you located is nearest to you.
Cause
See this comment
Related issues
is duplicated by
relates to
Attachments
Comments


Does it happens in other worlds? Have you tried other villages(maybe the one where you are its not a valid village for some reason)
Loaded up a new world to try to reproduce the glitch, and I realized my steps to reproduce were not reliable. I did reproduce this bug using 2 valid ocean ruin structures that could be detected by commands, and I did /locate in various places and confirmed that /locate does not always locate the nearest structure unlike in 1.13.2. I edited this report accordingly, redid my steps to reproduce, and tested them, so now they should be completely reliable.

I reported the same bug MCPE-45526 for Bedrock Edition (and that bug has been confirmed). I found examples of /locate failing to find the nearest structure (a stronghold and two monuments) even when I was right on top of it. This bug isn't just in Java Edition.

Here's my steps of reproduction:
1. Use seed -5541185355023925660 and go to exactly
/tp -5831 66 -4607. You might find yourself inside a tree.
2. /locate Desert_Pyramid, it's 241m away.
3. Go 1m north. You should pass through a chunk border doing this.
4. /locate Desert_Pyramid, it's 1060m away!

The locate
command doesn't locate the nearest structure because the locate
command works on Anvil regions and it stops searching when a structure is found. The Anvil file format saves worlds in region files containing 512×512 blocks. The region is used as a base for generating structures, for example, there can be no more than one village per region, one temple, one ocean monument, one ruined nether portal, one desert well, one witch hut, etc. Other structures have different rules: they could be more common (shipwrecks and ocean ruins can occur multiple times per chunk) or rarer (woodland mansions).
The effect of regions can be seen when steps to reproduce posted by @Paint are analysed. The coordinates given are -5831 66 -4607. Mod 512, x and z are: 313, 1. When the player moves 1m north, the player is crossing a chunk boundary that is also on a region boundary. When crossing a region boundary in this way, the locate
command would search a different region for the requested structure, but it searches only one region.
The effect of regions can be seen clearly when looking for ruined Nether portals in the 20w16a snapshot. These occur once per region in the Overworld, in most or all biomes. If one occurs near the edge of a region, crossing a region can cause a nearby ruined portal to be ignored while a farther one is found instead.
If greater accuracy is required, the locate
command should be modified so it searches additional neighbouring regions as well as the current region. It should search an additional region on both sides. The command already searches multiple regions, such as when suitable locations for that structure are not nearby. It should add one extra search region on both sides even if the structure is found. This won't guarantee the closest structure will always be found (eg: the nearest edge of villages), but it will prevent obviously wrong results such as the one that Paint demonstrated.

@bdm68 – that's a good explanation, but it doesn't explain how you can be practically right on top of a structure that locate
still doesn't find. Multiple examples are cited in MCPE-45526 – granted, that's Bedrock not Java, but the issues do seem related.

So A. Matulich... perhaps to add to bdm68's explanation, I do believe it answers why you can be "practically on top of a structure" and locate tells you another structure is closer (assuming bdm68's explanation of this bug is accurate).
Basically... structures generate from a "starting point", a specific coordinate that is almost always on 1 of the outermost edges of the structure, even some blocks away from the actual object because it generates blocks/air/water surrounding the structure - thus, the "starting point" is basically never in the centre of the structure itself.
Thus, if you are in the centre of the structure , yet the generation "starting point" is outside of your current chunk.... your chunk (the centre of the structure) may easily be in a different Anvil region.... therefore being in a different anvil region, your /locate command will draw you away from the nearby structure because it finds another of the same structure in it's own anvil region instead of the one a few blocks away.
I really hope that helps 🙂 I'm trying to explain something that I think a picture would do better to explain 🙂

@Kris – in the examples I cited in MCPE-45526 it doesn't matter where you are around the structure: on top, nearby, just off any outermost boundary in any direction.... locate
doesn't find it. That may be a different problem than described in this ticket (this ticket is Java, that one is Bedrock). But I am skeptical that the "anvil region" explanation applies there.

Thanks for the feedback @Matulich. Unfortunately I don't have bedrock so can't test that :/ But using this ticket and the seed in the description, I am able to reproduce the issue described by Anvil region ...
I.E.
Using the description seed (-5957804310435822198).
Newly created map in 20w19a.
Spawn in chunk coordinate X:0 Z:-5.
Moving Southbound and running the "/locate Monument" in each chunk - I get "The nearest Monument is at [304, ~, -336] ([NUMBER] blocks away)" stop at chunk X:0 Z:-1 (assuming this is a region boundary)
Exactly SAME happens moving EAST - where I tested chunks X:-3 .... to X:10 ..... (but stayed in Z:-1 and lower... -2, -3, etc) ....... Nearest Monument [304, ~, -336].
However.... the moment you cross the Z:-1 chunk, moving into Z:0 and higher (1, 2, etc)..... (Assumed I'm moving into the next region)
The nearest Monument is at [128, ~,48]. This promotes the Anvil Region description, in that if you're in the wrong region, then you get directed to the closest structure in the respective region, instead of the adjacent region, which could provided a much closer structure.
Although only "related" and not the same problem exactly .... this ticket MC-178842 shows the way crossing a chunk boundary can show a completely different "closest structure" ... from 2 blocks away ... to 668 blocks away...(see screenshots 4 and 5 of that ticket).
I of course cannot speak for Bedrock, but am only able to safely reproduce this issue in Java Edition, I believe the mechanics might differ between minecraft versions – so it's reasonable to say the issues could be completely different, or simply present differently altogether. I can only speculate 🙂
Having said all that, the "Anvil Region" description provided by @bdm68 fits very well – but I don't know the inner workings of the code myself, so I simply agree with the description of the problem because it fits the issue and makes the most sense to me. If it doesn't fit in the case of Bedrock, then an alternate reason for the problem should be sought 😃 I think I might arrange to get bedrock and do some testing to help there 🙂

Still present in 20w21a

Still present in 20w22a...
Very interesting test with slightly more info – I just discovered what I think is the corner between 4 "anvil regions"...
Same seed: -5957804310435822198
4 Adjacent chunks..... using "/locate monument"
Chunk coords: 0, 0
The nearest monument is at [128, ~, 48] (136 blocks away)
Notice location..... X and Z positive - monument is South East
Chunk coords: -1, 0
The nearest monument is at [-256, ~, 192] (319 blocks away)
Notice location..... X negative... Z positive
monument is South West
Chunk coords: -1, -1
The nearest monument is at [-416, ~, -288] (504 blocks away)
Notice location..... X and Z negative
monument is North West
Chunk coords: 0, -1
The nearest monument is at [304, ~, -336] (452 blocks away)
Notice location..... X positive... Z negative
monument is North East
Very interesting.
Anyway, still present 👍

Still present in 1.16 Pre-release 8.

@Kris – there may be hope. I confirmed it got fixed in 1.16 in MCPE-45526 in the Bedrock version of this ticket. So a fix is possible – although that may simply mean that Bedrock uses a different mechanism than anvil regions for locating things.

Thanks Matulich!
Also confirming this issue is still present in 1.16.1 and 20w29a 👍

Still present in 1.16.2 👍

Present as of 21w42a

This seems to have gotten worse in Java 1.18 (released).

Affects 22w03a.
Seed: 690738353614676304
Coordinates: 4096 ~ 0
Observe the differing results of /locate village
when the command is run from positive/negative z. (Interestingly, crossing x=4096 doesn't seem to affect the results.)

Affects 1.18.2-pre1. The bug no longer occurs at the seed/coords from my previous comment so use this instead:
Seed: 690738353614676304
/tp @s 51200 100 -8192
/locate monument
Observe the differing results when the command is run on either side of x=51200. (This time, crossing z=-8192 does affect things slightly when x>51200.)

Affects 1.18.2-pre3.

Affects 1.18.2.

Affects 22w11a.

Affects 22w12a (edit: and 22w13a, 22w14a and 22w16b).

@unknown I think it would be better to comment affected versions in separate comments, as that is easier to see.

@unknown well I was trying to avoid shoving my repro seed/coords into the "View X older comments" button but not that you've gone and done it for me I guess I'll resume making separate comments 🙃. (Honestly I can usually reproduce it fairly quickly by just teleporting myself along an axis so it was more for others' sake.)

Affects 22w17a.

Affects 22w18a.

Affects 22w19a. /locate biome
is very slightly affected, but not nearly as significantly as /locate structure
. /locate poi
is unaffected.

Affects 1.19-pre1.

I recently noticed this bug myself in Minecraft 1.19. There were two woodland mansions close to spawn on my world, which I found both via the /locate command. With the second mansion I located with the command, I noticed that according to my coordinates, the first one I located was actually slightly less far away from my position than the second one I located, which was really confusing.

Can confirm in 1.20.5. Also duplicated by MC-271211.