📄 模板系统
简介
该插件拥有极高的可自定义性,但完全无预设的配置难以实现。即便最简单的选项也需在YAML中单独声明。当此类参数过多时,配置文件会变得冗长。为此,插件引入了模板系统,您可先定义基础模板,再通过参数和覆写机制来简化配置流程,大幅减少重复性操作耗时。
它是如何工作的?
配置模板时,需以 templates
作为 YAML 文件的根键。 templates
下的首个元素即为模板名称(如下例中 namespace:my_first_template
),该名称下方的所有内容构成实际模板配置。
templates:
namespace:my_first_template:
option_1: true
option_2: false
option_3:
- hello
option_4: 20.25
option_5:
hello: world
观看这段快速动画了解插件如何应用模板:
namespace:template_id
作为模板的唯一标识符,在后续调用时必须使用该完整名称。
namespace:template_id
下方的配置部分完全可自定义,只需符合YAML语法规范即可自由定义。
多模板
您可以通过将 template
设置为列表来组合多个模板。
items:
craftengine:custom_item:
template:
- namespace:my_first_template
- namespace:my_second_template
注意:若两个模板存在相同配置项,下方的模板会覆盖上方配置。但当配置项为列表类型时,它们将合并为统一列表。
参数
在模板中可使用 ${xxx}
定义参数(如 ${nutrition}
或 ${saturation}
),调用时通过 arguments
节点传入具体值。示例:
templates:
craftengine:apple_template:
material: apple
data:
food:
nutrition: "${nutrition}"
saturation: "${saturation}"
items:
craftengine:custom_apple:
template: craftengine:apple_template
arguments:
nutrition: 1
saturation: 2.5
若需将花括号 {}
作为普通文本使用(而非参数标识),只需使用反斜杠转义:\{
或 \}
。
\${你好世界}
为参数设置默认值时,在参数名后添加 :-
即可,例如:
${nutrition:-1}
${saturation:-2.5d}
${map:-{aa:bb,cc:ddd}}
${string:-"1234"}
默认值采用Minecraft的SNBT格式(与指令中指定NBT数据时的格式相同)。
覆写
覆写会完全替换指定配置路径下的所有内容(包括列表和映射),不进行合并操作,直接整体替换。
items:
craftengine:custom_bread:
template: craftengine:apple_template
arguments:
nutrition: 1
saturation: 2.5
overrides:
material: bread
合并
深度合并会完整整合两个配置部分(包括所有列表项),其运作逻辑与多重模板几乎完全一致。
items:
craftengine:custom_bread:
template: craftengine:apple_template
arguments:
nutrition: 1
saturation: 2.5
merges:
data:
food:
can-always-eat: true
小提示
虽然不常用,你也可以在配置键中使用参数。
templates:
craftengine:example:
"${argument}": value
你也可以在模板列表、覆写和合并中使用参数。
templates:
default:settings/ore:
template:
- default:sound/stone
- default:pickaxe_power/level_${break_power}
overrides:
push-reaction: NORMAL
is-redstone-conductor: true
is-suffocating: true
instrument: BASEDRUM
can-occlude: true
map-color: ${map_color}
tags:
- minecraft:mineable/pickaxe
merges:
hardness: ${hardness}
resistance: 3.0