MCreator 方块物品染色(Item Tint)功能详解:原理、配置与代码生成全流程

原创2026-10-15 13:18:261,454 阅读
文章标签:桌面应用游戏开发代码生成低代码开发工具模板引擎

MCreator 方块物品染色(Item Tint)功能详解:原理、配置与代码生成全流程

MCreator 是一款用于制作 Minecraft Java 版模组、Bedrock 版附加包、资源包与数据包的开源可视化开发工具。本文围绕 MCreator 中方块(Block)与植物(Plant)元素的 “方块物品染色(Is block item tinted?)” 选项,结合仓库源码,系统讲解该选项的界面入口、数据模型、代码生成原理以及生成代码的真实运行逻辑,帮助你彻底理解"物品栏中的方块物品如何根据染色类型获得统一色调"这一机制,并能在自己的模组中准确运用。

该选项是 MCreator 方块视觉渲染设置(Visual rendering settings)中的一环。文档原文(德语)说明如下:当该选项启用时,方块在物品栏中会被施加一个固定色调,色调类型与所选的 tint type 匹配;即使指定了对象纹理(object texture),色调依然会被应用。 下文将逐层展开:先讲界面与数据模型,再讲生成代码如何实现"世界中的染色"与"物品栏中的染色"两套渲染管线,最后给出典型使用场景与注意事项。

一、选项是什么:界面位置与作用

在 MCreator 中编辑一个方块(Block)或植物(Plant)元素时,打开元素编辑器的 视觉渲染设置(Visual rendering settings) 面板,可以看到两个密切相关的控件(见 BlockGUI.java):

  • Tint type(色调类型):一个下拉框,可选值包括 No tint(无色)、Grass(草色)、Foliage(树叶色)、Dry foliage、Birch foliage、Spruce foliage、Default foliage、Water、Sky、Fog、Water fog;
  • Is block item tinted?(方块物品是否染色):一个复选框,即本文主题。勾选后,物品栏中的方块物品会按 tint type 施加统一色调。

这两个控件在界面上的完整定义位于 BlockGUI.java:

private final TranslatedComboBox tintType = ComponentFromAnnotation.translatedOptions(Block.class, "tintType",
        "elementgui.block.tint_type.");
private final JCheckBox isItemTinted = L10N.checkbox("elementgui.common.enable");

其中 isItemTinted 的界面文案来自本地化文件 texts.properties,原文为:

Is block item tinted? — If checked, the block will be tinted when in the inventory

也就是说,本选项与"世界中被放置的方块"无关,它只影响手持/物品栏/快捷栏中显示的物品图标。德语帮助文档 is_item_tinted.md 对此补充了两个关键点:

  1. 启用后,物品栏中的方块会被施加一个与所选色调类型匹配的固定色调(fester Farbton);
  2. 即使为该方块指定了对象纹理(object texture),该色调依然会被应用——即物品图标会在对象纹理之上再做一层颜色叠加。

注意:此选项默认关闭

在数据模型层面,无论是方块还是植物,isItemTinted 的默认值都是 false。见 Block.java 中的初始化逻辑(tintType 默认 "No tint" 且 isItemTinted 默认关闭),以及 Plant.java。同时,Block.java 中定义了 tintType 的完整可选项列表(Block.java):

@LimitedOptions({ "No tint", "Grass", "Foliage", "Dry foliage", "Birch foliage", "Spruce foliage",
        "Default foliage", "Water", "Sky", "Fog", "Water fog" }) public String tintType;
public boolean isItemTinted;

值得注意的是,该选项仅对带有物品形式(hasBlockItem)的方块有意义——代码生成时会对 isItemTinted && hasBlockItem 做双重判断(详见下文第三节)。

二、数据模型:选项如何被保存

MCreator 的每个元素(Block/Plant)最终会保存为一个包含 JSON 字段定义的模型对象。Block 与 Plant 元素类型中都包含两个字段:

字段 类型 默认值 说明
tintType String "No tint" 色调类型,取自 @LimitedOptions 列表
isItemTinted boolean false 是否对物品栏中的方块物品染色

MCreator 的集成测试会在随机生成的工作区数据中同时覆盖这两个字段,以保证不同取值组合都能被正确保存与生成。相关测试代码位于 TestWorkspaceDataProvider.java,例如对植物随机指定 tint type 并强制 isItemTinted = true(TestWorkspaceDataProvider.java),对方块同样随机指定 tint type 并打开 isItemTinted(TestWorkspaceDataProvider.java)。

