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