Hello there! We are conducting a survey to better understand the user experience in making a first edit. If you have ever made an edit on Gamepedia, please fill out the survey. Thank you!

Commands/data

From Minecraft Wiki
Jump to: navigation, search
Information icon.svg
This feature is exclusive to Java Edition.
data
Restrictions

Operator

First introduced

Java Edition 1.13 (Java Edition 17w45a)

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

Syntax

There are four instructions for /data (get, merge, modify, remove), and the targets/sources referenced by each instruction command may be either block <targetPos> or entity <target>.

data get block <targetPos> [<path>] [<scale>]
data get entity <target> [<path>] [<scale>]
data merge block <targetPos> <nbt>
data merge entity <target> <nbt>
data modify block <targetPos> <targetPath> (append|insert <index>|merge|prepend|set) from block <sourcePos> <sourcePath>
data modify block <targetPos> <targetPath> (append|insert <index>|merge|prepend|set) from entity <source> <sourcePath>
data modify block <targetPos> <targetPath> (append|insert <index>|merge|prepend|set) value <nbt>
data modify entity <target> <targetPath> (append|insert <index>|merge|prepend|set) from block <sourcePos> <sourcePath>
data modify entity <target> <targetPath> (append|insert <index>|merge|prepend|set) from entity <source> <sourcePath>
data modify entity <target> <targetPath> (append|insert <index>|merge|prepend|set) value <value>
data remove block <targetPos> <targetPath>
data remove entity <target> <path>
Syntax trees
Arguments
(block <targetPos>|entity <target>):
  • <targetPos>—The <x> <y> <z> position of the target tile entity whose NBT is to be operated on
  • <target>—Valid UUID, or target selector variable for an entity whose NBT is to be operated on
get . . . [<path>] [<scale>]:
  • <path>—Valid path name defining the specific NBT to retrieve
  • <scale>—Scalar for the command's return value
merge . . . <nbt>:
  • <nbt>—Compound tag. The simplest valid imput is {}.
modify . . . <targetPath> (append|insert <index>|merge|prepend|set):
  • <targetPath>—Valid path name defining the specific target NBT to modify
  • <index>—Valid integer specifying an item's index within a list
… from (block <sourcePos>|entity <source>) <sourcePath>:
  • <sourcePos>—The <x> <y> <z> position of the source block whose NBT is to be used
  • <source>—Valid player name, UUID, or target selector variable of a source entity whose NBT is to be used
  • <sourcePath>—Valid path name defining the specific source NBT to be used by modify
… value <value>:
  • <value>—Numeric value to use in modifying the target NBT. Must match in data type
remove . . . <path>:
  • <path>—Valid path name to the NBT to remove
See § NBT path
Results
  • Command fails if the arguments are not specified correctly.
  • On success:
/data get
/data get . . .
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.
/data get . . . <path>
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.
/data get . . . <path> <scale>
Return the value of the tag, scaled by <scale>.
<path> needs to specify a numeric tag—TAG_byte, TAG_short, TAG_int, TAG_long, TAG_float, or TAG_double.
/data merge
Merge the NBT data from the sourced block position or entity with the specified <nbt> data.
If applicable, returns 1 on success.
/data modify
append
Append the source data onto the end of the pointed-to list.
<targetPath> must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
insert <index>
Insert the source data into the pointed-to list as element <index>, then shift higher elements one position upwards.
<targetPath> must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
merge
Merge the source data with the pointed-to object.
<targetPath> must specify a tag of the TAG_list type, and source data must be of the TAG_compound type.
prepend
Prepend the source data onto the beginning of the pointed-to list.
<targetPath> must specify a tag of the TAG_list type, and the source data should be of an item type appropriate for the list.
set
Set the tag specified by <targetPath> to the source data.
/data remove
Removes NBT data at <path> from the targeted block position or entity. Player NBT data cannot be removed.
If applicable, returns 1 on success.

NBT path[edit]

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 superTag.childTag.childTag……parentTag.tag, where tag is the specified tag and superTag 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: a-z A-Z 0-9 _ . [ ] { } (To put this as a Regular expression, [][}{a-zA-Z0-9.]*) "Quoted strings" may be used if a name of a key needs to be escaped.

Examples[edit]

Fubar example:[edit]

/data get entity @p foo.bar[0]."A [crazy name]!".baz

These names have been arbitrarily picked, for demonstrative purposes.

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

Book-in-a-chest example:[edit]

/data get block ~ ~ ~ Items[1].tag.pages[3]

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.

History[edit]

Java Edition
1.1317w45bAdded /data.
18w03a/data get with a path now works on non-numeric values.
1.1418w43aAdded /data modify.