Commands/data

The  command allows the user to get, merge, modify, and remove NBT data of a block entity or entity.


 * Syntax

There are four instructions for , and the targets/sources referenced by each instruction command may be either   or.




 * Syntax trees
 * {| class="collapsible collapsed collapse-button-none" data-description="simple_tree"

! style="text-align:left" | Simplified tree:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:
 * where substituted arguments are:


 * }
 * {| class="collapsible collapsed collapse-button-none" data-description="complete_tree"

! style="text-align:left" | Comprehensive tree:


 * }
 * {| class="collapsible collapsed collapse-button-none" data-description="squished_tree"

! style="text-align:left" | Squished tree:


 * }
 * {| class="collapsible collapsed collapse-button-none" data-description="tree_diagram"

! style="text-align:left" | Diagram:
 * }
 * {| class="collapsible collapsed collapse-button-none" data-description="one_line"

! style="text-align:left" | On one line: /data (get (block |entity ) [ ] [ ]|merge (block |entity ) |modify (block |entity ) (append|insert |merge|prepend|set) (from (block &#58;
 * —The  position of the target tile entity whose NBT is to be operated on
 * —Valid UUID, or target selector variable for an entity whose NBT is to be operated on
 * —Valid namespaced ID for a storage to be operated on


 * &#58;
 * —Valid path name defining the specific NBT to retrieve
 * —Scalar for the command's return value


 * &#58;
 * —Compound tag. The simplest valid imput is.


 * &#58;
 * —Valid path name defining the specific target NBT to modify
 * —Valid integer specifying an item's index within a list
 * &#58;
 * —The  position of the source block whose NBT is to be used
 * —Valid player name, UUID, or target selector variable of a source entity whose NBT is to be used
 * —Valid path name defining the specific source NBT to be used by
 * &#58;
 * —Numeric value to use in modifying the target NBT. Must match in data type


 * &#58;
 * —Valid path name to the NBT to remove


 * See 


 * Results


 * Command fails if the arguments are not specified correctly.
 * On success:
 * Read off the entire NBT data from the targeted block position or entity to the executor with syntax highlighting.
 * If applicable, returns 1 on success.
 * Read off the entire NBT data from the targeted block position or entity to the executor with syntax highlighting.
 * If applicable, returns 1 on success.


 * Send the subsection of the NBT data from the targeted block position or entity to the executor without syntax highlighting.
 * Return value depends on the type of the NBT element:
 * Retrieving a number returns that number.
 * Retrieving a string returns the length of the string.
 * Retrieving a list returns the number of elements in that list.
 * Retrieving a compound returns the number of tags that are direct children of that compound.
 * Return the value of the tag, scaled by .
 *  needs to specify a numeric tag—TAG_byte, TAG_short, TAG_int, TAG_long, TAG_float, or TAG_double.
 * Return the value of the tag, scaled by .
 *  needs to specify a numeric tag—TAG_byte, TAG_short, TAG_int, TAG_long, TAG_float, or TAG_double.


 * Merge the NBT data from the sourced block position or entity with the specified  data.
 * If applicable, returns 1 on success.
 * If applicable, returns 1 on success.


 * Append the source data onto the end of the pointed-to list.
 * must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
 * Insert the source data into the pointed-to list as element, then shift higher elements one position upwards.
 * must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
 * Merge the source data with the pointed-to object.
 * must specify a tag of the TAG_list type, and source data must be of the TAG_compound type.
 * Prepend the source data onto the beginning of the pointed-to list.
 * must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
 * Set the tag specified by  to the source data.
 * must specify a tag of the TAG_list type, and source data must be of the TAG_compound type.
 * Prepend the source data onto the beginning of the pointed-to list.
 * must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
 * Set the tag specified by  to the source data.
 * Set the tag specified by  to the source data.
 * Set the tag specified by  to the source data.


 * Removes NBT data at  from the targeted block position or entity. Player NBT data cannot be removed.
 * If applicable, returns 1 on success.
 * If applicable, returns 1 on success.

NBT path
An NBT data path is a descriptive label used to specify any particular element from an NBT data tree. A path has the general form, where   is the specified tag and   is one of the most superficial tags.

After each key, it is possible to give an NBT object which gets compared to the value of that tag. The tag only is targeted by the path if the comparison has a positive result.

When defining an element from a list, brackets are used after the key of the list. The content of the brackets can be one of 3 things:


 * Blank space—This returns every element in the list.
 * An index number—For a nonnegative index n, this returns the (n + 1)th element in the list. For a negative index n, this returns the nth element from the end of the list.
 * An NBT compound. This returns all objects in the list that match the specifications of that compound.

The valid character set for a path seems to be all non-whitespace characters, as long as any opening brackets and braces ( and  ) are closed and at the end of each term (that is, followed by nothing but a period or the end of the path—  or  ). That being said, the reasonable character set for defining a data tag would be:  (To put this as a,  ) "Quoted strings" may be used if a name of a key needs to be escaped.

Foo bar example:
These names have been arbitrarily picked, for demonstrative purposes.


 * —Specifies the top-level element named "foo"
 * —Specifies foo's child named "bar". Let's pretend this is a list.
 * —Specifies the first element of the list "bar"
 * —Specifies the child named "A [crazy name]!" under the that first element
 * —Specifies the child named "baz" under the that crazily named element
 * —Specifies all elements of the list "bar"
 * —Specifies the children of all elements of the list "bar"
 * —Specifies all elements of the list "bar" of which the "baz" tag is set to
 * —Specifies the root tag of the targeted entity or block
 * —Specifies the root tag of the targeted entity or block if the "foo" tag is set to
 * —Specifies the "foo" tag if its child "bar" has the value
 * —Specifies the "bar" tag if it matches the value

The tree structure
 * The root entity data
 * : The "foo" element
 * : The "bar" element
 * The first element of list "bar"
 * : The "A [crazy name]!" element
 * : The "baz" element; the target element of this example.
 * Another unrelated element in list "bar"

Book-in-a-chest example:
The player has written a book and placed it inside a chest at her feet. She's going to work up to the above command in stages.


 * {| class="collapsible collapsed collapse-button-none" data-description="bookInAChest_example"

! style="text-align:left" | Chat log * Alex jumps on top of a chest. * Alex runs the command: /data get block ~ ~ ~ 0 55 0 has the following block data: {{{TAG_number|x|0}},, , Items : [{{{TAG_number|Slot|0|b}}, , }, {{{TAG_number|Slot|9|b}}, , , tag : { pages : [ '{"text":"\'twas brillig and the slithy toves"}' , '{"text":"Did gyre and gimble in the wabe."}' , '{"text":"All mimsy were the borogoves,"}' , '{"text":"And the mome raths outgrabe."}' ], , }}], } ''* Alex only wants to look at the chest's inventory. She could search the Official Minecraft Wiki for what the name of that tag is, but because she understands how to read NBT format, she decides to figure it out from that last command's output.'' * Alex runs the command: /data get block ~ ~ ~ Items 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items:  [{{{TAG_number|Slot|0|b}},, }, {{{TAG_number|Slot|9|b}}, , , tag : { pages : [ '{"text":"\'twas brillig and the slithy toves"}' , '{"text":"Did gyre and gimble in the wabe."}' , '{"text":"All mimsy were the borogoves,"}' , '{"text":"And the mome raths outgrabe."}' ], , }}] , id: "chest"} ''* Alex wants to narrow this down to the second item in the chest. She starts counting from 0, so the second item would be element 1.'' * Alex runs the command: /data get block ~ ~ ~ Items[1] 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "clock", Count: 1b},  {{{TAG_number|Slot|9|b}},, , tag : { pages : [ '{"text":"\'twas brillig and the slithy toves"}' , '{"text":"Did gyre and gimble in the wabe."}' , '{"text":"All mimsy were the borogoves,"}' , '{"text":"And the mome raths outgrabe."}' ], , }} ], id: "chest"} * Alex wants just the "tag" tag. * Alex runs the command: /data get block ~ ~ ~ Items[1].tag 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "clock", Count: 1b}, {Slot: 9b, id: "written_book", Count: 1b, tag:  { pages : [ '{"text":"\'twas brillig and the slithy toves"}', '{"text":"Did gyre and gimble in the wabe."}' , '{"text":"All mimsy were the borogoves,"}' , '{"text":"And the mome raths outgrabe."}' ], , } }], id: "chest"} * Alex wants just the "pages" tag. * Alex runs the command: /data get block ~ ~ ~ Items[1].tag.pages 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "clock", Count: 1b}, {Slot: 9b, id: "written_book", Count: 1b, tag: {pages:  [ '{"text":"\'twas brillig and the slithy toves"}', '{"text":"Did gyre and gimble in the wabe."}' , '{"text":"All mimsy were the borogoves,"}' , '{"text":"And the mome raths outgrabe."}' ] , author: "LewisCarroll", title: "Jabberwocky"}}], id: "chest"} * Alex wants just the fourth element from this list. * Alex runs the command: /data get block ~ ~ ~ Items[1].tag.pages[3]  0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "clock", Count: 1b}, {Slot: 9b, id: "written_book", Count: 1b, tag: {pages: ['{"text":"\'twas brillig and the slithy toves"}', '{"text":"Did gyre and gimble in the wabe."}', '{"text":"All mimsy were the borogoves,"}',  '{"text":"And the mome raths outgrabe."}' ], author: "LewisCarroll", title: "Jabberwocky"}}], id: "chest"} ''* Alex has what she needs now... She uses the paths she's gathered to edit the book from outside the chest.'' ''* Alex runs the command: /data modify block ~ ~ ~ Items[1].tag.pages[3] set value '{"text":"And this pig here\'s named Babe."}' Modified block data of 0 55 0 ''* Alex runs the command: /data modify block ~ ~ ~ Items[1].tag.pages prepend value '{"text":"Call me Ishmael."}' Modified block data of 0 55 0 ''* Alex runs the command: /data modify block ~ ~ ~ Items[1].tag.author set value "Cthulhu the Sleeper"
 * }

History
命令/data