Skip to content

自定义标签 ​

概述 ​

标签(tag)用于给一批方块或物品打上相同的分类标识,便于统一查询和判断,例如把多个模组中的“苹果”类物品都归入common:apple标签。

行为包新增netease_tags目录,允许开发者为方块和物品增加tag。

注意:物品tag和方块tag是两类相互独立的tag,即使identifier相同也不会互通。两者的具体区别可查阅基岩版开发wiki中关于物品标签与方块标签的说明。

配置方法 ​

在行为包下新建netease_tags文件夹,并在该目录下添加任意名称的json文件,例如:apple.json。一个json文件对应注册一个tag。

behavior_pack_xxxxxx
└── netease_tags
    ├── apple.json
    └── sand.json

字段说明 ​

字段类型说明
typestrtag类型,item表示物品tag,block表示方块tag
identifierstrtag标签名,例如common:apple
itemslist(str)该tag包含的方块或物品列表。可填写原版或自定义的方块/物品identifier;以#开头时表示引用另一个同类型的tag,即包含所有具有该tag的方块/物品

物品tag注册 ​

{
    "type": "item", // 物品tag注册
    "identifier": "common:apple", // tag标签名为 common:apple
    "items": [
        "minecraft:apple", // 原版物品id
        "example:apple", // 模组物品id
        "#netease:apple" // 其他所有具有netease:apple标签的物品
    ]
}

方块tag注册 ​

{
    "type": "block", // 方块tag注册
    "identifier": "sand", // tag标签名为 sand
    "items": [
        "minecraft:dirt" // 给原版的泥土增加sand这个tag
    ]
}

多模组合并规则 ​

若多个模组对同一个标签(type相同且identifier相同)进行了注册,系统会自动合并各模组的items列表并去重,而不是相互覆盖。

例如模组a与模组b都注册了物品tag common:apple:

// modA/netease_tags/apple.json
{
    "type": "item",
    "identifier": "common:apple",
    "items": [
        "minecraft:apple",
        "#netease:apple"
    ]
}
// modB/netease_tags/apple.json
{
    "type": "item",
    "identifier": "common:apple",
    "items": [
        "minecraft:apple",
        "example:apple",
        "#myg0:apple"
    ]
}

两个模组同时加载后,最终的注册结果等价于:

{
    "type": "item",
    "identifier": "common:apple",
    "items": [
        "minecraft:apple", // 原版物品id,两个模组都填写了,合并后只保留一份
        "example:apple", // 模组物品id
        "#myg0:apple", // 其他所有具有myg0:apple标签的物品
        "#netease:apple" // 其他所有具有netease:apple标签的物品
    ]
}

查询标签 ​

注册完成后,可通过以下接口在服务端或客户端查询某个tag下的所有方块/物品,查询结果会自动展开#引用的标签,并包含多模组合并后的内容;未通过netease_tags注册的原版tag也可以直接查询。

python
import mod.server.extraServerApi as serverApi
comp = serverApi.GetEngineCompFactory().CreateItem(levelId)
# 返回 [{'itemName': 'minecraft:apple'}, {'itemName': 'example:apple'}, ...]
comp.LookupItemsByItemTag("common:apple")

注意点 ​

1.type只能填写item或block,物品tag与方块tag分别独立注册、独立合并。

2.不同模组只有在type和identifier都相同时才会合并,建议为tag的identifier加上命名空间(如common:),避免与其他模组的tag意外合并。