Vanessa
2013-03-08 96f326554ea38b9fd4cac54a4cd50dab4460d9ad
commit | author | age
a4a3c2 1 --data tables definitions
R 2 unified_inventory = {}
3 unified_inventory.players = {}
4 unified_inventory.current_page = {}
5 unified_inventory.current_index = {}
6 unified_inventory.items_list_size = 0
7 unified_inventory.items_list = {}
8 unified_inventory.filtered_items_list_size = {}
9 unified_inventory.filtered_items_list = {}
10 unified_inventory.activefilter = {}
11 unified_inventory.alternate = {}
12 unified_inventory.current_item = {}
13
14 -- default inventory page
15 unified_inventory.default = "craft"
16
17 -- homepos stuff
18 local home_gui = {}
19 local homepos = {}
20 unified_inventory.home_filename = minetest.get_worldpath()..'/unified_inventory_home'
21
22 -- Create detached creative inventory after loading all mods
23 minetest.after(0.01, function()
24     unified_inventory.items_list = {}
25     for name,def in pairs(minetest.registered_items) do
26         if (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0)
27                 and def.description and def.description ~= "" then
28             table.insert(unified_inventory.items_list, name)
29         end
30     end
31     table.sort(unified_inventory.items_list)
32     unified_inventory.items_list_size = #unified_inventory.items_list
33     print ("Unified Inventory. inventory size: "..unified_inventory.items_list_size)
34 end)
35
36 -- register_on_joinplayer
37 minetest.register_on_joinplayer(function(player)
38     local player_name = player:get_player_name()
35cf96 39     unified_inventory.players[player_name]={}
R 40     unified_inventory.players[player_name]["sound_volume"]=minetest.setting_get("sound_volume")*10
a4a3c2 41     unified_inventory.current_index[player_name] = 1
R 42     unified_inventory.filtered_items_list[player_name] = {}
43     unified_inventory.filtered_items_list[player_name] = unified_inventory.items_list
44     unified_inventory.filtered_items_list_size[player_name]=unified_inventory.items_list_size
45     unified_inventory.activefilter[player_name]=""
36b104 46     unified_inventory.apply_filter(player, "")
a4a3c2 47     unified_inventory.alternate[player_name] = 1
R 48     unified_inventory.current_item[player_name] =nil
49     unified_inventory.set_inventory_formspec(player,unified_inventory.get_formspec(player, unified_inventory.default))
50
51 --crafting guide inventories    
52 local inv = minetest.create_detached_inventory(player:get_player_name().."craftrecipe",{
53     allow_put = function(inv, listname, index, stack, player)
54         return 0
55         end,
56         allow_take = function(inv, listname, index, stack, player)
3a8a46 57             if minetest.setting_getbool("creative_mode") then
R 58                 return stack:get_count()
59             else
60                 return 0
61             end
a4a3c2 62         end,
R 63         allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
64             return 0
65         end,
66     })
67     inv:set_size("output", 1)
68     inv:set_size("build", 3*3)
69
70 -- refill slot
71 unified_inventory.refill = minetest.create_detached_inventory(player_name.."refill", {
72     allow_put = function(inv, listname, index, stack, player)
73         if minetest.setting_getbool("creative_mode") then
74             return stack:get_count()
75         else
76             return 0
77         end
78     end,
79     on_put = function(inv, listname, index, stack, player)
80         inv:set_stack(listname, index, ItemStack(stack:get_name().." "..stack:get_stack_max()))
ab14c4 81         minetest.sound_play("electricity", {to_player=player_name, gain = 1.0})
a4a3c2 82     end,
R 83 })
84 unified_inventory.refill:set_size("main", 1)
85 end)
86
87 -- trash slot
88 unified_inventory.trash = minetest.create_detached_inventory("trash", {
89     allow_put = function(inv, listname, index, stack, player)
90         if minetest.setting_getbool("creative_mode") then
91             return stack:get_count()
92         else
93             return 0
94         end
95     end,
96     on_put = function(inv, listname, index, stack, player)
97         inv:set_stack(listname, index, nil)
cc0600 98         local player_name=player:get_player_name()
R 99         minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
a4a3c2 100     end,
R 101 })
102 unified_inventory.trash:set_size("main", 1)
103
104 -- set_inventory_formspec
105 unified_inventory.set_inventory_formspec = function(player,formspec)
106     if player then
107         if minetest.setting_getbool("creative_mode") then
108             -- if creative mode is on then wait a bit
109             minetest.after(0.01,function()
110             player:set_inventory_formspec(formspec)
111             end)
112         else
113         player:set_inventory_formspec(formspec)
114         end
115     end
116 end
117
118 -- get_formspec
119 unified_inventory.get_formspec = function(player,page)
120     if player==nil then    return "" end
121     local player_name = player:get_player_name()
122     unified_inventory.current_page[player_name]=page
123     
124     local formspec = "size[14,10]"
eff76c 125
a4a3c2 126     -- player inventory
R 127     formspec = formspec .. "list[current_player;main;0,4.5;8,4;]"
eff76c 128
21e626 129     -- backgrounds
VE 130         formspec = formspec .. "background[-0.19,-0.2,;14.38,10.55;ui_form_bg.png]"
131     if page=="craft" then
96f326 132         formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_crafting_form.png]"
21e626 133         end
VE 134     if page=="craftguide" then
96f326 135         formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_craftguide_form.png]"
21e626 136         end
VE 137     if page=="misc" then
96f326 138         formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_misc_form.png]"
21e626 139         end
VE 140     if page=="bags" then
96f326 141         formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_bags_main_form.png]"
21e626 142         end
VE 143
144     for i=1,4 do
145         if page=="bag"..i then
146             local slots = player:get_inventory():get_stack(page, 1):get_definition().groups.bagslots
147             if slots == 8 then
96f326 148                 formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_bags_sm_form.png]"
21e626 149             elseif slots == 16 then
96f326 150                 formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_bags_med_form.png]"
21e626 151             elseif slots == 24 then
96f326 152                 formspec = formspec .. "background[0.06,0.99,;7.92,7.52;ui_bags_lg_form.png]"
21e626 153             end
VE 154         end
155     end
156
a4a3c2 157     -- main buttons
310cdb 158         local start_x=0
R 159         formspec = formspec .. "image_button["..(start_x+.65*0)..",9;.8,.8;ui_craft_icon.png;craft;]"
160         formspec = formspec .. "image_button["..(start_x+.65*1)..",9;.8,.8;ui_craftguide_icon.png;craftguide;]"
161         formspec = formspec .. "image_button["..(start_x+.65*2)..",9;.8,.8;ui_bags_icon.png;bags;]"
162         formspec = formspec .. "image_button["..(start_x+.65*3)..",9;.8,.8;ui_sethome_icon.png;home_gui_set;]"
163         formspec = formspec .. "image_button["..(start_x+.65*4)..",9;.8,.8;ui_gohome_icon.png;home_gui_go;]"
164         if minetest.setting_getbool("creative_mode") then
165         formspec = formspec .. "image_button["..(start_x+.65*5)..",9;.8,.8;ui_sun_icon.png;misc_set_day;]"
166         formspec = formspec .. "image_button["..(start_x+.65*6)..",9;.8,.8;ui_moon_icon.png;misc_set_night;]"
167         formspec = formspec .. "image_button["..(start_x+.65*7)..",9;.8,.8;ui_trash_icon.png;clear_inv;]"
168         end
169         
a4a3c2 170     --controls to flip items pages
310cdb 171         start_x=9.2
R 172         formspec = formspec .. "image_button["..(start_x+.6*0)..",9;.8,.8;ui_skip_backward_icon.png;start_list;]"
173         formspec = formspec .. "image_button["..(start_x+.6*1)..",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
174         formspec = formspec .. "image_button["..(start_x+.6*2)..",9;.8,.8;ui_left_icon.png;rewind1;]"
175         formspec = formspec .. "image_button["..(start_x+.6*3)..",9;.8,.8;ui_right_icon.png;forward1;]"
176         formspec = formspec .. "image_button["..(start_x+.6*4)..",9;.8,.8;ui_doubleright_icon.png;forward3;]"
177         formspec = formspec .. "image_button["..(start_x+.6*5)..",9;.8,.8;ui_skip_forward_icon.png;end_list;]"
178         
eff76c 179     -- search box
310cdb 180         formspec = formspec .. "field[9.5,8.325;3,1;searchbox;;]"
R 181         formspec = formspec .. "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]"
eff76c 182
a4a3c2 183     -- craft page
R 184     if page=="craft" then
185         formspec = formspec.."label[0,0;Crafting]"
186         formspec = formspec.."list[current_player;craftpreview;6,1;1,1;]"
187         formspec = formspec.."list[current_player;craft;2,1;3,3;]"
188             if minetest.setting_getbool("creative_mode") then
189                 formspec = formspec.."label[0,2.5;Refill:]"
190                 formspec = formspec.."list[detached:"..player_name.."refill;main;0,3;1,1;]"
191                 formspec = formspec.."label[7,2.5;Trash:]"
192                 formspec = formspec.."list[detached:trash;main;7,3;1,1;]"
193             end
194         end
eff76c 195
a4a3c2 196     -- craft guide page
R 197     if page=="craftguide" then
198         formspec = formspec.."label[0,0;Crafting Guide]"
199         formspec = formspec.."list[detached:"..player_name.."craftrecipe;build;2,1;3,3;]"
200         formspec = formspec.."list[detached:"..player_name.."craftrecipe;output;6,1;1,1;]"
201         formspec = formspec.."label[2,0.5;Input:]"
202         formspec = formspec.."label[6,0.5;Output:]"
203         formspec = formspec.."label[6,2.6;Method:]"
204         local item_name=unified_inventory.current_item[player_name]
205         if item_name then
206             formspec = formspec.."label[2,0;"..item_name.."]"    
207             local alternates = 0
208             local alternate = unified_inventory.alternate[player_name]
209             local crafts = crafts_table[item_name]
eff76c 210
a4a3c2 211             if crafts ~= nil then
R 212                 alternates = #crafts
213                 local craft = crafts[alternate]
214                 local method = "Crafting"
215                 if craft.type == "shapeless" then
216                 method="Crafting"
217                 end    
218                 if craft.type == "cooking" then
219                 method="Cooking"
220                 end    
221                 if craft.type == "fuel" then
222                 method="Fuel"
223                 end    
224                 if craft.type == "grinding" then
225                 method="Grinding"
226                 end    
227                 if craft.type == "alloy" then
228                 method="Alloy cooking"
229                 end    
230                 formspec = formspec.."label[6,3;"..method.."]"
231             end
232             
233             if alternates > 1 then
234             formspec = formspec.."label[0,2.6;Recipe "..tostring(alternate).." of "..tostring(alternates).."]"
235             formspec = formspec.."button[0,3.15;2,1;alternate;Alternate]"
236             end
237         end
238     end
315881 239
a4a3c2 240     -- bags
R 241     if page=="bags" then
315881 242     formspec = formspec.."label[0,0;Bags]"
a4a3c2 243     formspec=formspec.."button[0,2;2,0.5;bag1;Bag 1]"
R 244     formspec=formspec.."button[2,2;2,0.5;bag2;Bag 2]"
245     formspec=formspec.."button[4,2;2,0.5;bag3;Bag 3]"
246     formspec=formspec.."button[6,2;2,0.5;bag4;Bag 4]"
247     formspec=formspec.."list[detached:"..player_name.."_bags;bag1;0.5,1;1,1;]"
248     formspec=formspec.."list[detached:"..player_name.."_bags;bag2;2.5,1;1,1;]"
249     formspec=formspec.."list[detached:"..player_name.."_bags;bag3;4.5,1;1,1;]"
250     formspec=formspec.."list[detached:"..player_name.."_bags;bag4;6.5,1;1,1;]"
251         end
315881 252
a4a3c2 253     for i=1,4 do
R 254         if page=="bag"..i then
255             local image = player:get_inventory():get_stack("bag"..i, 1):get_definition().inventory_image
256             formspec=formspec.."image[7,0;1,1;"..image.."]"
257             formspec=formspec.."list[current_player;bag"..i.."contents;0,1;8,3;]"
258         end
259     end
315881 260
a4a3c2 261     --Items list
R 262     local list_index=unified_inventory.current_index[player_name]
263     local page=math.floor(list_index / (80) + 1)
264     local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name]-1) / (80) + 1)
265     local image
266     local item={}
267     for y=0,9,1 do
268     for x=0,7,1 do
269         name=unified_inventory.filtered_items_list[player_name][list_index]    
270         if minetest.registered_items[name] then
271         formspec=formspec.."item_image_button["..(8.2+x*.7)..","..(1+y*.7)..";.81,.81;"..name..";item_button"..list_index..";]"
272         list_index=list_index+1
273         end
274     end
275     end    
276     formspec=formspec.."label[8.2,0;Page:]"
277     formspec=formspec.."label[9,0;"..page.." of "..pagemax.."]"
278     formspec=formspec.."label[8.2,0.4;Filter:]"
279     formspec=formspec.."label[9,0.4;"..unified_inventory.activefilter[player_name].."]"
280     return formspec
281 end
282
283 -- register_on_player_receive_fields
284 minetest.register_on_player_receive_fields(function(player, formname, fields)
285     local player_name = player:get_player_name()
ab14c4 286
a4a3c2 287     -- main buttons
R 288     if fields.craft then
289         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,"craft"))
ab14c4 290         minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 291         return
R 292     end
ab14c4 293
a4a3c2 294     if fields.craftguide then
R 295         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,"craftguide"))
ab14c4 296         minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 297         return
R 298     end
ab14c4 299
a4a3c2 300     if fields.bags then
R 301         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,"bags"))
ab14c4 302         minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 303         return
R 304     end
310cdb 305
a4a3c2 306     -- bags
R 307     for i=1,4 do
308         local page = "bag"..i
309         if fields[page] then
310             if player:get_inventory():get_stack(page, 1):get_definition().groups.bagslots==nil then
311                 page = "bags"
312             end
313             unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,page))
ab14c4 314             minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 315             return
R 316         end
317     end
ab14c4 318
a4a3c2 319     -- Miscellaneous
R 320     if fields.home_gui_set then
321         unified_inventory.set_home(player, player:getpos())
310cdb 322         local home = homepos[player_name]
R 323         if home ~= nil then
ab14c4 324             minetest.sound_play("dingdong", {to_player=player_name, gain = 1.0})
310cdb 325             minetest.chat_send_player(player_name, "Home position set to: "..math.floor(home.x)..","..math.floor(home.y)..","..math.floor(home.z))
R 326         end
a4a3c2 327     end
R 328     if fields.home_gui_go then
329         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,"craft"))
ab14c4 330         minetest.sound_play("teleport", {to_player=player_name, gain = 1.0})
a4a3c2 331         unified_inventory.go_home(player)
R 332     end
333     if fields.misc_set_day then
334         if minetest.get_player_privs(player_name).settime==true then 
d300a5 335         minetest.sound_play("birds", {to_player=player_name, gain = 1.0})
a4a3c2 336         minetest.env:set_timeofday((6000 % 24000) / 24000)
R 337         minetest.chat_send_player(player_name, "Time of day set to 6am")
338         else
339         minetest.chat_send_player(player_name, "You don't have settime priviledge!")
340         end
341     end
342     if fields.misc_set_night then
343         if minetest.get_player_privs(player_name).settime==true then     
d300a5 344         minetest.sound_play("owl", {to_player=player_name, gain = 1.0})
a4a3c2 345         minetest.env:set_timeofday((21000 % 24000) / 24000)
R 346         minetest.chat_send_player(player_name, "Time of day set to 9pm")
347         else
348         minetest.chat_send_player(player_name, "You don't have settime priviledge!")    
349         end    
350     end
310cdb 351
R 352     if fields.clear_inv then
353         local inventory = {}
354         player:get_inventory():set_list("main", inventory)
355         minetest.chat_send_player(player_name, 'Inventory Cleared!')
cc0600 356         minetest.sound_play("trash_all", {to_player=player_name, gain = 1.0})
315881 357     end
a4a3c2 358     
R 359     -- Inventory page controls
360     local start=math.floor(unified_inventory.current_index[player_name]/80 +1 )
361     local start_i=start
362     local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name]-1) / (80) + 1)
363     
364     if fields.start_list then
ab14c4 365         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 366         start_i = 1
R 367     end
368     if fields.rewind1 then
ab14c4 369         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 370         start_i = start_i - 1
R 371     end
372     if fields.forward1 then
ab14c4 373         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 374         start_i = start_i + 1
R 375     end
376     if fields.rewind3 then
ab14c4 377         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 378         start_i = start_i - 3
R 379     end
380     if fields.forward3 then
ab14c4 381         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 382         start_i = start_i + 3
R 383     end
384     if fields.end_list then
ab14c4 385         minetest.sound_play("paperflip1", {to_player=player_name, gain = 1.0})
a4a3c2 386         start_i = pagemax
R 387     end
388     if start_i < 1 then
389         start_i = 1
390     end
391     if start_i > pagemax then
392         start_i =  pagemax
393     end
394     if not (start_i    ==start) then
395         unified_inventory.current_index[player_name] = (start_i-1)*80+1
396         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,unified_inventory.current_page[player_name]))
397         end
398     
399     -- Item list buttons
400     local list_index=unified_inventory.current_index[player_name]
401     local page=unified_inventory.current_page[player_name]
402     for i=0,80,1 do
403         local button="item_button"..list_index
404         if fields[button] then 
ab14c4 405             minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 406             if minetest.setting_getbool("creative_mode")==false then
R 407                 unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,"craftguide"))
408                 page="craftguide"
310cdb 409                 end
a4a3c2 410             if page=="craftguide" then 
R 411                 unified_inventory.current_item[player_name] = unified_inventory.filtered_items_list[player_name][list_index] 
412                 unified_inventory.alternate[player_name] = 1
413                 unified_inventory.update_recipe (player, unified_inventory.filtered_items_list[player_name][list_index], 1)
414                 unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,unified_inventory.current_page[player_name]))
415             else
416                 if minetest.setting_getbool("creative_mode") then
417                     local inv = player:get_inventory()
418                     dst_stack={}
419                     dst_stack["name"] = unified_inventory.filtered_items_list[player_name][list_index] 
420                     dst_stack["count"]=99
421                     if inv:room_for_item("main",dst_stack) then
422                     inv:add_item("main",dst_stack)
423                     end
424                 end    
425             end    
426         end    
427     list_index=list_index+1
428     end
429     
430     if fields.searchbutton then
36b104 431         unified_inventory.apply_filter(player, fields.searchbox)
a4a3c2 432         unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,unified_inventory.current_page[player_name]))
ab14c4 433         minetest.sound_play("paperflip2", {to_player=player_name, gain = 1.0})
a4a3c2 434     end    
R 435     
436     -- alternate button
437     if fields.alternate then
ab14c4 438         minetest.sound_play("click", {to_player=player_name, gain = 0.1})
a4a3c2 439         local item_name=unified_inventory.current_item[player_name]
R 440         if item_name then
441             local alternates = 0
442             local alternate=unified_inventory.alternate[player_name]
443             local crafts = crafts_table[item_name]
444             if crafts ~= nil then
445                 alternates = #crafts
446             end
447             if alternates > 1 then
448             alternate=alternate+1
449             if alternate>alternates then
450                 alternate=1
451             end
452             unified_inventory.alternate[player_name]=alternate        
453             unified_inventory.update_recipe (player, unified_inventory.current_item[player_name], alternate)
454             unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,unified_inventory.current_page[player_name]))
455             end
456         end    
457     end        
458 end)
459
460 -- load_home
461 local load_home = function()
462     local input = io.open(unified_inventory.home_filename..".home", "r")
463     if input then
464         while true do
465             local x = input:read("*n")
466             if x == nil then
467                 break
468             end
469             local y = input:read("*n")
470             local z = input:read("*n")
471             local name = input:read("*l")
472             homepos[name:sub(2)] = {x = x, y = y, z = z}
473         end
474         io.close(input)
475     else
476         homepos = {}
477     end
478 end
479 load_home() -- run it now
480
481 -- set_home
482 unified_inventory.set_home = function(player, pos)
310cdb 483     local player_name=player:get_player_name()
R 484     homepos[player_name] = pos
a4a3c2 485     -- save the home data from the table to the file
R 486     local output = io.open(unified_inventory.home_filename..".home", "w")
487     for k, v in pairs(homepos) do
488         if v ~= nil then
489             output:write(math.floor(v.x).." "..math.floor(v.y).." "..math.floor(v.z).." "..k.."\n")
490         end
491     end
492     io.close(output)
493 end
494
495 -- go_home 
496 unified_inventory.go_home = function(player)
497     local pos = homepos[player:get_player_name()]
498     if pos~=nil then
499         player:setpos(pos)
500     end
501 end
502
503 --apply filter to the inventory list (create filtered copy of full one)
36b104 504 unified_inventory.apply_filter = function(player,filter)
K 505     local player_name = player:get_player_name() 
a4a3c2 506     local size=0
R 507     local str_temp1=string.lower(filter)
0137db 508     if str_temp1 ~= "" then 
R 509         for i=1,str_temp1:len(),1 do
510             if string.byte(str_temp1,i) == 91 then 
511                 str_temp1=""
512                 end
513             end
514     end
a4a3c2 515     local str_temp2
R 516     local str_temp3
517     unified_inventory.filtered_items_list[player_name]={}
518     for name,def in pairs(minetest.registered_items) do
519         if (not def.groups.not_in_creative_inventory or def.groups.not_in_creative_inventory == 0)
520                 and def.description and def.description ~= "" then
521             str_temp2=string.lower(def.name)
522             str_temp3=string.lower(def.description)
523             if string.find(str_temp2, str_temp1) or string.find(str_temp3, str_temp1) then
524                 table.insert(unified_inventory.filtered_items_list[player_name], name)
525                 size=size+1
526             end
527         end
528     
529     end
530     table.sort(unified_inventory.filtered_items_list[player_name])
531     unified_inventory.filtered_items_list_size[player_name]=size
532     unified_inventory.current_index[player_name]=1    
533     unified_inventory.activefilter[player_name]=filter
534     unified_inventory.set_inventory_formspec(player, unified_inventory.get_formspec(player,unified_inventory.current_page[player_name]))
535 end
536
537
538 -- update_recipe
539 unified_inventory.update_recipe = function(player, stack_name, alternate)
540     --print("Lookup:"..stack_name)
541     local inv = minetest.get_inventory({type="detached", name=player:get_player_name().."craftrecipe"})    
542     for i=0,inv:get_size("build"),1 do
543         inv:set_stack("build", i, nil)
544     end
545     inv:set_stack("cook", 1, nil)
546     inv:set_stack("fuel", 1, nil)
547
548     inv:set_stack("output", 1, stack_name)
549     local def
550     alternate = tonumber(alternate) or 1
551     local crafts = crafts_table[stack_name]
552     if crafts == nil then
553         --minetest.chat_send_player(player:get_player_name(), "no recipe available for "..stack_name)
554         return
555     end
556     if alternate < 1 or alternate > #crafts then
557         alternate = 1
558     end
559     local craft = crafts[alternate]
560     --print (dump(craft))
561     --minetest.chat_send_player(player:get_player_name(), "recipe for "..stack_name..": "..dump(craft))
562     
563     local itemstack = ItemStack(craft.output)
564     inv:set_stack("output", 1, itemstack)
565
566     -- cook, fuel, grinding recipes
567     if craft.type == "cooking" or craft.type == "fuel" or craft.type == "grinding" then
568         def=unified_inventory.find_item_def(craft.recipe)
569         if def then
570             inv:set_stack("build", 1, def)
571         end
572         return 
573     end
574     
575     -- build (shaped or shapeless)
576     if craft.recipe[1] then
577         def=unified_inventory.find_item_def(craft.recipe[1])
578         if def then
579             inv:set_stack("build", 1, def)
580         else
581             def=unified_inventory.find_item_def(craft.recipe[1][1])
582             if def then
583                 inv:set_stack("build", 1, def)
584             end
585             def=unified_inventory.find_item_def(craft.recipe[1][2])
586             if def then
587                 inv:set_stack("build", 2, def)
588             end
589             def=unified_inventory.find_item_def(craft.recipe[1][3])
590             if def then
591                 inv:set_stack("build", 3, def)
592             end
593         end
594     end
595     if craft.recipe[2] then
596         def=unified_inventory.find_item_def(craft.recipe[2])
597         if def then
598             inv:set_stack("build", 2, def)
599         else
600             def=unified_inventory.find_item_def(craft.recipe[2][1])
601             if def then
602                 inv:set_stack("build", 4, def)
603             end
604             def=unified_inventory.find_item_def(craft.recipe[2][2])
605             if def then
606                 inv:set_stack("build", 5, def)
607             end
608             def=unified_inventory.find_item_def(craft.recipe[2][3])
609             if def then
610                 inv:set_stack("build", 6, def)
611             end
612         end
613     end
614     
615     if craft.recipe[3] then
616         def=unified_inventory.find_item_def(craft.recipe[3])
617         if def then
618             inv:set_stack("build", 3, def)
619         else
620             def=unified_inventory.find_item_def(craft.recipe[3][1])
621             if def then
622                 inv:set_stack("build", 7, def)
623             end
624             def=unified_inventory.find_item_def(craft.recipe[3][2])
625             if def then
626                 inv:set_stack("build", 8, def)
627             end
628             def=unified_inventory.find_item_def(craft.recipe[3][3])
629             if def then
630                 inv:set_stack("build", 9, def)
631             end
632         end
633     end
634     if craft.recipe[4] then
635         def=unified_inventory.find_item_def(craft.recipe[4])
636         if def then
637             inv:set_stack("build", 4, def)
638         end
639     end
640     if craft.recipe[5] then
641         def=unified_inventory.find_item_def(craft.recipe[5])
642         if def then
643             inv:set_stack("build", 5, def)
644         end
645     end
646     if craft.recipe[6] then
647         def=unified_inventory.find_item_def(craft.recipe[6])
648         if def then
649             inv:set_stack("build", 6, def)
650         end
651     end
652     if craft.recipe[7] then
653         def=unified_inventory.find_item_def(craft.recipe[7])
654         if def then
655             inv:set_stack("build", 7, def)
656         end
657     end
658     if craft.recipe[8] then
659         def=unified_inventory.find_item_def(craft.recipe[8])
660         if def then
661             inv:set_stack("build", 8, def)
662         end
663     end
664     if craft.recipe[9] then
665         def=unified_inventory.find_item_def(craft.recipe[9])
666         if def then
667             inv:set_stack("build", 9, def)
668         end
669     end
670 end
671
672 unified_inventory.find_item_def = function(def1)
673 if type(def1)=="string" then
674     if string.find(def1, "group:") then
675         def1=string.gsub(def1, "group:", "")
676         def1=string.gsub(def1, '\"', "")
677         for name,def in pairs(minetest.registered_items) do
678                 if def.groups[def1] == 1 and def.groups.not_in_creative_inventory ~= 1 then
679                     return def
680                 end
681         end
682     else
683     return def1
684     end
685 end
686 return nil
687 end