5

I'm playing 1.14 and wanted to test some villager mechanics in a creative world. To help debugging I thought it'd be a good idea to constantly print the home/workplace information of the closest villager, which is found in the Brain tag of the NBT data. So I wrote the following command:

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

And it works great:

command output

So now I thought I could just put this in a command block and trigger it every second to have easy debugging. So I put this in a command block:

/execute as orlp at orlp run data get entity @e[type=minecraft:villager,limit=1,sort=nearest] Brain

However I don't get any output on triggering the command block. Even /gamerule commandBlockOutput true doesn't make it output anything.

How do I output the result of a data command ran in a command block to chat (or otherwise)?

1
  • I'm afraid the only way to do this through the command block is by looking into command block, which isn't any better than executing the command by yourself.
    – Quijibo
    May 5, 2019 at 15:59

1 Answer 1

5

Yes! 1.14 added NBT as a JSON component. So you can now print NBT like this:

/tellraw @s {"nbt":"Brain","entity":"@e[type=villager]"}

This even works for multiple entities (unlike /data get), which puts "," between the NBT outputs (which usually leads to the second and further tags appearing in a new line, because Minecraft only wraps lines on spaces, which aren't normally in NBT).

You can also output all NBT of an entity like this:

/tellraw @s {"nbt":"","entity":"@e[type=villager]"}

It also works for blocks:

/tellraw @s {"nbt":"","block":"12 -34 56"}

And you can combine "block" and "entity", then "entity" just gets ignored.

And as a special treat, you can even interpret that NBT as a JSON component, for example if you give a villager a formatted name like this:

/summon minecraft:villager ~ ~ ~ {CustomName:"{\"text\":\"Horst\",\"bold\":true}"}

…, then you can either output that JSON as text:

/tellraw @s {"nbt":"CustomName","entity":"@e[type=villager]"}

Output: {"bold":true,"text":"Horst"}

… or you can output it how it appears on top of its head:

/tellraw @s {"nbt":"CustomName","entity":"@e[type=villager]","interpret":true}

Output: Horst

And finally you can get really crazy and recursive:

/give @s written_book{author:"",title:"",pages:["{\"nbt\":\"CustomName\",\"entity\":\"@e[type=villager]\",\"interpret\":true}"]}
/tellraw @s {"nbt":"Inventory[0].tag.pages[0]","entity":"@s"}

Output: {"nbt":"CustomName","entity":"@e[type=villager]","interpret":true}

And interpreted:

/tellraw @s {"nbt":"Inventory[0].tag.pages[0]","entity":"@s","interpret":true}

Output: Horst

Note that this changes when you open the book, because then the JSON gets interpreted and the book page changes from {"nbt":"CustomName","entity":"@e[type=villager]","interpret":true} to Horst.

4
  • Can I use limit=1,sort=nearest within the tellraw if I prefix the tellraw with /execute as orlp at orlp run?
    – orlp
    May 5, 2019 at 16:30
  • Sure. If "orlp" is you, you don't even need to do that. May 5, 2019 at 16:49
  • orlp is indeed me. If I didn't do that how would the command block know what to center the nearest sort on then?
    – orlp
    May 5, 2019 at 16:53
  • @orlp The command executor. May 5, 2019 at 19:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .