ShadowNinja
2013-09-25 2e52c6c795704c12bebf3790e1ef70d7f0234801
commit | author | age
310761 1
S 2 function unified_inventory.get_formspec(player, page)
3     if not player then
4         return ""
5     end
6     local player_name = player:get_player_name()
7     unified_inventory.current_page[player_name] = page
8
9     local formspec = "size[14,10]"
10
11     -- Player inventory
12     formspec = formspec .. "list[current_player;main;0,4.5;8,4;]"
13
14     -- Background
15     formspec = formspec .. "background[-0.19,-0.2;14.38,10.55;ui_form_bg.png]"
16     
17     -- Current page
18     if unified_inventory.pages[page] then
19         formspec = unified_inventory.pages[page].get_formspec(player, formspec)
20     else
21         return "" -- Invalid page name
22     end
23
24     -- Main buttons
25     local i = 0
26     for i, def in pairs(unified_inventory.buttons) do
27         if def.type == "image" then
28             formspec = formspec.."image_button["
29                     ..(0.65 * i)..",9;0.8,0.8;"
30                     ..minetest.formspec_escape(def.image)..";"
31                     ..minetest.formspec_escape(def.name)..";]"
32         end
33         i = i + 1
34     end
35
36     -- Controls to flip items pages
37     local start_x = 9.2
38     formspec = formspec .. "image_button["..(start_x + 0.6 * 0)..",9;.8,.8;ui_skip_backward_icon.png;start_list;]"
39     formspec = formspec .. "image_button["..(start_x + 0.6 * 1)..",9;.8,.8;ui_doubleleft_icon.png;rewind3;]"
40     formspec = formspec .. "image_button["..(start_x + 0.6 * 2)..",9;.8,.8;ui_left_icon.png;rewind1;]"
41     formspec = formspec .. "image_button["..(start_x + 0.6 * 3)..",9;.8,.8;ui_right_icon.png;forward1;]"
42     formspec = formspec .. "image_button["..(start_x + 0.6 * 4)..",9;.8,.8;ui_doubleright_icon.png;forward3;]"
43     formspec = formspec .. "image_button["..(start_x + 0.6 * 5)..",9;.8,.8;ui_skip_forward_icon.png;end_list;]"
44
45     -- Search box
46     formspec = formspec .. "field[9.5,8.325;3,1;searchbox;;]"
47     formspec = formspec .. "image_button[12.2,8.1;.8,.8;ui_search_icon.png;searchbutton;]"
48
49     -- Items list
50     local list_index = unified_inventory.current_index[player_name]
51     local page = math.floor(list_index / (80) + 1)
52     local pagemax = math.floor((unified_inventory.filtered_items_list_size[player_name] - 1) / (80) + 1)
53     local image = nil
54     local item = {}
55     for y = 0, 9 do
56     for x = 0, 7 do
57         name = unified_inventory.filtered_items_list[player_name][list_index]    
58         if minetest.registered_items[name] then
59             formspec = formspec.."item_image_button["
60                     ..(8.2 + x * 0.7)..","
61                     ..(1   + y * 0.7)..";.81,.81;"
62                     ..name..";item_button"
63                     ..list_index..";]"
64             list_index = list_index + 1
65         end
66     end
67     end
68     formspec = formspec.."label[8.2,0;Page:]"
69     formspec = formspec.."label[9,0;"..page.." of "..pagemax.."]"
70     formspec = formspec.."label[8.2,0.4;Filter:]"
71     formspec = formspec.."label[9,0.4;"..unified_inventory.activefilter[player_name].."]"
72     return formspec
73 end
74
75 function unified_inventory.set_inventory_formspec(player, page)
76     if player then
77         local formspec = unified_inventory.get_formspec(player, page)
78         player:set_inventory_formspec(formspec)
79     end
80 end
81
82 --apply filter to the inventory list (create filtered copy of full one)
83 function unified_inventory.apply_filter(player, filter)
84     local player_name = player:get_player_name() 
85     local size = 0
86     local lfilter = string.lower(filter)
87     if lfilter ~= "" then 
88         for i=1, lfilter:len() do
89             if lfilter:sub(i, i) == '[' then 
90                 str_temp1 = ""
91                 break
92             end
93         end
94     end
95     unified_inventory.filtered_items_list[player_name]={}
96     for name, def in pairs(minetest.registered_items) do
97         if (not def.groups.not_in_creative_inventory or
98            def.groups.not_in_creative_inventory == 0)
99            and def.description and def.description ~= "" then
100             local lname = string.lower(name)
101             local ldesc = string.lower(def.description)
102             if string.find(lname, lfilter) or string.find(ldesc, lfilter) then
103                 table.insert(unified_inventory.filtered_items_list[player_name], name)
104                 size = size + 1
105             end
106         end
107     
108     end
109     table.sort(unified_inventory.filtered_items_list[player_name])
110     unified_inventory.filtered_items_list_size[player_name] = size
111     unified_inventory.current_index[player_name] = 1
112     unified_inventory.activefilter[player_name] = filter
113     unified_inventory.set_inventory_formspec(player,
114             unified_inventory.current_page[player_name])
115 end
116
117
118 -- update_recipe
119 function unified_inventory.update_recipe(player, stack_name, alternate)
120     local inv = minetest.get_inventory({
121         type = "detached",
122         name = player:get_player_name().."craftrecipe"
123     })    
124     for i = 0, inv:get_size("build") do
125         inv:set_stack("build", i, nil)
126     end
127     inv:set_stack("output", 1, nil)
128     alternate = tonumber(alternate) or 1
129     local crafts = unified_inventory.crafts_table[stack_name]
130     --print(dump(crafts))
131     if next(crafts) == nil then -- No craft recipes
132         return
133     end
134     if alternate < 1 or alternate > #crafts then
135         alternate = 1
136     end
137     local craft = crafts[alternate]
138     inv:set_stack("output", 1, craft.output)
139     local items = craft.items
140
141     if craft.type == "cooking" or
142        craft.type == "fuel" or
143        craft.type == "grinding" or
144        craft.type == "extracting" or
145        craft.type == "compressing" then
146         def = unified_inventory.find_item_def(craft["items"][1])
147         if def then
148             inv:set_stack("build", 1, def)
149         end
150         return
151     end
152     if craft.width == 0 then
153         for i = 1, 3 do
154             if craft.items[i] then
155                 def = unified_inventory.find_item_def(craft.items[i])
156                 if def then
157                     inv:set_stack("build", i, def)
158                 end
159             end
160         end
161     end
162     if craft.width == 1 then
163         local build_table={1, 4, 7}
164         for i = 1, 3 do
165             if craft.items[i] then
166                 def = unified_inventory.find_item_def(craft.items[i])
167                 if def then
168                     inv:set_stack("build", build_table[i], def)
169                 end
170             end
171         end
172     end
173     if craft.width == 2 then
174         local build_table = {1, 2, 4, 5, 7, 8}
175         for i=1, 6 do
176             if craft.items[i] then
177                 def = unified_inventory.find_item_def(craft.items[i])
178                 if def then
179                     inv:set_stack("build", build_table[i], def)
180                 end
181             end
182         end
183     end
184     if craft.width == 3 then
185         for i=1, 9 do
186             if craft.items[i] then
187                 def = unified_inventory.find_item_def(craft.items[i])
188                 if def then
189                     inv:set_stack("build", i, def)
190                 end
191             end
192         end
193     end
194 end
195
196 function unified_inventory.find_item_def(def)
197     if type(def) ~= "string" then
198         return nil
199     end
200     if string.find(def, "group:") then
201         def = string.gsub(def, "group:", "")
202         def = string.gsub(def, "\"", "")
203         if minetest.registered_nodes["default:"..def] then
204             return "default:"..def
205         end
206         local items = unified_inventory.items_in_group(def)
207         return items[1]
208     else
209         return def
210     end
211 end
212
213 function unified_inventory.items_in_group(groups)
214     local items = {}
215     for name, item in pairs(minetest.registered_items) do
216         for _, group in pairs(groups:split(',')) do
217             if item.groups[group] then
218                 table.insert(items, name)
219             end
220         end
221     end
222     return items
223 end