内置配方类型

内置配方类型

Minecraft 内置了多种可直接使用的配方类型和序列化器。本文介绍各配方类型及其数据生成方法。

合成

合成配方通常在工作台、合成器,或模组工作台、机器中使用。其配方类型为 minecraft:crafting。

有序合成

工作台、木棍、大多数工具等重要配方都通过有序合成创建。这些配方定义了必须按其摆放物品的图案或形状,因此称为“有序”。示例如下:

{
    "type": "minecraft:crafting_shaped",
    "category": "equipment",
    "pattern": [
        "XXX",
        " # ",
        " # "
    ],
    "key": {
        "#": {
            "item": "minecraft:stick"
        },
        "X": {
            "item": "minecraft:iron_ingot"
        }
    },
    "result": {
        "count": 1,
        "id": "minecraft:iron_pickaxe"
    }
}

逐行解释如下:

  • type:有序配方序列化器 ID,即 minecraft:crafting_shaped。
  • category:可选字段,定义配方书中的分类。
  • key 和 pattern:共同定义物品在合成网格中的摆放方式。
    • pattern 最多定义三行字符串,每行最多三个字符,表示配方形状。所有行长度必须相同,即图案必须为矩形。空格表示应留空的槽位。
    • key 将图案中使用的字符关联到原料。上例中,所有 X 必须是铁锭,所有 # 必须是木棍。
  • result:配方结果,采用物品堆叠的 JSON 表示。
  • 示例中未展示 group 键。这个可选字符串属性用于在配方书中创建分组;同组配方会在配方书中合并显示。

下面是生成该配方的方法:

// We use a builder pattern, therefore no variable is created. Create a new builder by calling
// ShapedRecipeBuilder#shaped with the recipe category (found in the RecipeCategory enum)
// and a result item, a result item and count, or a result item stack.
ShapedRecipeBuilder.shaped(RecipeCategory.TOOLS, Items.IRON_PICKAXE)
        // Create the lines of your pattern. Each call to #pattern adds a new line.
        // Patterns will be validated, i.e. their shape will be checked.
        .pattern("XXX")
        .pattern(" # ")
        .pattern(" # ")
        // Create the keys for the pattern. All non-space characters used in the pattern must be defined.
        // This can either accept Ingredients, TagKey<Item>s or ItemLikes, i.e. items or blocks.
        .define('X', Items.IRON_INGOT)
        .define('#', Items.STICK)
        // Creates the recipe advancement. While not mandated by the consuming background systems,
        // the recipe builder will crash if you omit this. The first parameter is the advancement name,
        // and the second one is the condition. Normally, you want to use the has() shortcut for the condition.
        // Multiple advancement requirements can be added by calling #unlockedBy multiple times.
        .unlockedBy("has_iron_ingot", has(Items.IRON_INGOT))
        // Stores the recipe in the passed RecipeOutput, to be written to disk.
        // If you want to add conditions to the recipe, those can be set on the output.
        .save(output);

还可以调用 #group 设置配方书分组。

无序合成

与有序配方不同,无序配方不关心原料的摆放顺序。因此没有 pattern 和 key,只有原料列表:

{
    "type": "minecraft:crafting_shapeless",
    "category": "misc",
    "ingredients": [
        {
            "item": "minecraft:brown_mushroom"
        },
        {
            "item": "minecraft:red_mushroom"
        },
        {
            "item": "minecraft:bowl"
        }
    ],
    "result": {
        "count": 1,
        "id": "minecraft:mushroom_stew"
    }
}

同样逐行解释:

  • type:无序配方序列化器 ID,即 minecraft:crafting_shapeless。
  • category:可选字段,定义配方书中的分类。
  • ingredients:原料列表。代码会保留列表顺序以供配方展示使用,但配方本身接受任意顺序的原料。
  • result:配方结果,采用物品堆叠的 JSON 表示。
  • 示例中未展示 group 键。这个可选字符串属性用于在配方书中创建分组;同组配方会合并显示。

下面是生成该配方的方法:

// We use a builder pattern, therefore no variable is created. Create a new builder by calling
// ShapelessRecipeBuilder#shapeless with the recipe category (found in the RecipeCategory enum)
// and a result item, a result item and count, or a result item stack.
ShapelessRecipeBuilder.shapeless(RecipeCategory.MISC, Items.MUSHROOM_STEW)
        // Add the recipe ingredients. This can either accept Ingredients, TagKey<Item>s or ItemLikes.
        // Overloads also exist that additionally accept a count, adding the same ingredient multiple times.
        .requires(Blocks.BROWN_MUSHROOM)
        .requires(Blocks.RED_MUSHROOM)
        .requires(Items.BOWL)
        // Creates the recipe advancement. While not mandated by the consuming background systems,
        // the recipe builder will crash if you omit this. The first parameter is the advancement name,
        // and the second one is the condition. Normally, you want to use the has() shortcut for the condition.
        // Multiple advancement requirements can be added by calling #unlockedBy multiple times.
        .unlockedBy("has_mushroom_stew", has(Items.MUSHROOM_STEW))
        .unlockedBy("has_bowl", has(Items.BOWL))
        .unlockedBy("has_brown_mushroom", has(Blocks.BROWN_MUSHROOM))
        .unlockedBy("has_red_mushroom", has(Blocks.RED_MUSHROOM))
        // Stores the recipe in the passed RecipeOutput, to be written to disk.
        // If you want to add conditions to the recipe, those can be set on the output.
        .save(output);

还可以调用 #group 设置配方书分组。

说明 只有一种输入物品的配方(例如拆解储存方块)应使用无序配方,以遵循原版惯例。

特殊合成

某些情况下,需要根据输入动态创建输出。多数情况是从输入堆叠复制或计算数据组件值,再将其设置到输出上。这些配方通常只指定类型,其他内容都硬编码。例如:

{
    "type": "minecraft:crafting_special_armordye"
}

这个皮革盔甲染色配方只指定类型,其余都硬编码,尤其是难以用 JSON 表达的颜色计算。Minecraft 为所有特殊合成配方使用 crafting_special_ 前缀,但无需强制遵循。

该配方的数据生成如下:

// The parameter of #special is a Function<CraftingBookCategory, Recipe<?>>.
// All vanilla special recipes use a constructor with one CraftingBookCategory parameter for this.
SpecialRecipeBuilder.special(ArmorDyeRecipe::new)
        // This overload of #save allows us to specify a name. It can also be used on shaped or shapeless builders.
        .save(output, "armor_dye");

原版提供以下特殊合成序列化器(模组可以添加更多):

  • minecraft:crafting_special_armordye:为皮革盔甲及其他可染色物品染色。
  • minecraft:crafting_special_bannerduplicate:复制旗帜。
  • minecraft:crafting_special_bookcloning:复制成书,使结果书的 generation 属性增加一。
  • minecraft:crafting_special_firework_rocket:合成烟花火箭。
  • minecraft:crafting_special_firework_star:合成烟火之星。
  • minecraft:crafting_special_firework_star_fade:为烟火之星添加淡出颜色。
  • minecraft:crafting_special_mapcloning:复制已有内容的地图,也适用于藏宝图。
  • minecraft:crafting_special_mapextending:扩展已有内容的地图。
  • minecraft:crafting_special_repairitem:将两个损坏物品修复为一个。
  • minecraft:crafting_special_shielddecoration:将旗帜应用到盾牌上。
  • minecraft:crafting_special_shulkerboxcoloring:为潜影盒染色,并保留内容物。
  • minecraft:crafting_special_suspiciousstew:根据输入花朵合成谜之炖菜。
  • minecraft:crafting_special_tippedarrow:根据输入药水合成药箭。
  • minecraft:crafting_decorated_pot:使用陶片合成饰纹陶罐。

熔炉类配方

第二类重要配方是通过熔炼或类似过程完成的配方。熔炉(类型 minecraft:smelting)、烟熏炉(minecraft:smoking)、高炉(minecraft:blasting)和营火(minecraft:campfire_cooking)使用相同格式:

{
    "type": "minecraft:smelting",
    "category": "food",
    "cookingtime": 200,
    "experience": 0.1,
    "ingredient": {
        "item": "minecraft:kelp"
    },
    "result": {
        "id": "minecraft:dried_kelp"
    }
}

逐行解释如下:

  • type:配方序列化器 ID,此处为 minecraft:smelting,会根据熔炉类配方的种类而变化。
  • category:可选字段,定义配方书中的分类。
  • cookingtime:配方处理所需游戏刻数。原版熔炉配方均为 200,烟熏炉和高炉为 100,营火为 600。不过,可以使用任何需要的值。
  • experience:完成配方奖励的经验量。此字段可选,省略时不奖励经验。
  • ingredient:配方输入原料。
  • result:配方结果,采用物品堆叠的 JSON 表示。

这些配方的数据生成如下:

// Use #smoking for smoking recipes, #blasting for blasting recipes, and #campfireCooking for campfire recipes.
// All of these builders work the same otherwise.
SimpleCookingRecipeBuilder.smelting(
        // Our input ingredient.
        Ingredient.of(Items.KELP),
        // Our recipe category.
        RecipeCategory.FOOD,
        // Our result item. May also be an ItemStack.
        Items.DRIED_KELP,
        // Our experience reward
        0.1f,
        // Our cooking time.
        200
)
        // The recipe advancement, like with the crafting recipes above.
        .unlockedBy("has_kelp", has(Blocks.KELP))
        // This overload of #save allows us to specify a name.
        .save(p_301191_, "dried_kelp_smelting");

说明 这些配方的配方类型与其序列化器相同,即熔炉使用 minecraft:smelting,烟熏炉使用 minecraft:smoking,以此类推。

切石

切石机配方使用 minecraft:stonecutting 配方类型。其结构很简单,只有类型、输入和输出:

{
    "type": "minecraft:stonecutting",
    "ingredient": {
        "item": "minecraft:andesite"
    },
    "result": {
        "count": 2,
        "id": "minecraft:andesite_slab"
    }
}

type 定义配方序列化器(minecraft:stonecutting)。ingredient 是原料,result 是基本的物品堆叠 JSON。与合成配方一样,也可以指定可选的 group,用于配方书分组。

数据生成同样简单:

SingleItemRecipeBuilder.stonecutting(Ingredient.of(Items.ANDESITE), RecipeCategory.BUILDING_BLOCKS, Items.ANDESITE_SLAB, 2)
        .unlockedBy("has_andesite", has(Items.ANDESITE))
        .save(output, "andesite_slab_from_andesite_stonecutting");

注意,单物品配方构建器不支持实际的 ItemStack 结果,因此也不支持带数据组件的结果。不过,配方 codec 支持这种结果;需要该功能时,必须实现自定义构建器。

锻造

锻造台支持两种配方序列化器:一种将输入转换为输出,并复制输入的组件(例如附魔);另一种向输入应用组件。两者都使用 minecraft:smithing 配方类型,都需要三种输入,分别称为基础物品、模板和附加物品。

转换锻造

此序列化器将两个输入物品转换成一个,保留第一个输入的数据组件。原版主要用于下界合金装备,不过这里可以使用任意物品:

{
    "type": "minecraft:smithing_transform",
    "base": {
        "item": "minecraft:diamond_axe"
    },
    "template": {
        "item": "minecraft:netherite_upgrade_smithing_template"
    },
    "addition": {
        "item": "minecraft:netherite_ingot"
    },
    "result": {
        "count": 1,
        "id": "minecraft:netherite_axe"
    }
}

逐行解释如下:

  • type:配方序列化器 ID,即 minecraft:smithing_transform。
  • base:基础原料,通常是某件装备。
  • template:模板原料,通常是锻造模板。
  • addition:附加原料,通常是某种材料,例如下界合金锭。
  • result:配方结果,采用物品堆叠的 JSON 表示。

数据生成时,调用 SmithingTransformRecipeBuilder#smithing 添加配方:

SmithingTransformRecipeBuilder.smithing(
        // The template ingredient.
        Ingredient.of(Items.NETHERITE_UPGRADE_SMITHING_TEMPLATE),
        // The base ingredient.
        Ingredient.of(Items.DIAMOND_AXE),
        // The addition ingredient.
        Ingredient.of(Items.NETHERITE_INGOT),
        // The recipe book category.
        RecipeCategory.TOOLS,
        // The result item. Note that while the recipe codec accepts an item stack here, the builder does not.
        // If you need an item stack output, you need to use your own builder.
        Items.NETHERITE_AXE
)
        // The recipe advancement, like with the other recipes above.
        .unlocks("has_netherite_ingot", has(Items.NETHERITE_INGOT))
        // This overload of #save allows us to specify a name.
        .save(output, "netherite_axe_smithing");

纹饰锻造

纹饰锻造是向盔甲应用盔甲纹饰的过程:

{
    "type": "minecraft:smithing_trim",
    "addition": {
        "tag": "minecraft:trim_materials"
    },
    "base": {
        "tag": "minecraft:trimmable_armor"
    },
    "template": {
        "item": "minecraft:bolt_armor_trim_smithing_template"
    }
}

同样逐项解释:

  • type:配方序列化器 ID,即 minecraft:smithing_trim。
  • base:基础原料。原版所有用例均使用 minecraft:trimmable_armor 标签。
  • template:模板原料。原版所有用例均使用盔甲纹饰锻造模板。
  • addition:附加原料。原版所有用例均使用 minecraft:trim_materials 标签。

此序列化器没有结果字段,因为它使用基础输入,并将模板和附加物品“应用”其上:根据其他输入设置基础物品的组件,再将操作结果作为配方结果。

数据生成时,调用 SmithingTrimRecipeBuilder#smithingTrim 添加配方:

SmithingTrimRecipeBuilder.smithingTrim(
        // The base ingredient.
        Ingredient.of(ItemTags.TRIMMABLE_ARMOR),
        // The template ingredient.
        Ingredient.of(Items.BOLT_ARMOR_TRIM_SMITHING_TEMPLATE),
        // The addition ingredient.
        Ingredient.of(ItemTags.TRIM_MATERIALS),
        // The recipe book category.
        RecipeCategory.MISC
)
        // The recipe advancement, like with the other recipes above.
        .unlocks("has_smithing_trim_template", has(Items.BOLT_ARMOR_TRIM_SMITHING_TEMPLATE))
        // This overload of #save allows us to specify a name. Yes, this name is copied from vanilla.
        .save(output, "bolt_armor_trim_smithing_template_smithing_trim");

来源: NeoForged/Documentation(1.21.1)。中文翻译依据提交 4a680cdd4d15。