Skip to content

Commit e7f5cdf

Browse files
RealBadAngelkwolekr
authored andcommittedApr 11, 2013
Bugfixes to get_craft_recipe and get_all_craft_recipes.
Improvements to get_all_craft_recipes (see api doc)
1 parent dda2071 commit e7f5cdf

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed
 

‎doc/lua_api.txt

+17-4
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,23 @@ minetest.get_craft_recipe(output) -> input
10021002
^ input.items = for example { stack 1, stack 2, stack 3, stack 4,
10031003
stack 5, stack 6, stack 7, stack 8, stack 9 }
10041004
^ input.items = nil if no recipe found
1005-
minetest.get_all_craft_recipes(output) -> table or nil
1006-
^ returns table with all registered recipes for output item (node)
1007-
^ returns nil if no recipe was found
1008-
^ table entries have same format as minetest.get_craft_recipe
1005+
minetest.get_all_craft_recipes(query item) -> table or nil
1006+
^ returns indexed table with all registered recipes for query item (node)
1007+
or nil if no recipe was found
1008+
recipe entry table:
1009+
{
1010+
method = 'normal' or 'cooking' or 'fuel'
1011+
width = 0-3, 0 means shapeless recipe
1012+
items = indexed [1-9] table with recipe items
1013+
output = string with item name and quantity
1014+
}
1015+
Example query for default:gold_ingot will return table:
1016+
{
1017+
1={type = "cooking", width = 3, output = "default:gold_ingot",
1018+
items = {1 = "default:gold_lump"}},
1019+
2={type = "normal", width = 1, output = "default:gold_ingot 9",
1020+
items = {1 = "default:goldblock"}}
1021+
}
10091022
minetest.handle_node_drops(pos, drops, digger)
10101023
^ drops: list of itemstrings
10111024
^ Handles drops from nodes after digging: Default action is to put them into

‎src/scriptapi_craft.cpp

+17-18
Original file line numberDiff line numberDiff line change
@@ -330,8 +330,7 @@ int l_get_craft_result(lua_State *L)
330330
// get_craft_recipe(result item)
331331
int l_get_craft_recipe(lua_State *L)
332332
{
333-
int k = 0;
334-
char tmp[20];
333+
int k = 1;
335334
int input_i = 1;
336335
std::string o_item = luaL_checkstring(L,input_i);
337336

@@ -351,8 +350,7 @@ int l_get_craft_recipe(lua_State *L)
351350
{
352351
continue;
353352
}
354-
sprintf(tmp,"%d",k);
355-
lua_pushstring(L,tmp);
353+
lua_pushinteger(L,k);
356354
lua_pushstring(L,i->name.c_str());
357355
lua_settable(L, -3);
358356
}
@@ -383,9 +381,7 @@ int l_get_craft_recipe(lua_State *L)
383381
// get_all_craft_recipes(result item)
384382
int l_get_all_craft_recipes(lua_State *L)
385383
{
386-
char tmp[20];
387-
int input_i = 1;
388-
std::string o_item = luaL_checkstring(L,input_i);
384+
std::string o_item = luaL_checkstring(L,1);
389385
IGameDef *gdef = get_server(L);
390386
ICraftDefManager *cdef = gdef->cdef();
391387
CraftInput input;
@@ -402,7 +398,7 @@ int l_get_all_craft_recipes(lua_State *L)
402398
int table_insert = lua_gettop(L);
403399
lua_newtable(L);
404400
int table = lua_gettop(L);
405-
for(std::vector<CraftDefinition*>::const_iterator
401+
for (std::vector<CraftDefinition*>::const_iterator
406402
i = recipes_list.begin();
407403
i != recipes_list.end(); i++)
408404
{
@@ -411,28 +407,29 @@ int l_get_all_craft_recipes(lua_State *L)
411407
tmpout.time = 0;
412408
CraftDefinition *def = *i;
413409
tmpout = def->getOutput(input, gdef);
414-
if(tmpout.item.substr(0,output.item.length()) == output.item)
410+
std::string query = tmpout.item;
411+
char *fmtpos, *fmt = &query[0];
412+
if (strtok_r(fmt, " ", &fmtpos) == output.item)
415413
{
416414
input = def->getInput(output, gdef);
417415
lua_pushvalue(L, table_insert);
418416
lua_pushvalue(L, table);
419417
lua_newtable(L);
420-
int k = 0;
418+
int k = 1;
421419
lua_newtable(L);
422420
for(std::vector<ItemStack>::const_iterator
423421
i = input.items.begin();
424422
i != input.items.end(); i++, k++)
425423
{
426-
if (i->empty()) continue;
427-
sprintf(tmp,"%d",k);
428-
lua_pushstring(L,tmp);
429-
lua_pushstring(L,i->name.c_str());
424+
if (i->empty())
425+
continue;
426+
lua_pushinteger(L, k);
427+
lua_pushstring(L, i->name.c_str());
430428
lua_settable(L, -3);
431429
}
432430
lua_setfield(L, -2, "items");
433431
setintfield(L, -1, "width", input.width);
434-
switch (input.method)
435-
{
432+
switch (input.method) {
436433
case CRAFT_METHOD_NORMAL:
437434
lua_pushstring(L,"normal");
438435
break;
@@ -446,8 +443,10 @@ int l_get_all_craft_recipes(lua_State *L)
446443
lua_pushstring(L,"unknown");
447444
}
448445
lua_setfield(L, -2, "type");
449-
if(lua_pcall(L, 2, 0, 0))
450-
script_error(L, "error: %s", lua_tostring(L, -1));
446+
lua_pushstring(L, &tmpout.item[0]);
447+
lua_setfield(L, -2, "output");
448+
if (lua_pcall(L, 2, 0, 0))
449+
script_error(L, "error: %s", lua_tostring(L, -1));
451450
}
452451
}
453452
return 1;

1 commit comments

Comments
 (1)

PilzAdam commented on Apr 11, 2013

@PilzAdam
Contributor

Why the example? lua-api.txt shouldnt contain many examples. It should be short and precise.
This function is completly understandable without examples. You can add the example to the dev wiki if you want.

Please sign in to comment.