NOTE: Java edition 1.15.1

As an example, my villagers keep running off somewhere / getting stuck on the side of mountains - and it's not always easy to track them down by their sound.

Rather than fussing around - is there a way to print out the XYZ of the nearest entity?

Something like "print coord of @e[type=villager,distance=0..40]" - where it prints either the closest match, or all entities found.


In general, the below command will display the coordinates of all entities of a certain type:

/execute as <relevant entities> run data get entity @s Pos

For your specific cases:

/execute as @e[type=villager,distance=0..40] run data get entity @s Pos

will display the coordinates for all villagers within 40 blocks. To display only the coordinates of the nearest villager:

/data get entity @e[type=villager,sort=nearest,limit=1] Pos

(the /execute is not necessary when you are only dealing with one entity)

| improve this answer | |
  • Thanks pppery. Perfect. – Litch Jan 8 at 9:17
  • Do you actually need to use execute at all if you can properly construct the selector? Or does the data command need only a single entity in order to run? – MBraedley Jul 31 at 14:20
  • The /data command fails if the selector it is given could in theory produce more than one entity, so if you want it to operate on more than one entity (such as the "print the positions of all villagers" example), you need to run it through /execute – pppery Jul 31 at 14:24

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.