// key at index -2 and value at index -1
if(!lua_isstring(L, -1))
return false;
- recipe.push_back(lua_tostring(L, -1));
+ recipe.emplace_back(readParam<std::string>(L, -1));
// removes value, keeps key for next iteration
lua_pop(L, 1);
colcount++;
// key at index -2 and value at index -1
if(!lua_isstring(L, -1))
return false;
- recipe.push_back(lua_tostring(L, -1));
+ recipe.emplace_back(readParam<std::string>(L, -1));
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
lua_rawgeti(L, -1, 1);
if(!lua_isstring(L, -1))
return false;
- std::string replace_from = lua_tostring(L, -1);
+ std::string replace_from = readParam<std::string>(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, -1, 2);
if(!lua_isstring(L, -1))
return false;
- std::string replace_to = lua_tostring(L, -1);
+ std::string replace_to = readParam<std::string>(L, -1);
lua_pop(L, 1);
- replacements.pairs.push_back(
- std::make_pair(replace_from, replace_to));
+ replacements.pairs.emplace_back(replace_from, replace_to);
// removes value, keeps key for next iteration
lua_pop(L, 1);
}
*/
if(type == "shaped"){
std::string output = getstringfield_default(L, table, "output", "");
- if(output == "")
+ if (output.empty())
throw LuaError("Crafting definition is missing an output");
int width = 0;
*/
else if(type == "shapeless"){
std::string output = getstringfield_default(L, table, "output", "");
- if(output == "")
+ if (output.empty())
throw LuaError("Crafting definition (shapeless)"
" is missing an output");
*/
else if(type == "cooking"){
std::string output = getstringfield_default(L, table, "output", "");
- if(output == "")
+ if (output.empty())
throw LuaError("Crafting definition (cooking)"
" is missing an output");
std::string recipe = getstringfield_default(L, table, "recipe", "");
- if(recipe == "")
+ if (recipe.empty())
throw LuaError("Crafting definition (cooking)"
" is missing a recipe"
" (output=\"" + output + "\")");
*/
else if(type == "fuel"){
std::string recipe = getstringfield_default(L, table, "recipe", "");
- if(recipe == "")
+ if (recipe.empty())
throw LuaError("Crafting definition (fuel)"
" is missing a recipe");
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 + "\")");
+ if (!output.empty()) {
+ if (craftdef->clearCraftsByOutput(c_output, getServer(L))) {
+ lua_pushboolean(L, true);
+ return 1;
+ }
+
+ warningstream << "No craft recipe known for output" << std::endl;
+ lua_pushboolean(L, false);
+ return 1;
}
std::vector<std::string> recipe;
int width = 0;
else if (type == "cooking") {
method = CRAFT_METHOD_COOKING;
std::string rec = getstringfield_default(L, table, "recipe", "");
- if (rec == "")
+ if (rec.empty())
throw LuaError("Crafting definition (cooking)"
" is missing a recipe");
recipe.push_back(rec);
else if (type == "fuel") {
method = CRAFT_METHOD_FUEL;
std::string rec = getstringfield_default(L, table, "recipe", "");
- if (rec == "")
+ if (rec.empty())
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;
+
+ std::vector<ItemStack> items;
+ items.reserve(recipe.size());
+ for (const auto &item : recipe)
+ items.emplace_back(item, 1, 0, getServer(L)->idef());
+ CraftInput input(method, width, items);
+
+ if (!craftdef->clearCraftsByInput(input, 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)
int ModApiCraft::l_get_craft_result(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
+ IGameDef *gdef = getGameDef(L);
- int input_i = 1;
+ const int input_i = 1;
std::string method_s = getstringfield_default(L, input_i, "method", "normal");
enum CraftMethod method = (CraftMethod)getenumfield(L, input_i, "method",
es_CraftMethod, CRAFT_METHOD_NORMAL);
width = luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, input_i, "items");
- std::vector<ItemStack> items = read_items(L, -1,getServer(L));
+ std::vector<ItemStack> items = read_items(L, -1, gdef);
lua_pop(L, 1); // items
- IGameDef *gdef = getServer(L);
ICraftDefManager *cdef = gdef->cdef();
CraftInput input(method, width, items);
CraftOutput output;
const std::vector<CraftDefinition*> &recipes,
const CraftOutput &output)
{
- lua_createtable(L, recipes.size(), 0);
-
if (recipes.empty()) {
lua_pushnil(L);
return;
}
+ lua_createtable(L, recipes.size(), 0);
+
std::vector<CraftDefinition*>::const_iterator it = recipes.begin();
for (unsigned i = 0; it != recipes.end(); ++it) {
lua_newtable(L);
NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1);
- Server *server = getServer(L);
+ IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0);
- std::vector<CraftDefinition*> recipes = server->cdef()
- ->getCraftRecipes(output, server, 1);
+ auto recipes = gdef->cdef()->getCraftRecipes(output, gdef, 1);
lua_createtable(L, 1, 0);
setintfield(L, -1, "width", 0);
return 1;
}
- push_craft_recipe(L, server, recipes[0], output);
+ push_craft_recipe(L, gdef, recipes[0], output);
return 1;
}
NO_MAP_LOCK_REQUIRED;
std::string item = luaL_checkstring(L, 1);
- Server *server = getServer(L);
+ IGameDef *gdef = getGameDef(L);
CraftOutput output(item, 0);
- std::vector<CraftDefinition*> recipes = server->cdef()
- ->getCraftRecipes(output, server);
+ auto recipes = gdef->cdef()->getCraftRecipes(output, gdef);
- push_craft_recipes(L, server, recipes, output);
+ push_craft_recipes(L, gdef, recipes, output);
return 1;
}
API_FCT(register_craft);
API_FCT(clear_craft);
}
+
+void ModApiCraft::InitializeAsync(lua_State *L, int top)
+{
+ // all read-only functions
+ API_FCT(get_all_craft_recipes);
+ API_FCT(get_craft_recipe);
+ API_FCT(get_craft_result);
+}