命令/infobox
允许玩家获取、修改、合并或是移除实体或方块的数据标签。其中,修改数据标签可以是将一个标签的值设为和另一个方块或实体的某个标签的值相同,或者在一个列表中插入值,或者直接修改为一个具体的数值。
- 语法
/data
有四个子命令(get
,merge
,modify
,remove
),每个子命令提及的目标或源都是block <目标方块坐标>
或entity <目标>
。
data get block <目标方块坐标> [<路径>] [<倍率>]
data get entity <目标实体> [<路径>] [<倍率>]
data merge block <目标方块坐标> <nbt>
data merge entity <目标实体> <nbt>
data modify block <目标方块坐标> <目标路径> (append|insert <索引>|merge|prepend|set) from block <源方块坐标> <路径>
data modify block <目标方块坐标> <目标路径> (append|insert <索引>|merge|prepend|set) from entity <源实体> <路径>
data modify block <目标方块坐标> <目标路径> (append|insert <索引>|merge|prepend|set) value <nbt>
data modify entity <目标实体> <目标路径> (append|insert <索引>|merge|prepend|set) from block <源方块坐标> <路径>
data modify entity <目标实体> <目标路径> (append|insert <索引>|merge|prepend|set) from entity <源实体> <路径>
data modify entity <目标实体> <目标路径> (append|insert <索引>|merge|prepend|set) value <值>
data remove block <目标方块坐标> <目标路径>
data remove entity <目标实体> <路径>
- 语法树状图
简化的语法树:
详细的语法树: /data
… get
… block <目标方块坐标> [<路径>] [<倍率>]
… entity <目标实体> [<路径>] [<倍率>]
… merge
… block <目标方块坐标> <nbt>
… entity <目标实体> <nbt>
… modify
… block <目标方块坐标> <目标路径> (append|insert <索引>|merge|prepend|set)
… from
… block <源方块坐标> [<源路径>]
… entity <源实体> [<源路径>]
… value <值>
… entity <目标实体> <目标路径> (append|insert <索引>|merge|prepend|set)
… from
… block <源方块坐标> [<源路径>]
… entity <源实体> [<源路径>]
… value <值>
… remove
… block <源方块坐标> [<源路径>]
… entity <源实体> [<源路径>]
压缩的语法树:
一行版: /data (get (block <目标方块坐标>|entity <目标实体>) [<路径>] [<倍率>]|merge (block <目标方块坐标>|entity <目标实体>) <nbt>|modify (block <目标方块坐标>|entity <目标实体>) (append|insert <索引>|merge|prepend|set) (from (block <源方块坐标>|entity <源实体>) <源路径>|value <值>)|remove (block <目标方块坐标>|entity <源实体>) <路径>)
- 参数
(block <目标方块坐标>|entity <目标实体>)
:
<目标方块坐标>
—指定需要获取、修改、合并或移除数据标签的方块的<x> <y> <z>
坐标。<目标实体>
—指定需要获取、修改、合并或移除数据标签的目标实体,可以是一个合法的UUID或目标选择器。
get . . . [<路径>] [<倍率>]
:
<路径>
—指定检索特定NBT的合法的路径名。<倍率>
—缩放获得的NBT具体数值。
merge . . . <nbt>
:
<nbt>
—Compound标签。最简单的合法输入是{}
。
modify . . . <目标路径> (append|insert <索引>|merge|prepend|set)
:
<目标路径>
—指定修改特定NBT的合法的路径名。<索引>
—指定列表中元素的索引的有效整数。
… from (block <源方块坐标>|entity <源实体>) <源路径>
:
<源方块坐标>
—指定需要获取、修改、合并或移除数据标签的方块的<x> <y> <z>
坐标。<源实体>
—指定需要获取、修改、合并或移除数据标签的作为NBT来源的实体,可以是一个合法的UUID,玩家名或目标选择器。<源路径>
—指定修改特定NBT的来源的合法的路径名。
… value <值>
:
<值>
—一般是修改目标NBT要使用的数值。必须符合数据类型。
remove . . . <路径>
:
<路径>
—指定删除NBT的合法的路径名。
- 效果
- 当参数没有被正确指定时失败。(若目标实体的选择器没有规定实体数量为1,则是命令语法错误而非执行失败。)
- 若成功:
/data get
-
/data get . . .
- 返回指定位置的方块或单个目标实体的NBT数据,并且带有语法高亮。
- 若适用,成功时的返回值为1。
/data get . . . <路径>
- 返回指定位置的方块或单个目标实体的一部分NBT数据,并且不带有语法高亮。
- 返回值取决于请求的NBT元素的类型。
- 请求一个数字,则返回值为数字。
- 请求一个字符串,则返回该字符串的长度。
- 请求一个列表会返回该列表中元素的个数。
- 请求一个Compound标签会返回该标签下一级子元素(不包括子元素的子元素)的数量。
/data get . . . <路径> <倍率>
- 返回该标签的数值并乘<倍率>。
- 此时<路径>需要指向一个数字类型的标签,即该标签的类型可以为TAG_byte,TAG_short,TAG_int,TAG_long,TAG_float或TAG_double。
/data merge
- 将从源方块坐标或源实体的特定NBT数据合并到目标。
- 若适用,成功时的返回值为1。
/data modify
-
append
- 在指向的列表的末尾增加一个元素。
<目标路径>
必须指定一个TAG_list类型的元素,源数据应该符合该列表中元素的类型。insert <索引>
- 将数据以第
<索引>
个元素插入指向的列表,其后的元素的编号自动加一。 <目标路径>
必须指定一个TAG_list类型的元素,源数据应该符合该列表中元素的类型。merge
- 将源数据与指定的元素合并。
<目标路径>
必须指定一个TAG_list类型的元素,源数据必须是TAG_compound类型。prepend
- 在指向的列表的开头增加一个元素。
<目标路径>
必须指定一个TAG_list类型的元素,源数据应该符合该列表中元素的类型。set
- 将
<目标路径>
特指的标签设定为源数据。
/data remove
- 移除指定位置的方块或单个目标实体指定路径的NBT数据。无法移除玩家的NBT数据。
- 若适用,成功时的返回值为1。
NBT路径
NBT路径是一个描述性的标识,用于从一个NBT数据树中特定任意元素。一个路径通常以superTag.childTag.childTag……parentTag.tag
的形式存在。此处tag
是被指定的标签,superTag
是其中一个最外层的标签。
每个键之后可以给出一个NBT对象来与那个标签的值比较。标签仅当比较存在正的结果时被路径作为对象。
当从列表中定义一个元素时,方括号被用在列表后的键。方括号内的内容可以是以下三种的任意一种:
- 什么都不填—将会返回列表中的每一个元素。[note 1]
- 一个索引数字—对于一个非负索引n,会返回列表中的第(n+1)个元素。对于一个负的索引n,会从列表结尾开始,往前取第n个元素。
- 一个NBT复合标签—会返回所有含有方括号内NBT标签的对象。
一个路径的合法的字符集似乎是所有的非空字符,只要所有的未闭合的方括号和花括号([
和{
)被正确闭合,并位于每一项的结尾即可。(即其后只有一个句点或路径的结尾—.
或
)。也就是说,定义一个数据标签的合理的字符集是a-z A-Z 0-9 _ . [ ] { }
(用正则表达式表示就是[][}{a-zA-Z0-9.]*
)。如果一个键的名字中存在空格,可以用双引号括起来。
示例
- 获取所有生物的所有数据:
execute as @e run data get entity @s
- 获取自己的物品栏中的第一个物品数据:
data get entity @s Inventory[0]
- 获取随机一个骷髅的装备数据:
data get entity @e[type=skeleton,sort=random,limit=1] ArmorItems
- 将所有的羊染成白色:
execute as @e[type=sheep] run data merge entity @s {Color:0b}
或者execute as @e[type=sheep] run data modify entity @s Color set value 0b
- 将除玩家之外的所有生物设为不可摧毁(被创造模式玩家摧毁除外):
execute as @e run data merge entity @s {Invulnerable:1b}
或execute as @e run data modify entity @s Invulnerable set value 1b
,由于不能修改玩家的数据,所以玩家会被自动过滤掉,故选择器无需再添加type=!player。
Foo bar示例
/data get entity @p foo.bar[0]."A [crazy name]!".baz
以上NBT路径中的名字仅用于演示用途,是随机挑选的。
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.bar[]
—指定列表“bar”中的所有元素。foo.bar[].baz
—指定列表“bar”中所有元素的子元素“baz”。foo.bar[{baz:5b}]
—指定“bar”列表中所有“baz”标签被设置为5b
的元素。{}
—指定目标实体或方块的根标签。{foo:4.0f}
—在“foo”标签被设定为4.0f
时指定目标实体或方块的根标签。foo{bar:"baz"}
—指定标签“foo”,如果其子标签“bar”的值为"baz"
。foo{bar:"baz"}.bar
—指定标签“bar”,如果其值为"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”。
箱中书
/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"
存储
使用NBT数据保存的命令存储是访问或储存NBT数据的有效方法,且其无需通过方块实体或实体读取或写入NBT数据。
每个命令存储都保存着通用的键值对。命令存储使用命名空间ID进行区分。
/data get
和/data modify
可以读取存储,而/data merge
、/data modify
、/data remove
和/execute store
可以写入存储。
命令存储也可以通过原始JSON文本读取。
历史
Java版 | |||||
---|---|---|---|---|---|
1.13 | 17w45b | 加入了/data 。 | |||
1.14 | 18w43a | 加入了/data modify 。 | |||
1.15 | 19w38a | 加入了命令存储。 | |||
加入了storage <命名空间ID> 作为来源或目标。 |
注释
引用错误:组名为“note”的<ref>
标签存在,但没有找到相应的<references group="note"/>
标签