由于犀牛引擎不支持JavaScript的class对象,所以只能使用function的protype来建立一个类似于class的function,具体解析如下:
配方Json样本:
{
"type": "ae2:inscriber",
"mode": "inscribe",
"result": {
"item": "ae2:printed_calculation_processor"
},
"ingredients": {
"top": {
"item": "lazierae2:universal_press"
},
"middle": {
"item": "ae2:certus_quartz_crystal"
}
}
}
代码:
//AE2的压印机
function ae2Inscriber() {
//初始化配方主体
this.recipe = {
type: 'ae2:inscriber',
mode: 'press',
result: {},
ingredients: {}
}
}
//定义方法,注意每个方法除了build和replace方法为最终创建方法外,都需要return本身以方便多次调用方法来设置属性
ae2Inscriber.prototype = {
//更改配方模式
setMode: function (mode) {
this.recipe.mode = mode;
return this;
},
//更改输出物品
setResult: function (result) {
this.recipe.result = result;
return this;
},
//更改顶部输入,格式为Ingredient
setIngredientsTop: function (ingredientsTop) {
this.recipe.ingredients.top = ingredientsTop;
return this;
},
//更改中间输入,格式为Ingredient
setIngredientsMiddle: function (ingredientsMiddle) {
this.recipe.ingredients.middle = ingredientsMiddle;
return this;
},
//更改底部输入,格式为Ingredient
setIngredientsBottom: function (ingredientsBottom) {
this.recipe.ingredients.bottom = ingredientsBottom;
return this;
},
//如果是shapeless配方怎么办?即输入的Ingredients为一个数组,这里假设压印器的ingredients是一个数组
//addIngredients: function (Ingredients) {
//this.recipe.ingredients.push(Ingredients);
// return this;
//},
//Over
//创建配方,需要传入recipe事件和配方名称,自动生成配方的ResourceLocation,namespace为预先设置的常量,或者直接改为你需要的字符串即可
//可以取消配方名称设置但是下面的replace方法也要去掉该变量的传入
build: function (event, recipename) {
event.custom(this.recipe).id(namespace + ':inscriber/' + recipename);
return;
},
//替代配方,先根据配方id删除配方,然后调用build创建配方
replaceRecipe: function (event, recipeid) {
event.remove({ id: recipeid });
let recipename = recipeid.split(':')[1].split('/').pop();
this.build(event, recipename);
}
};
//上面一个压印器的builder定义就完成了,以下是调用
onEvent('recipes', event => {
//在recipes事件内调用builder
new ae2Inscriber()
//设置输出,参数为Ingredient,支持修改数量、NBT等,详见其他教程,下同
.setResult(Item.of('ae2:logic_processor').toJson())
//设置压印模式为压印
.setMode('press')
//设置顶部输入为逻辑电路板
.setIngredientsTop(Item.of('ae2:printed_logic_processor').toJson())
//设置中间输入为标签:铜锭
.setIngredientsMiddle(Ingredient.of('#forge:ingots/copper').toJson())
//设置底部输入为硅板
.setIngredientsBottom(Item.of('ae2:printed_silicon').toJson())
//调用替代方法,替代原有的配方
.replaceRecipe(event, 'ae2:inscriber/logic_processor');
});