Minecraft Blogs / Tutorial

Using nbt paths in minecraft. 1.14 (/data modify|get)

  • 2,894 views, 1 today
  • 6
  • 4
  • 3
Breakmit's Avatar Breakmit
Level 56 : Grandmaster Goblin
14
Tutorial:How to use paths properly


Terminology & notes


-this tutorial is for the <path> part of the syntax of the "new" /data command


-this is a pretty advanced tutorial about json and nbt tags and you might want to do some research about that first

-key/value: if you don't know at all about nbt tags in minecraft, try typing /data get entity @s, you should get something like {word1:word, word3:number, word4:[...], ect}
these are the nbt tags of the player, (or it' data), it's composed of keys and values, basically, keys are before ":" and values are after it.

-types of data :

-a key:value set : i will call that a set or a tag during this tutorial

-the different values :
  - a number : pretty easy, don't worry about that letter we don't really care for this tutorial
  - a string : just text, in quotes ' or double quotes " in json but NOT in minecraft.
  -a boolean :it's value can be true or false
  -a null : it has no value, not very used in minecraft, written null

  - an object :a list of tags, in { }, filled with tags separated by ","
  - an array : a list of values , in [ ], filled with values separated by", "

-keys are always strings

-the root object is the object in which everything is stored, the { } we see around the data

-{ } and [ ] will be used instead of {} and [] for clarity but remember to remove the spacing in actual code


the basics of the paths


in minecraft, path refers to a modified version of JSONpath, a syntax used to search values in json objects.

Let's create an example minecraft object for this tutorial:
{mass:15,industrial:true,sauce:{name:ketchip,ingredients:[{name:tomato,mass:1},{name:sugar,mass:14}]}}
or
{
  mass:15,
  industrial:true,
  sauce:{
    name:ketchup,
    ingredients:[
      {name:tomato,mass:1},
      {name:sugar,mass:14}
    ]
  }
}        
Now lets imagine we want to know the mass of this sauce, we will use mass, the system will send back 15, and that's it, to access a value in the first object, we just use it's key.

But what if we want the name of the sauce, if <e use name, the computer wont find it since it's in the sauce tag and send an error. If we use sauce, we will not get an error but {name:ketchip,ingredients:[{name:tomato,mass:1},{name:sugar,mass:14}]} So, how do we access a value in an object in the root object?, we use the point!, the correct path is: sauce.name, we say to the computer: "we want the value of sauce but it in that value, we want a value named name", this is the basic path, this structure can be repeated as many times as we want in order to access values: foo.bar.name.prefix is a valid path.


finding values in arrays or lists:


Now we know how to access objects but what if our value is part of a list, for example, the tomato ingredient, we can't use sauce.ingredients.tomato since tomato is not a key of the object sauce.ingredients since ingredients is not an object but an array.

To access array, we use indexation (giving a number as the key of a value in a list), for example, the first value of sauce.ingredients is {name:tomato,mass:1}, we access values in list using nameOfList[​index] so our tomato ingredient is at sauce.ingredients[​0], we always start indexation at index 0.


negative indexes


for this, another example is used:values:[
  sand
  gravel
  stone
  grass
  ...
  sponge
]
Here we don't know how many value this array contains but we just added sponge and we would love to access it for some reason, we use negative idexes  to count from the end of a list, this time, we count from -1 and not -0 since -0 is the same as 0:


value
sandgravelstonegrasssponge
positive index
01234
negative index-5-4-3-2-1



recursive research : indexes are always integers, except when they aren't


If we try to use a non integer value as an index in minecraft, the game will tell us it wants a "valid integer", but minecraft can transform some non integer values in integers for us, specifically an object, the integer will be the index of the value of the list that contains the object, if multiple value match the condition, minecraft will send an error. Explanation:


taking back our first example, we want to get the informations of the ingredient sugar, we could use sauce.ingredients[​1] or sauce.ingredients[-1] but let's imagine we don't know the index of that object, we use recursive research, we find the value that contains what we want: the syntax is sauce.ingredients[{name:sugar}], and we get the value of sauce.ingredients that contains the tag name:sugar.


ingredients[ ] and ingredients


We already know that sauce.ingredients will send back [.......] but we can also use sauce.ingredients[ ], and that will send {...},{..}... back this is rarely used but still useful to know and will cause an error with data get since it can only output 1 value, it's like recursive research but with no condition.


conditional path



For this last part, we want to get the last ingredient of our ketchip, but only if it's an industrial sauce, we can't use sauce.ingredients[-1] because that would give the output whatever the value of industrial is. To achieve our goal we need to remember that a blank path is the root object and to know conditional paths.
The syntax is name{condition} so our path is (root{industrial:true}.sauce.ingredients[-1].name



--------------------------------------------------------------------------------------------------------------------------------


sum up table:


name
syntax
role
notes
normal
foo.bar
tag bar in object foo
conditional
foo{bar:value}
foo if foo.bar = value
index
foo[​index]
value index of foo
index: + = from start   - = from end
recursive
foo[{bar:value}]
values of foo containing bar:value
can cause an error if multiple values check




-----------------------------------------------------------------------------------------------------------------------------------

you now know everything about nbt paths in minecraft, feel free to use this in other places since this is a very poorly documented subject but remember to give credit with a link to this
Creditcomplement info can be found at https://minecraft.gamepedia.com/Commands/data#NBT_path
Tags

Create an account or sign in to comment.

1
01/28/2021 6:43 am
Level 1 : New Miner
GalaxyLinus
GalaxyLinus's Avatar
So for example how do I get the item from my inventory slot 0? /data get entity @s Inventory ...?
1
01/28/2021 6:44 am
Level 1 : New Miner
GalaxyLinus
GalaxyLinus's Avatar
I'm a bit disorientated with the foos and bars and this stuff
1
03/16/2020 5:49 am
Level 62 : High Grandmaster Dragon
RevonZev
RevonZev's Avatar
THANK YOU. You've helped me. I've been looking for this, for a long time!
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome