Recipes¶
原文:Recipes
提示 本页面仍在开发中。它并不完整,且随时可能更改。 > 提示 配方事件是服务端事件。
目录¶
- 配方与回调的工作原理
- 添加配方
- 有序合成
- 无序合成
- 锻造配方
- 烧炼与其他烹饪
- 切石
- 自定义(JSON)
- 移除配方
- 修改与替换配方
- 辅助函数、效率与高级原料(也就是「如何避免重复劳动」)
配方、回调与你 ↑¶
配方事件可用于添加、移除或替换配方。
任何修改配方的脚本都应放在 kubejs/server_scripts 文件夹中,并可在任何时候用 /reload 重载。
对配方的任何修改都应在 recipes 事件的上下文中完成。这意味着我们需要为 ServerEvents.recipes 事件注册一个「事件监听器」,并给它一些代码,让它在游戏准备修改配方时执行。下面是我们告诉 KubeJS 在「配方时刻」执行某些代码的方法:
/*
* ServerEvents.recipes() is a function that accepts another function,
* called the "callback", as a parameter. The callback gets run when the
* server is working on recipes, and then we can make our own changes.
* When the callback runs, it is also known as the event "firing".
*/
ServerEvents.recipes(event => { //listen for the "recipes" server event.
// You can replace `event` with any name you like, as
// long as you change it inside the callback too!
// This part, inside the curly braces, is the callback.
// You can modify as many recipes as you like in here,
// without needing to use ServerEvents.recipes() again.
console.log('Hello! The recipe event has fired!')
})
在接下来的章节中,你可以看到回调里该放什么。
添加配方 ↑¶
¶
以下所有代码都应放在你的配方回调_内部_。
有序合成 ↑¶
有序合成配方使用 event.shaped() 方法添加。有序合成配方的原料必须按特定的顺序和形状排列,才能与玩家的输入匹配。event.shaped() 的参数为:
-
产物物品,数量可为 1-64
-
一个表示工作台行数的数组(最大长度 3),以字符串表示(最大长度 3)。空格代表没有物品的槽位,字母代表物品。字母本身不必有意义;你在下一个参数中说明它们代表什么。
-
一个将字母映射到物品的对象,如
{letter: item}。输入物品的数量必须为 1。
如果你想强制合成格中的严格位置或禁用镜像,请参阅自定义配方的方法。
event.shaped('3x minecraft:stone', [// arg 1: output
'A B',
' C ', // arg 2: the shape (array of strings)
'B A'
], {
A: 'minecraft:andesite',
B: 'minecraft:diorite', //arg 3: the mapping object
C: 'minecraft:granite'
}
)
无序合成 ↑¶
无序合成配方使用 event.shapeless() 方法添加。玩家可以把无序合成配方的原料放在合成格的任意位置,仍然可以合成。event.shapeless() 的参数为:
- 产物物品
-
输入物品数组。所有输入物品的总数量最多为 9。
event.shapeless('3x minecraft:dandelion', [ // arg 1: output 'minecraft:bone_meal', 'minecraft:yellow_dye', //arg 2: the array of inputs '3x minecraft:ender_pearl' ])
锻造配方 ↑¶
锻造配方有 2 个输入和 1 个产物,使用 event.smithing() 方法添加。锻造配方在锻造台(Smithing Table)中合成。
event.smithing(
'minecraft:netherite', // arg 1: output
'minecraft:iron_ingot', // arg 2: the item to be upgraded
'minecraft:black_dye' // arg 3: the upgrade item
)
烧炼与烹饪 ↑¶
烹饪配方都非常相似:接受一个输入(单个物品),产出一个输出(最多 64 个相同物品)。燃料无法在此配方事件中更改,应改用标签来实现。
- 烧炼配方使用
event.smelting()添加,需要普通熔炉(Furnace)。 - 高炉烧炼配方使用
event.blasting()添加,需要高炉(Blast Furnace)。 - 烟熏配方使用
event.smoking()添加,需要烟熏炉(Smoker)。 -
营火烹饪配方使用
event.campfireCooking()添加,需要营火(Campfire)。// Cook 1 stone into 3 gravel in a Furnace: event.smelting('3x minecraft:gravel', 'minecraft:stone')
// Blast 1 iron ingot into 10 nuggets in a Blast Furnace: event.blasting('10x minecraft:iron_nugget', 'minecraft:iron_ingot')
// Smoke glass into tinted glass in the Smoker: event.smoking('minecraft:tinted_glass', 'minecraft:glass')
// Burn sticks into torches on the Campfire: event.campfireCooking('minecraft:torch', 'minecraft:stick')
切石 ↑¶
与烹饪配方类似,切石配方非常简单:一个输入(单个物品)和一个输出(最多 64 个相同物品)。它们使用 event.stonecutting() 方法添加。
//allow cutting 3 sticks from any plank on the stonecutter
event.stonecutting('3x minecraft:stick', '#minecraft:planks')
自定义/模组 JSON 配方 ↑¶
如果模组支持数据包配方,你无需任何附加模组支持即可为其添加配方!很遗憾,我们无法给出具体建议,因为每个模组的布局都不一样,但如果模组有 GitHub(大多数都有!)或其他源代码,你可以在 /src/generated/resources/data/<modname>/recipes/ 中找到相关的 JSON 文件。否则,你也许可以通过解压模组的 .jar 文件找到它们。
下面是一个添加农夫乐事(Farmer's Delight)砧板配方的示例,其中的输入、输出和工具都直接取自他们的 GitHub。显然,你可以替换这里的任何物品来制作自己的配方!
// Slice cake on a cutting board!
event.custom({
type: 'farmersdelight:cutting',
ingredients: [
{ item: 'minecraft:cake' }
],
tool: { tag: 'forge:tools/knives' },
result: [
{ item: 'farmersdelight:cake_slice', count: 7 }
]
})
这是另一个 event.custom() 的示例,用于制作匠魂(Tinkers' Construct)合金配方,其中输入、输出和温度都直接取自他们的 GitHub(已移除条件):
// Adding the Molten Electrum alloying recipe from Tinkers' Construct
event.custom({
type: 'tconstruct:alloy',
inputs: [
{ tag: 'forge:molten_gold', amount: 90 },
{ tag: 'forge:molten_silver', amount: 90 }
],
result: { fluid: 'tconstruct:molten_electrum', amount: 180 },
temperature: 760
})
移除配方 ↑¶
¶
可以使用 event.remove() 方法移除配方。它接受 1 个参数:配方过滤器。过滤器是一组决定选择哪些配方的属性。你可以用许多条件来选择配方:
-
按产物物品
{output: '<item_id>'} -
按输入物品
{input: '<item_id>'} -
按模组
{mod: '<mod_id>'} -
按唯一配方 ID
{id: '<recipe_id>'} -
上述条件的组合:
- 要求满足所有条件:
{condition1: 'value', condition2: 'value2'} - 要求满足任一条件:
[{condition_a: 'true'}, {condition_b: 'true'}] - 要求不满足该条件:
{not: {condition: 'requirement'}}
- 要求满足所有条件:
以下所有内容都可以照常放在你的配方回调中:
// A blank condition removes all recipes (obviously not recommended):
event.remove({})
// Remove all recipes where output is stone pickaxe:
event.remove({ output: 'minecraft:stone_pickaxe' })
// Remove all recipes where output has the Wool tag:
event.remove({ output: '#minecraft:wool' })
// Remove all recipes where any input has the Redstone Dust tag:
event.remove({ input: '#forge:dusts/redstone' })
// Remove all recipes from Farmer's Delight:
event.remove({ mod: 'farmersdelight' })
// Remove all campfire cooking recipes:
event.remove({ type: 'minecraft:campfire_cooking' })
// Remove all recipes that grant stone EXCEPT smelting recipes:
event.remove({ not: { type: 'minecraft:smelting' }, output: 'stone' })
// Remove recipes that output cooked chicken AND are cooked on a campfire:
event.remove({ output: 'minecraft:cooked_chicken', type: 'minecraft:campfire_cooking' })
// Remove any blasting OR smelting recipes that output minecraft:iron_ingot:
event.remove([{ type: 'minecraft:smelting', output: 'minecraft:iron_ingot' }, { type: 'minecraft:blasting', output: 'minecraft:iron_ingot' }])
// Remove a recipe by ID. in this case, data/minecraft/recipes/glowstone.json:
// Note: Recipe ID and output are different!
event.remove({ id: 'minecraft:glowstone' })
要查找配方的唯一 ID,请使用 F3+H 键打开高级提示文本(你会在聊天中看到提示),然后悬停在 [+] 符号(如果使用 REI)或产物(如果使用 JEI)上。
修改与替换配方 ↑¶
你可以使用 event.replaceInput() 和 event.replaceOutput() 批量修改受支持的配方。它们各接受 3 个参数:
- 一个过滤器,与移除配方时使用的类似
- 要替换的原料
- 用于替换的原料(可以是标签)
例如,假设你想移除所有木棍,并让人们改用树苗来合成物品。你可以在回调中放入:
event.replaceInput(
{ input: 'minecraft:stick' }, // Arg 1: the filter
'minecraft:stick', // Arg 2: the item to replace
'#minecraft:saplings' // Arg 3: the item to replace it with
// Note: tagged fluid ingredients do not work on Fabric, but tagged items do.
)
高级技巧 ↑¶
辅助函数**↑¶
你在制作大量相似的配方吗?觉得自己在反复输入同样的内容?试试辅助函数吧!辅助函数只接收相关的部分,然后替你完成重复的工作,用更少的代码完成重复操作!
下面是一个辅助函数,它允许你通过在指定物品周围合成一个花盆来制作物品。
ServerEvents.recipes(event => {
let potting = (output, pottedInput) => {
event.shaped(output, [
'BIB',
' B '
], {
B: 'minecraft:brick',
I: pottedInput
})
}
//Now we can make many 'potting' recipes without typing that huge block over and over!
potting('kubejs:potted_snowball', 'minecraft:snowball')
potting('kubejs:potted_lava', 'minecraft:lava_bucket')
potting('minecraft:blast_furnace', 'minecraft:furnace')
})
循环 **↑¶
除了辅助函数,你还可以遍历数组,对数组中的每个物品执行操作。