clear_craft: Return false if recipe not found, don't throw error (#7804)
authorParamat <paramat@users.noreply.github.com>
Wed, 24 Oct 2018 14:03:21 +0000 (15:03 +0100)
committerGitHub <noreply@github.com>
Wed, 24 Oct 2018 14:03:21 +0000 (15:03 +0100)
doc/lua_api.txt
src/script/lua_api/l_craft.cpp

index b079ac82261e6be3eb010cca7e76a63d89b37872..53f27e20c08c15a8ab01c39a639b4562473e3872 100644 (file)
@@ -3555,7 +3555,7 @@ Call these functions only at load time!
       ignored. For input use the same recipe table syntax as for
       `minetest.register_craft(recipe)`. For output specify only the item,
       without a quantity.
-    * If no erase candidate could be found, Lua exception will be thrown.
+    * Returns false if no erase candidate could be found, otherwise returns true.
     * **Warning**! The type field ("shaped", "cooking" or any other) will be
       ignored if the recipe contains output. Erasing is then done independently
       from the crafting method.
index 64177109e67e2c6286d3592947ba87dd35c9b6b1..0899b945e99980201d10fcdbdfc836e54fb360c7 100644 (file)
@@ -294,11 +294,14 @@ int ModApiCraft::l_clear_craft(lua_State *L)
        std::string type = getstringfield_default(L, table, "type", "shaped");
        CraftOutput c_output(output, 0);
        if (!output.empty()) {
-               if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L)))
-                       return 0;
+               if (craftdef->clearCraftRecipesByOutput(c_output, getServer(L))) {
+                       lua_pushboolean(L, true);
+                       return 1;
+               }
 
-               throw LuaError("No craft recipe known for output"
-                               " (output=\"" + output + "\")");
+               warningstream << "No craft recipe known for output" << std::endl;
+               lua_pushboolean(L, false);
+               return 1;
        }
        std::vector<std::string> recipe;
        int width = 0;
@@ -347,10 +350,15 @@ int ModApiCraft::l_clear_craft(lua_State *L)
        } 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;
+
+       if (!craftdef->clearCraftRecipesByInput(method, width, recipe, getServer(L))) {
+               warningstream << "No craft recipe matches input" << std::endl;
+               lua_pushboolean(L, false);
+               return 1;
+       }
+
+       lua_pushboolean(L, true);
+       return 1;
 }
 
 // get_craft_result(input)