Minecraft – (Java Minecraft 1.14) How to use /data with an nbt path

minecraft-commandsminecraft-java-edition

This site and this site both speak of an nbt path which allows me to put a certain value (say, the durability of the currently held tool in a player's hand) into a scoreboard. The wiki describes a path like this:

path (Optional)
  Specifies the path to the targeted NBT.
  Data paths look like this: foo.bar[0]."A [crazy name]".baz.
      foo.bar means foo's child called bar.
      foo[0] means element 0 of foo.
     "quoted strings" may be used if a name of a key needs to be escaped.

However, I don't understand what they mean by this. I know foo and bar are just dummy programming terms and that [] are for indices (say Pos[0] or Motion[1]), but I don't understand what the child and string notation is/how it is used.

This post has /data get entity @e[type=minecraft:villager,limit=1,sort=nearest] Brain to get a villager's home location, and I have used /data get entity @s Pos[0] before, but that is getting nbt about an entity specifically, not the contents of their inventory.

If someone could explain what the wiki excerpt means (preferably with examples) and also an example of getting nbt from an inventory item (again, say, the durability of the currently held tool in a player's hand) that would be very helpful.

Best Answer

"Child" tags are just tags inside other tags. Like "memories" is a child tag of "Brain".

The string notation is a bit misleading, you can't actually use it for strings in NBT. It's used if an NBT tag's name contains special characters, like a period, a square bracket, etc. No normal Vanilla NBT tag contains such special characters, but it is possible in a few places to use custom NBT tags.

If you want to get the damage on the item in your hand, use /data get entity @s SelectedItem.tag.Damage 1. The Player.dat wiki page shows player tags, and if you use SelectedItem (which returns an item) you need to look at the Item Structure tags, and see that the tag: tag has information about the item itself. Going to the general tags shows you the Damage tag. Therefore, you chain these together to get SelectedItem.tag.Damage.