Is it possible to display output of /data from a command block?
Solution 1:
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
.