📄 模板 [必读]

本页主要说明如何在服务器中添加新模板

引言

该插件拥有极高的可自定义性,但完全无预设的配置难以实现。即便最简单的选项也需在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作为模板的唯一标识符,在后续调用时必须使用该完整名称

多模板

您可以通过将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
译者注:我还没拿到这段动画的源文件无法翻译

若需将花括号{}作为普通文本使用(而非参数标识),只需使用反斜杠转义:\{\} \${Hello world} ${a\{b\}c}

覆写

覆写会完全替换指定配置路径下的所有内容(包括列表和映射),不进行合并操作,直接整体替换

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

最后更新于

这有帮助吗?