在保存/加载界面状态时,BlockGUI 负责把界面控件与数据模型互相同步:加载时 tintType.setSelectedItem(block.tintType) 与 isItemTinted.setSelected(block.isItemTinted)(BlockGUI.java),保存时反向写回(BlockGUI.java)。

此外,tintType 的选择还会影响方块的 renderType(渲染类型):当模型为单纹理(single texture)、十字(cross)或花盆(potted plant)时,只要 tint type 不是 "No tint",renderType 就会从 11/12/15 切换为 110/120/150,从而让方块模型在世界中也启用染色渲染(BlockGUI.java)。这也解释了为什么"物品栏染色"与"世界染色"是两套独立但共用一个色调类型设置的管线。

三、代码生成:isItemTinted 如何变成真实代码

当你在 MCreator 中点击"生成代码"后,isItemTinted 会在多个 FreeMarker 模板(.ftl)中被消费。以 NeoForge 1.21.1 生成器为例,涉及的关键模板有:

3.1 汇总模板中的双重条件判断

在 blocks.java.ftl 中,生成器会遍历工作区内所有 block 与 plant 元素,统计两类标志:

<#assign hasTintedBlocks = false>
<#assign hasTintedBlockItems = false>
<#list blocks as block>
    <#if block.getModElement().getTypeString() == "block">
        <#if block.tintType != "No tint">
            <#assign hasTintedBlocks = true>
            <#if block.isItemTinted && block.hasBlockItem>
                <#assign hasTintedBlockItems = true>
            </#if>
        </#if>
    ...
</#list>

只有当存在"世界染色方块"(hasTintedBlocks)或"物品染色方块"(hasTintedBlockItems)时,才会生成客户端事件处理类 BlocksClientSideHandler(blocks.java.ftl):

@EventBusSubscriber(Dist.CLIENT) public static class BlocksClientSideHandler {
    @SubscribeEvent public static void blockColorLoad(RegisterColorHandlersEvent.Block event) {
        <#list blocks as block>
            <#if block.tintType != "No tint">
                 ${block.getModElement().getName()}Block.blockColorLoad(event);
            </#if>
        </#list>
    }
    @SubscribeEvent public static void itemColorLoad(RegisterColorHandlersEvent.Item event) {
        <#list blocks as block>
            <#if block.tintType != "No tint" && block.isItemTinted && block.hasBlockItem>
                 ${block.getModElement().getName()}Block.itemColorLoad(event);
            </#if>
        </#list>
    }
}

注意 itemColorLoad 的事件注册条件:tintType != "No tint"、isItemTinted == true、hasBlockItem == true 三者缺一不可。这也从源码层面印证了帮助文档的核心语义:物品染色是独立于世界染色的一层可选渲染。

3.2 方块模板中的 itemColorLoad 实现

在 block.java.ftl 中,勾选 isItemTinted 且方块有物品形式时,会生成如下客户端代码:

<#if data.isItemTinted && data.hasBlockItem>
@OnlyIn(Dist.CLIENT) public static void itemColorLoad(RegisterColorHandlersEvent.Item event) {
    event.getItemColors().register((stack, index) -> {
        <#if data.tintType == "Grass">
            return GrassColor.get(0.5D, 1.0D);
        <#elseif data.tintType == "Foliage" || data.tintType == "Default foliage">
            return FoliageColor.getDefaultColor();
        <#elseif data.tintType == "Birch foliage">
            return FoliageColor.getBirchColor();
        <#elseif data.tintType == "Spruce foliage">
            return FoliageColor.getEvergreenColor();
        <#elseif data.tintType == "Dry foliage">
            return 10710342;
        <#elseif data.tintType == "Water">
            return 3694022;
        <#elseif data.tintType == "Sky">
            return 8562943;
        <#elseif data.tintType == "Fog">
            return 12638463;
        <#else>
            return 329011;
        </#if>
    }, ${JavaModName}Blocks.${REGISTRYNAME}.get());
}
</#if>

