@@ -173,7 +173,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
173
173
174
174
CraftDefinition *def = new CraftDefinitionShaped (
175
175
output, width, recipe, replacements);
176
- craftdef->registerCraft (def);
176
+ craftdef->registerCraft (def, getServer (L) );
177
177
}
178
178
/*
179
179
CraftDefinitionShapeless
@@ -205,7 +205,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
205
205
206
206
CraftDefinition *def = new CraftDefinitionShapeless (
207
207
output, recipe, replacements);
208
- craftdef->registerCraft (def);
208
+ craftdef->registerCraft (def, getServer (L) );
209
209
}
210
210
/*
211
211
CraftDefinitionToolRepair
@@ -216,7 +216,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
216
216
217
217
CraftDefinition *def = new CraftDefinitionToolRepair (
218
218
additional_wear);
219
- craftdef->registerCraft (def);
219
+ craftdef->registerCraft (def, getServer (L) );
220
220
}
221
221
/*
222
222
CraftDefinitionCooking
@@ -246,7 +246,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
246
246
247
247
CraftDefinition *def = new CraftDefinitionCooking (
248
248
output, recipe, cooktime, replacements);
249
- craftdef->registerCraft (def);
249
+ craftdef->registerCraft (def, getServer (L) );
250
250
}
251
251
/*
252
252
CraftDefinitionFuel
@@ -270,7 +270,7 @@ int ModApiCraft::l_register_craft(lua_State *L)
270
270
271
271
CraftDefinition *def = new CraftDefinitionFuel (
272
272
recipe, burntime, replacements);
273
- craftdef->registerCraft (def);
273
+ craftdef->registerCraft (def, getServer (L) );
274
274
}
275
275
else
276
276
{
@@ -326,56 +326,80 @@ int ModApiCraft::l_get_craft_result(lua_State *L)
326
326
return 2 ;
327
327
}
328
328
329
+
330
+ void push_craft_recipe (lua_State *L, IGameDef *gdef,
331
+ const CraftDefinition *recipe,
332
+ const CraftOutput &tmpout)
333
+ {
334
+ CraftInput input = recipe->getInput (tmpout, gdef);
335
+ CraftOutput output = recipe->getOutput (input, gdef);
336
+
337
+ lua_newtable (L); // items
338
+ std::vector<ItemStack>::const_iterator iter = input.items .begin ();
339
+ for (u16 j = 1 ; iter != input.items .end (); iter++, j++) {
340
+ if (iter->empty ())
341
+ continue ;
342
+ lua_pushstring (L, iter->name .c_str ());
343
+ lua_rawseti (L, -2 , j);
344
+ }
345
+ lua_setfield (L, -2 , " items" );
346
+ setintfield (L, -1 , " width" , input.width );
347
+ switch (input.method ) {
348
+ case CRAFT_METHOD_NORMAL:
349
+ lua_pushstring (L, " normal" );
350
+ break ;
351
+ case CRAFT_METHOD_COOKING:
352
+ lua_pushstring (L, " cooking" );
353
+ break ;
354
+ case CRAFT_METHOD_FUEL:
355
+ lua_pushstring (L, " fuel" );
356
+ break ;
357
+ default :
358
+ lua_pushstring (L, " unknown" );
359
+ }
360
+ lua_setfield (L, -2 , " type" );
361
+ lua_pushstring (L, tmpout.item .c_str ());
362
+ lua_setfield (L, -2 , " output" );
363
+ }
364
+
365
+ void push_craft_recipes (lua_State *L, IGameDef *gdef,
366
+ const std::vector<CraftDefinition*> &recipes,
367
+ const CraftOutput &output)
368
+ {
369
+ lua_createtable (L, recipes.size (), 0 );
370
+
371
+ if (recipes.empty ()) {
372
+ lua_pushnil (L);
373
+ return ;
374
+ }
375
+
376
+ std::vector<CraftDefinition*>::const_iterator it = recipes.begin ();
377
+ for (unsigned i = 0 ; it != recipes.end (); ++it) {
378
+ lua_newtable (L);
379
+ push_craft_recipe (L, gdef, *it, output);
380
+ lua_rawseti (L, -2 , ++i);
381
+ }
382
+ }
383
+
384
+
329
385
// get_craft_recipe(result item)
330
386
int ModApiCraft::l_get_craft_recipe (lua_State *L)
331
387
{
332
388
NO_MAP_LOCK_REQUIRED;
333
389
334
- int k = 1 ;
335
- int input_i = 1 ;
336
- std::string o_item = luaL_checkstring (L,input_i);
390
+ std::string item = luaL_checkstring (L, 1 );
391
+ Server *server = getServer (L);
392
+ CraftOutput output (item, 0 );
393
+ std::vector<CraftDefinition*> recipes = server->cdef ()
394
+ ->getCraftRecipes (output, server, 1 );
337
395
338
- IGameDef *gdef = getServer (L);
339
- ICraftDefManager *cdef = gdef->cdef ();
340
- CraftInput input;
341
- CraftOutput output (o_item,0 );
342
- bool got = cdef->getCraftRecipe (input, output, gdef);
343
- lua_newtable (L); // output table
344
- if (got){
345
- lua_newtable (L);
346
- for (std::vector<ItemStack>::const_iterator
347
- i = input.items .begin ();
348
- i != input.items .end (); i++, k++)
349
- {
350
- if (i->empty ())
351
- {
352
- continue ;
353
- }
354
- lua_pushinteger (L,k);
355
- lua_pushstring (L,i->name .c_str ());
356
- lua_settable (L, -3 );
357
- }
358
- lua_setfield (L, -2 , " items" );
359
- setintfield (L, -1 , " width" , input.width );
360
- switch (input.method ) {
361
- case CRAFT_METHOD_NORMAL:
362
- lua_pushstring (L," normal" );
363
- break ;
364
- case CRAFT_METHOD_COOKING:
365
- lua_pushstring (L," cooking" );
366
- break ;
367
- case CRAFT_METHOD_FUEL:
368
- lua_pushstring (L," fuel" );
369
- break ;
370
- default :
371
- lua_pushstring (L," unknown" );
372
- }
373
- lua_setfield (L, -2 , " type" );
374
- } else {
396
+ if (recipes.empty ()) {
375
397
lua_pushnil (L);
376
398
lua_setfield (L, -2 , " items" );
377
399
setintfield (L, -1 , " width" , 0 );
400
+ return 1 ;
378
401
}
402
+ push_craft_recipe (L, server, recipes[0 ], output);
379
403
return 1 ;
380
404
}
381
405
@@ -384,59 +408,13 @@ int ModApiCraft::l_get_all_craft_recipes(lua_State *L)
384
408
{
385
409
NO_MAP_LOCK_REQUIRED;
386
410
387
- std::string o_item = luaL_checkstring (L,1 );
388
- IGameDef *gdef = getServer (L);
389
- ICraftDefManager *cdef = gdef->cdef ();
390
- CraftInput input;
391
- CraftOutput output (o_item,0 );
392
- std::vector<CraftDefinition*> recipes_list;
393
- recipes_list = cdef->getCraftRecipes (output, gdef);
394
- if (recipes_list.empty ()) {
395
- lua_pushnil (L);
396
- return 1 ;
397
- }
411
+ std::string item = luaL_checkstring (L, 1 );
412
+ Server *server = getServer (L);
413
+ CraftOutput output (item, 0 );
414
+ std::vector<CraftDefinition*> recipes = server->cdef ()
415
+ ->getCraftRecipes (output, server);
398
416
399
- lua_createtable (L, recipes_list.size (), 0 );
400
- std::vector<CraftDefinition*>::const_iterator iter = recipes_list.begin ();
401
- for (u16 i = 0 ; iter != recipes_list.end (); iter++) {
402
- CraftOutput tmpout;
403
- tmpout.item = " " ;
404
- tmpout.time = 0 ;
405
- tmpout = (*iter)->getOutput (input, gdef);
406
- std::string query = tmpout.item ;
407
- char *fmtpos, *fmt = &query[0 ];
408
- if (strtok_r (fmt, " " , &fmtpos) == output.item ) {
409
- input = (*iter)->getInput (output, gdef);
410
- lua_newtable (L);
411
- lua_newtable (L); // items
412
- std::vector<ItemStack>::const_iterator iter = input.items .begin ();
413
- for (u16 j = 1 ; iter != input.items .end (); iter++, j++) {
414
- if (iter->empty ())
415
- continue ;
416
- lua_pushstring (L, iter->name .c_str ());
417
- lua_rawseti (L, -2 , j);
418
- }
419
- lua_setfield (L, -2 , " items" );
420
- setintfield (L, -1 , " width" , input.width );
421
- switch (input.method ) {
422
- case CRAFT_METHOD_NORMAL:
423
- lua_pushstring (L, " normal" );
424
- break ;
425
- case CRAFT_METHOD_COOKING:
426
- lua_pushstring (L, " cooking" );
427
- break ;
428
- case CRAFT_METHOD_FUEL:
429
- lua_pushstring (L, " fuel" );
430
- break ;
431
- default :
432
- lua_pushstring (L, " unknown" );
433
- }
434
- lua_setfield (L, -2 , " type" );
435
- lua_pushstring (L, &tmpout.item [0 ]);
436
- lua_setfield (L, -2 , " output" );
437
- lua_rawseti (L, -2 , ++i);
438
- }
439
- }
417
+ push_craft_recipes (L, server, recipes, output);
440
418
return 1 ;
441
419
}
442
420
0 commit comments