From 281e9f39fdffd106e79122ae23e2b5443a5daea3 Mon Sep 17 00:00:00 2001 From: Foghrye4 Date: Mon, 25 Apr 2016 18:50:10 +0300 Subject: Adding minetest.clear_craft Modifications by est31: grammar fixes in doc + error messages and a little style fix, no functional change. --- src/script/lua_api/l_craft.cpp | 76 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) (limited to 'src/script/lua_api/l_craft.cpp') diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index 391a0133d..d135c689f 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -34,7 +34,6 @@ struct EnumString ModApiCraft::es_CraftMethod[] = {0, NULL}, }; - // helper for register_craft bool ModApiCraft::readCraftRecipeShaped(lua_State *L, int index, int &width, std::vector &recipe) @@ -281,6 +280,80 @@ int ModApiCraft::l_register_craft(lua_State *L) return 0; /* number of results */ } +// clear_craft({[output=item], [recipe={{item00,item10},{item01,item11}}]) +int ModApiCraft::l_clear_craft(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + luaL_checktype(L, 1, LUA_TTABLE); + int table = 1; + + // Get the writable craft definition manager from the server + IWritableCraftDefManager *craftdef = + getServer(L)->getWritableCraftDefManager(); + + std::string output = getstringfield_default(L, table, "output", ""); + std::string type = getstringfield_default(L, table, "type", "shaped"); + CraftOutput c_output(output, 0); + if (output != "") { + if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L))) + return 0; + else + throw LuaError("No craft recipe known for output" + " (output=\"" + output + "\")"); + } + std::vector recipe; + int width = 0; + CraftMethod method = CRAFT_METHOD_NORMAL; + /* + CraftDefinitionShaped + */ + if (type == "shaped") { + lua_getfield(L, table, "recipe"); + if (lua_isnil(L, -1)) + throw LuaError("Either output or recipe has to be defined"); + if (!readCraftRecipeShaped(L, -1, width, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionShapeless + */ + else if (type == "shapeless") { + lua_getfield(L, table, "recipe"); + if (lua_isnil(L, -1)) + throw LuaError("Either output or recipe has to be defined"); + if (!readCraftRecipeShapeless(L, -1, recipe)) + throw LuaError("Invalid crafting recipe"); + } + /* + CraftDefinitionCooking + */ + else if (type == "cooking") { + method = CRAFT_METHOD_COOKING; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if (rec == "") + throw LuaError("Crafting definition (cooking)" + " is missing a recipe"); + recipe.push_back(rec); + } + /* + CraftDefinitionFuel + */ + else if (type == "fuel") { + method = CRAFT_METHOD_FUEL; + std::string rec = getstringfield_default(L, table, "recipe", ""); + if (rec == "") + throw LuaError("Crafting definition (fuel)" + " is missing a recipe"); + recipe.push_back(rec); + } else { + throw LuaError("Unknown crafting definition type: \"" + type + "\""); + } + if (!craftdef->clearCraftRecipesByInput(method, width, recipe, getServer(L))) + throw LuaError("No crafting specified for input"); + lua_pop(L, 1); + return 0; +} + // get_craft_result(input) int ModApiCraft::l_get_craft_result(lua_State *L) { @@ -431,4 +504,5 @@ void ModApiCraft::Initialize(lua_State *L, int top) API_FCT(get_craft_recipe); API_FCT(get_craft_result); API_FCT(register_craft); + API_FCT(clear_craft); } -- cgit v1.2.3 From 5c0e659516857733d968707490b16bcf62f33c92 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Sat, 30 Jul 2016 12:50:22 +0200 Subject: Script API: Make the craft recipe field 'method' consistent --- src/script/lua_api/l_craft.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/script/lua_api/l_craft.cpp') diff --git a/src/script/lua_api/l_craft.cpp b/src/script/lua_api/l_craft.cpp index d135c689f..2236566de 100644 --- a/src/script/lua_api/l_craft.cpp +++ b/src/script/lua_api/l_craft.cpp @@ -422,20 +422,28 @@ static void push_craft_recipe(lua_State *L, IGameDef *gdef, } lua_setfield(L, -2, "items"); setintfield(L, -1, "width", input.width); + + std::string method_s; switch (input.method) { case CRAFT_METHOD_NORMAL: - lua_pushstring(L, "normal"); + method_s = "normal"; break; case CRAFT_METHOD_COOKING: - lua_pushstring(L, "cooking"); + method_s = "cooking"; break; case CRAFT_METHOD_FUEL: - lua_pushstring(L, "fuel"); + method_s = "fuel"; break; default: - lua_pushstring(L, "unknown"); + method_s = "unknown"; } + lua_pushstring(L, method_s.c_str()); + lua_setfield(L, -2, "method"); + + // Deprecated, only for compatibility's sake + lua_pushstring(L, method_s.c_str()); lua_setfield(L, -2, "type"); + lua_pushstring(L, output.item.c_str()); lua_setfield(L, -2, "output"); } -- cgit v1.2.3