植物模板 plant.java.ftl 的 itemColorLoad 实现与方块完全一致。这段生成代码说明了几个重要事实:

  1. 物品染色返回的是一个固定颜色值,与方块在世界中的位置无关(世界染色则是基于 world/pos 的群系色,见 block.java.ftl);
  2. 每种 tint type 映射到明确的颜色值(十进制 RGB),其中 Dry foliage 在 1.21.1 中不存在对应 API,因此使用常量 10710342 代替(模板中也有对应注释);
  3. 这些颜色值与被染色的纹理通过 tintindex 机制叠加(在旧版 JSON 模型中通常对应 "tintindex": 0 的图层),因此即使指定了对象纹理(object texture),只要该纹理带有 tint 标记层,色调就会叠加其上——这正是文档所说"即使指定对象纹理,色调依然被应用"的底层原因。

3.3 新版生成器(26.3.x)的 JSON 模型实现

在 MCreator 26.3.x 对应的生成器 generator-26.3.x 中,物品模型的 JSON 模板 model_item_block.json.ftl 采用了新版 Minecraft 的物品模型 tint 机制:

{
  "model": {
    "type": "minecraft:model",
    "model": "${modid}:item/${registryname}"
    <#if data.tintType != "No tint" && data.isItemTinted>,
    "tints": [
      {
        <#if data.tintType == "Grass">
        "type": "minecraft:grass",
        "downfall": 1.0,
        "temperature": 0.5
        <#elseif data.tintType == "Foliage" || data.tintType == "Default foliage">
        "type": "minecraft:constant",
        "value": -12012264
        ...
        <#else>
        "type": "minecraft:constant",
        "value": 329011
        </#if>
      }
    ]
    </#if>
  }
}

可以看到,26.3.x 在保留"tintType != No tint 且 isItemTinted"双重条件的基础上,改用数据驱动的方式:Grass 使用 minecraft:grass 类型并按温度 0.5、降水量 1.0 计算草色,其余类型使用 minecraft:constant 常量色。与 1.21.1 模板中手写的 itemColorLoad Java 代码相比,这是 Minecraft 新版物品渲染 API 演进后的等效实现。

四、两条渲染管线的对比

为了帮助你准确理解"世界染色"与"物品染色"的区别,把两者对比整理如下:

维度 世界中的方块染色(blockColorLoad) 物品栏中的物品染色(itemColorLoad)
触发条件 tintType != "No tint" tintType != "No tint" 且 isItemTinted == true 且 hasBlockItem == true
生成位置 方块/植物自身模板 + blocks.java.ftl 汇总注册 同上(BlocksClientSideHandler.itemColorLoad)
颜色来源 基于 world/pos 的群系色(如 BiomeColors.getAverageGrassColor) 固定色(如 GrassColor.get(0.5D, 1.0D) 或常量色值)
影响对象 世界中放置的方块表面 物品栏/手持/快捷栏中的物品图标
与对象纹理关系 纹理 tint 层叠加 纹理 tint 层叠加(对象纹理也生效)

简言之:tint type 决定"染什么颜色",isItemTinted 决定"这个颜色是否也要出现在物品栏图标上"。

五、典型使用场景与实操建议

结合上述机制,以下是几个贴合实际模组开发的场景:

  • 草地/植物类方块:通常选择 Grass 或 Foliage tint type,并勾选 Is block item tinted?,让物品栏中的草方块、树叶等图标与世界中呈现的群系色调保持一致,避免"物品栏是纯色贴图、放置后颜色突变"的割裂感;
  • 水、天空、雾气类装饰方块:选择 Water/Sky/Fog/Water fog,物品图标会呈现对应的固定环境色;
  • 带对象纹理的方块:如果你为方块指定了对象纹理(object texture),该选项依然会叠加色调,因此需要留意"纹理本身是否已带有颜色"——若对象纹理已是最终成品色,则不应再开启物品染色,以免出现双重着色;
  • 无物品形式的方块:对于纯方块实体(如仅存在于世界中的技术性方块,hasBlockItem 关闭),该选项不产生效果,因为生成代码要求 hasBlockItem == true。

六、小结

MCreator 的"方块物品染色(Is block item tinted?)"是一个轻量但实用的视觉选项:它在数据模型层面由 isItemTinted 布尔字段与 tintType 字符串字段共同控制,在代码生成层面通过 FreeMarker 模板产出对应的颜色处理器或 JSON tints 定义。其核心语义可以一句话概括——让物品栏中的方块物品获得与所选 tint type 匹配的固定色调,且该色调叠加在对象纹理之上。理解了 blockColorLoad 与 itemColorLoad 两条管线的分工,你就能在 MCreator 中精准控制方块在"世界"与"物品栏"两个场景下的观感表现。

延伸阅读

登录后查看全文
MCreator