[TOC]
---
由於犀牛引擎不支持JavaScript的class對象,所以只能使用function的protoype來建立一個類似於class的function,具體解析如下:
配方Json樣本:
```json
{
"type": "ae2:inscriber",
"mode": "inscribe",
"result": {
"item": "ae2:printed_calculation_processor"
},
"ingredients": {
"top": {
"item": "lazierae2:universal_press"
},
"middle": {
"item": "ae2:certus_quartz_crystal"
}
}
}
```
代碼:
```js=
//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 => {
new ae2Inscriber() //在recipes事件內調用builder
.setResult(Item.of('ae2:logic_processor').toJson()) //設置輸出,參數為Ingredient,支持修改數量、NBT等,詳見其他教學,下同
.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'); //調用替代方法,替代原有的配方
});
```
---
{%hackmd @lumynou5/dark-theme %}