search function consolidated in creative and crafting
This commit is contained in:
parent
58968f02ce
commit
6826612712
31
crafting.lua
31
crafting.lua
@ -366,36 +366,7 @@ local function crafting_callback(state)
|
||||
state:onInput(function(state, fields, player)
|
||||
local search_string = state:get("search"):getText()
|
||||
if search_string ~= "" and search_string ~= state.param.survival_search_string then
|
||||
local filtered_list = {}
|
||||
local revealed_list = cache.get_revealed_items(player)
|
||||
state.param.survival_search_string = search_string:lower()
|
||||
for _, entry in ipairs(revealed_list) do
|
||||
local def = minetest.registered_items[entry.item]
|
||||
if string.find(def.description:lower(), search_string) or
|
||||
string.find(def.name:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
else
|
||||
for _, cgroup in pairs(entry.citem.cgroups) do
|
||||
if string.find(cgroup.name:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if smart_inventory.doc_items_mod then
|
||||
for _, entry in ipairs(filtered_list) do
|
||||
if entry.recipes then
|
||||
local valid_recipes = {}
|
||||
for _, recipe in ipairs(entry.recipes) do
|
||||
if cache.crecipes[recipe]:is_revealed(player) then
|
||||
table.insert(valid_recipes, recipe)
|
||||
end
|
||||
end
|
||||
entry.recipes = valid_recipes
|
||||
end
|
||||
end
|
||||
end
|
||||
local filtered_list = ui_tools.search_in_list(cache.get_revealed_items(player), search_string)
|
||||
state.param.crafting_grouped_items = cache.get_list_grouped(filtered_list)
|
||||
update_group_selection(state, true)
|
||||
end
|
||||
|
34
creative.lua
34
creative.lua
@ -132,39 +132,9 @@ local function creative_callback(state)
|
||||
state:onInput(function(state, fields, player)
|
||||
local search_string = state:get("search"):getText()
|
||||
if search_string ~= (state.param.creative_search_string or "") then
|
||||
local filtered_list = {}
|
||||
state.param.creative_search_string = search_string:lower()
|
||||
for _, entry in ipairs(state.param.creative_grouped_items_all) do
|
||||
local def = minetest.registered_items[entry.item]
|
||||
if string.find(def.description:lower(), search_string) or
|
||||
string.find(def.name:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
else
|
||||
for _, cgroup in pairs(entry.citem.cgroups) do
|
||||
if string.find(cgroup.name:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_all, search_string)
|
||||
state.param.creative_grouped_items = cache.get_list_grouped(filtered_list)
|
||||
|
||||
filtered_list = {}
|
||||
for _, entry in ipairs(state.param.creative_grouped_items_material_all) do
|
||||
local def = minetest.registered_items[entry.item]
|
||||
if string.find(def.description, search_string) or
|
||||
string.find(def.name, search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
else
|
||||
for _, cgroup in ipairs(entry.citem.cgroups) do
|
||||
if string.find(cgroup.name, search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
filtered_list = ui_tools.search_in_list(state.param.creative_grouped_items_material_all, search_string)
|
||||
state.param.creative_grouped_material_items = filtered_list
|
||||
update_group_selection(state, 0)
|
||||
end
|
||||
|
36
ui_tools.lua
36
ui_tools.lua
@ -12,7 +12,6 @@ local cache = smart_inventory.cache
|
||||
-- Return: updated groups_tab
|
||||
|
||||
function ui_tools.update_group_selection(grouped, groups_sel, groups_tab)
|
||||
|
||||
-- save old selection
|
||||
local sel_id = groups_sel:getSelected()
|
||||
local sel_grp
|
||||
@ -59,7 +58,6 @@ function ui_tools.update_group_selection(grouped, groups_sel, groups_tab)
|
||||
return groups_tab
|
||||
end
|
||||
|
||||
|
||||
function ui_tools.create_trash_inv(state, name)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
local invname = name.."_trash_inv"
|
||||
@ -91,5 +89,39 @@ function ui_tools.create_trash_inv(state, name)
|
||||
inv:set_size(listname, 1)
|
||||
end
|
||||
|
||||
function ui_tools.search_in_list(list, search_string)
|
||||
local filtered_list = {}
|
||||
search_string = search_string:lower()
|
||||
for _, entry in ipairs(list) do
|
||||
local def = minetest.registered_items[entry.item]
|
||||
if string.find(def.description:lower(), search_string) or
|
||||
string.find(def.name:lower(), search_string) then
|
||||
table.insert(filtered_list, entry)
|
||||
else
|
||||
for _, cgroup in pairs(entry.citem.cgroups) do
|
||||
local prefix_end_pos = cgroup.name:find(":")
|
||||
if string.find(cgroup.name:lower(), search_string, prefix_end_pos) then
|
||||
table.insert(filtered_list, entry)
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if smart_inventory.doc_items_mod then
|
||||
for _, entry in ipairs(filtered_list) do
|
||||
if entry.recipes then
|
||||
local valid_recipes = {}
|
||||
for _, recipe in ipairs(entry.recipes) do
|
||||
if cache.crecipes[recipe]:is_revealed(player) then
|
||||
table.insert(valid_recipes, recipe)
|
||||
end
|
||||
end
|
||||
entry.recipes = valid_recipes
|
||||
end
|
||||
end
|
||||
end
|
||||
return filtered_list
|
||||
end
|
||||
|
||||
--------------------------------
|
||||
return ui_tools
|
||||
|
Loading…
x
Reference in New Issue
Block a user