此特性为Java版独有。
NBT路径(NBT path)是用来从NBT数据树中指定一系列特定元素的描述性标签。路径的基本格式为节点.….节点,其中每个节点声明了可以从先前标签中选择哪种类型的子元素。
节点
有6种可用的节点(node)。
标签集合(tags collection)仅从一个元素开始(例如根标签)并随路径中的节点一同改变。NBT路径会在最后一个标签集合生效。
| 名称 | 格式 | 描述 | 选择结果 | 示例 | 示例描述 |
|---|---|---|---|---|---|
| 根复合标签 | {tag},其中tag是复合NBT内容,可以为空。
|
只有在开始标签匹配给定的复合标签(若有)则选择开始标签。 只能应用于路径中的第一个元素。 |
开始标签,或者匹配失败时无。 | {Invisible:1b}
|
如果其子标签 Invisible的值为1则选择根标签。
|
{}
|
选择根标签。 | ||||
| 命名标签 | name,其中name可以是直接的或转义的字符串。
|
选择上个标签中有叫做name的子标签。 | 标签的集合;其中元素个数可能超过上个标签集合。可能为空。 | VillagerData
|
选择VillagerData子标签。 |
"A cool name[]"
|
选择A cool name[]子标签。 | ||||
| 命名复合标签 | name{tag},其中name可以是直接的或转义的字符串,tag是复合NBT内容且可以为空。
|
选择匹配给定的且上个标签中命名为name复合标签(如有)的子复合标签。 | 复合标签集合;其中元素个数不超过上个标签集合。可能为空。 | VillagerData{profession:"minecraft:nitwit"}
|
选择 VillagerData标签,前提是有子标签 profession且值为minecraft:nitwit。
|
VillagerData{}
|
选择 VillagerData标签,前提是这是一个复合标签。 | ||||
| 命名列表或数组标签的元素 | name[index],其中name可以是直接的或转义的字符串,index是整数。
|
选择上个标签中命名为name的列表(或数组)中位于index处(如果index是负数,则是位于(列表长度+index)处)的元素。
|
标签集合;其中元素个数不超过上个标签集合。可能为空。 | Pos[0]
|
选择Pos子列表(或数组)中的第一个子标签。 |
Inventory[-1]
|
选择Inventory子列表(或数组)中的最后一个子标签。 | ||||
| 命名列表或数组标签的所有元素 | name[]
|
选择上个标签中命名为name的子列表(或数组)中的所有元素。 | 标签集合,其中元素个数可能超过上个标签集合,可能为空。 | ActiveEffects[]
|
选择最后一个标签的ActiveEffects子标签的所有元素。 |
| 命名列表标签的复合元素 | name[{tag}],其中name可以是直接的或转义的字符串,tag是复合NBT内容且可以为空。
|
选择上个标签中匹配给定的(如有)和列表中命名为name的复合标签的复合元素。 | 复合标签集合;其中元素个数可能超过上个标签集合,可能为空。 | Inventory[{Count:25b}]
|
选择拥有 Count子标签且其值为25的 Inventory标签的元素。
|
Foo[{}]
|
选择 Foo标签的元素。 | ||||
| 命名列表标签的子列表(或数组)的元素 | name[index或者空][index或者空][index或者空]......[index或tag],其中name可以是直接的或转义的字符串,index是整数,tag是复合NBT内容且可以为空。
|
选择上个标签中命名为name的列表中的子元素的子元素。 | 标签集合;其中元素个数可能超过上个标签集合,可能为空。 | foo[][{}]
|
选择 foo标签中子列表的元素。 |
foo.bar[0][0].baz
|
选择 foo标签中的bar列表的第一个元素的第一个元素的baz标签。 |
路径中的.(点)字符将节点分隔开来。节点可以是混合的或匹配的,但带有对象的根节点(如有)必须是路径中首先出现的。
名称转义
路径的合法的字符集(Valid character set)似乎是所有的非空字符,只要所有的未闭合的方括号和花括号([和{)被正确闭合,并位于每一项的结尾即可(即其后只有一个句点或路径的结尾—.或 )。也就是说,定义一个数据标签的合理的字符集包括大小写字母、数字、下划线、点、方括号和花括号(用正则表达式表示就是[][}{a-zA-Z0-9.]*)。
如果键的名称需要转义,可以使用引号括起来,比如"Lorem ipsum"。
示例
混合路径
{}——指定根标签{foo:4.0f}——指定根标签,如果其foo子标签的值为4.0f。foo——指定根标签下命名为foo的标签。foo.bar或foo{}.bar——指定foo的命名为bar的子标签。foo.bar[0]——指定列表(或数组)bar的第一个元素。foo.bar[-1]——指定列表(或数组)bar的最后一个元素。foo.bar[0]."A [crazy name]!"——指定第一个元素下命名为"A [crazy name]!"的子标签。foo.bar[0]."A [crazy name]!".baz——指定上述子标签中命名为baz的子标签。foo.bar[]——指定列表(或数组)bar的所有元素。foo.bar[].baz——指定(或数组)列表bar的所有元素中命名为baz的子标签。foo.bar[{baz:5b}]——指定列表bar中所有拥有baz子标签且其值为5b的元素。foo{bar:"baz"}——指定所有foo标签,如果其子标签bar的值为"baz"。foo{bar:"baz"}.bar——指定foo标签中的bar标签,如果其值为baz。
示例1
/data get entity @p foo.bar[0]."A [crazy name]!".baz
这些名称是随意取的,仅用于演示。
foo——指定根标签下命名为foo的子标签。foo.bar——指定foo的命名为bar的子标签。foo.bar[0]——指定上述列表bar中的第一个元素。foo.bar[0]."A [crazy name]!"——指定上述第一个元素中的命名为"A [crazy name]!"的子标签。foo.bar[0]."A [crazy name]!".baz——指定上述子标签中命名为baz的子标签。
树状图
- 实体的根数据
- foo:foo元素
- bar:bar元素
- 列表bar的第一个元素
- A [crazy name]!:"A [crazy name]!"元素
- baz:baz元素;本示例的目标元素
- A [crazy name]!:"A [crazy name]!"元素
- 列表bar中的另一个无关元素
- 列表bar的第一个元素
- bar:bar元素
- foo:foo元素
示例2
/data get block ~ ~ ~ Items[1].tag.pages[3]
某玩家写了一本书,将其放在了脚下的箱子里。她要一步步尝试拆解这条命令,最终运行上述命令。
聊天栏记录
* Alex跳上了箱子。 * Alex运行命令: /data get block ~ ~ ~ 0, 55, 0拥有以下方块数据:{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只想看看这个箱子的物品栏。虽然她可以搜一搜Minecraft Wiki来获取那个标签的名字,但由于她知道如何阅读NBT格式,她决定从上个命令的输出中把它弄明白。 * Alex运行命令: /data get block ~ ~ ~ Items 0, 55, 0拥有以下方块数据:{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想把输出范围缩减到箱子内的第二个物品。她从0开始数,得出第二个物品是元素1。 * Alex运行命令: /data get block ~ ~ ~ Items[1] 0, 55, 0拥有以下方块数据:{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只想要标签“tag”。 * Alex运行命令: /data get block ~ ~ ~ Items[1].tag 0, 55, 0拥有以下方块数据:{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只想要标签“pages”。 * Alex运行命令: /data get block ~ ~ ~ Items[1].tag.pages 0, 55, 0拥有以下方块数据:{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只想要这个列表的第四个元素。 * Alex运行命令: /data get block ~ ~ ~ Items[1].tag.pages[3] 0, 55, 0拥有以下方块数据:{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达到了她的目的。她想使用在箱子外部搞出来的NBT路径来编辑这本书。 * Alex运行命令: /data modify block ~ ~ ~ Items[1].tag.pages[3] set value '{"text":"And this pig here\'s named Babe."}' 已修改0, 55, 0处的方块数据 * Alex运行命令: /data modify block ~ ~ ~ Items[1].tag.pages prepend value '{"text":"Call me Ishmael."}' 已修改0, 55, 0处的方块数据 * Alex运行命令: /data modify block ~ ~ ~ Items[1].tag.author set value "Cthulhu the Sleeper"