@@ -330,8 +330,7 @@ int l_get_craft_result(lua_State *L)
330
330
// get_craft_recipe(result item)
331
331
int l_get_craft_recipe (lua_State *L)
332
332
{
333
- int k = 0 ;
334
- char tmp[20 ];
333
+ int k = 1 ;
335
334
int input_i = 1 ;
336
335
std::string o_item = luaL_checkstring (L,input_i);
337
336
@@ -351,8 +350,7 @@ int l_get_craft_recipe(lua_State *L)
351
350
{
352
351
continue ;
353
352
}
354
- sprintf (tmp," %d" ,k);
355
- lua_pushstring (L,tmp);
353
+ lua_pushinteger (L,k);
356
354
lua_pushstring (L,i->name .c_str ());
357
355
lua_settable (L, -3 );
358
356
}
@@ -383,9 +381,7 @@ int l_get_craft_recipe(lua_State *L)
383
381
// get_all_craft_recipes(result item)
384
382
int l_get_all_craft_recipes (lua_State *L)
385
383
{
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 );
389
385
IGameDef *gdef = get_server (L);
390
386
ICraftDefManager *cdef = gdef->cdef ();
391
387
CraftInput input;
@@ -402,7 +398,7 @@ int l_get_all_craft_recipes(lua_State *L)
402
398
int table_insert = lua_gettop (L);
403
399
lua_newtable (L);
404
400
int table = lua_gettop (L);
405
- for (std::vector<CraftDefinition*>::const_iterator
401
+ for (std::vector<CraftDefinition*>::const_iterator
406
402
i = recipes_list.begin ();
407
403
i != recipes_list.end (); i++)
408
404
{
@@ -411,28 +407,29 @@ int l_get_all_craft_recipes(lua_State *L)
411
407
tmpout.time = 0 ;
412
408
CraftDefinition *def = *i;
413
409
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 )
415
413
{
416
414
input = def->getInput (output, gdef);
417
415
lua_pushvalue (L, table_insert);
418
416
lua_pushvalue (L, table);
419
417
lua_newtable (L);
420
- int k = 0 ;
418
+ int k = 1 ;
421
419
lua_newtable (L);
422
420
for (std::vector<ItemStack>::const_iterator
423
421
i = input.items .begin ();
424
422
i != input.items .end (); i++, k++)
425
423
{
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 ());
430
428
lua_settable (L, -3 );
431
429
}
432
430
lua_setfield (L, -2 , " items" );
433
431
setintfield (L, -1 , " width" , input.width );
434
- switch (input.method )
435
- {
432
+ switch (input.method ) {
436
433
case CRAFT_METHOD_NORMAL:
437
434
lua_pushstring (L," normal" );
438
435
break ;
@@ -446,8 +443,10 @@ int l_get_all_craft_recipes(lua_State *L)
446
443
lua_pushstring (L," unknown" );
447
444
}
448
445
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 ));
451
450
}
452
451
}
453
452
return 1 ;
1 commit comments
PilzAdam commentedon Apr 11, 2013
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.