From 59b9e82cc6f0d66cf1d1ad7703fbb67fa6e76b19 Mon Sep 17 00:00:00 2001 From: KobeBryant114514 <116721335+KobeBryant114514@users.noreply.github.com> Date: Sun, 22 Sep 2024 09:11:07 +0800 Subject: [PATCH] feat: add custom recipe --- lib/GMLIB_API-JS.d.ts | 22 ++++++++++++++++++++++ lib/GMLIB_API-JS.js | 27 +++++++++++++++++++++++++++ src/CompatibilityApi.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) diff --git a/lib/GMLIB_API-JS.d.ts b/lib/GMLIB_API-JS.d.ts index 09c26f1..4d6982e 100644 --- a/lib/GMLIB_API-JS.d.ts +++ b/lib/GMLIB_API-JS.d.ts @@ -509,6 +509,28 @@ declare class Recipes { /** 解锁条件(也可以填物品命名空间ID) */ unlock: "AlwaysUnlocked" | "PlayerHasManyItems" | "PlayerInWater" | "None" | undefined ): boolean; + + /** 注册自定义有序合成表 */ + static registerCustomShapedRecipe( + /** 合成表唯一标识符 */ + recipeId: string, + /** 合成表摆放方式,数组元素为字符串 */ + shape: [string, string, string], + /** 材料数组 */ + ingredients: string[], + /** 合成结果 */ + result: Item + ): boolean; + + /** 注册自定义无序合成表 */ + static registerCustomShapelessRecipe( + /** 合成表唯一标识符 */ + recipeId: string, + /** 合成材料 */ + ingredients: string[], + /** 合成结果 */ + result: Item + ): boolean; } /** 实验性功能类 */ diff --git a/lib/GMLIB_API-JS.js b/lib/GMLIB_API-JS.js index 9a977e6..dd464f7 100644 --- a/lib/GMLIB_API-JS.js +++ b/lib/GMLIB_API-JS.js @@ -96,6 +96,10 @@ const GMLIB_API = { registerShapedRecipe: ll.import("GMLib_ModAPI", "registerShapedRecipe"), /** 注册无序合成表 @type {function(string,Array.,Array.,string,number,string)} */ registerShapelessRecipe: ll.import("GMLib_ModAPI", "registerShapelessRecipe"), + /** 注册有序合成表 @type {function(string,Array.,Array.,string,number,string)} */ + registerCustomShapedRecipe: ll.import("GMLIB_API", "registerShapedRecipe"), + /** 注册无序合成表 @type {function(string,Array.,Array.,string,number,string)} */ + registerCustomShapelessRecipe: ll.import("GMLIB_API", "registerShapelessRecipe"), /** 检测LRCA版本是否大于或等于此版本 @type {function(number,number,number):boolean} */ isVersionMatched: ll.import("GMLIB_API", "isVersionMatched"), /** 获取LRCA版本 @type {function():string} */ @@ -1194,6 +1198,18 @@ class Recipes { return GMLIB_API.registerShapedRecipe(recipeId, shape, ingredients, result, count, unlock); } + /** + * 注册自定义有序合成表 + * @param {string} recipeId 合成表唯一标识 + * @param {[string,string,string]} shape 合成表摆放方式,数组元素为字符串 + * @param {Array.} ingredients 材料数组 + * @param {Item} result 合成结果 + * @returns {boolean} 是否注册成功 + */ + static registerCustomShapedRecipe(recipeId, shape, ingredients, result) { + return GMLIB_API.registerCustomShapedRecipe(recipeId, shape, ingredients, result); + } + /** * 注册无序合成表 * @param {string} recipeId 合成表唯一标识 @@ -1206,6 +1222,17 @@ class Recipes { static registerShapelessRecipe(recipeId, ingredients, result, count = 1, unlock = "AlwaysUnlocked") { return GMLIB_API.registerShapelessRecipe(recipeId, ingredients, result, count, unlock); } + + /** + * 注册自定义无序合成表 + * @param {string} recipeId 合成表唯一标识 + * @param {Array.} ingredients 合成材料 + * @param {Item} result 合成结果 + * @returns {boolean} 是否注册成功 + */ + static registerCustomShapelessRecipe(recipeId, ingredients, result) { + return GMLIB_API.registerCustomShapelessRecipe(recipeId, ingredients, result); + } } /** 实验性功能类 */ diff --git a/src/CompatibilityApi.cpp b/src/CompatibilityApi.cpp index e9518dc..7e213c1 100644 --- a/src/CompatibilityApi.cpp +++ b/src/CompatibilityApi.cpp @@ -958,4 +958,43 @@ void Export_Compatibility_API() { level->setDefaultGameType((GameType)gameMode); } }); + RemoteCall::exportAs( + "GMLIB_API", + "registerCustomShapelessRecipe", + [](std::string const& recipe_id, std::vector ingredients, ItemStack* result) -> void { + auto level = GMLIB_Level::getInstance(); + if (!level) { + return; + } + std::vector types; + char rt = 'A'; + for (auto& ing : ingredients) { + auto key = Recipes::Type(ing, rt, 1, 0); + types.push_back(key); + rt++; + } + CustomRecipe::registerShapelessCraftingTableRecipe(recipe_id, types, *result); + } + ); + RemoteCall::exportAs( + "GMLIB_API", + "registerCustomShapedRecipe", + [](std::string const& recipe_id, + std::vector shape, + std::vector ingredients, + ItemStack* result) -> void { + auto level = GMLIB_Level::getInstance(); + if (!level) { + return; + } + std::vector types; + char rt = 'A'; + for (auto& ing : ingredients) { + auto key = Recipes::Type(ing, rt, 1, 0); + types.push_back(key); + rt++; + } + CustomRecipe::registerShapedCraftingTableRecipe(recipe_id, shape, types, *result); + } + ); } \ No newline at end of file