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