Skip to content
Permalink
Browse files

Improve creative inventory search (#2523)

  • Loading branch information
sfan5 committed Oct 22, 2019
1 parent 7caa5d3 commit f9a9e87af6bc4f9a553b3b7aaa19234b214baf2c
Showing with 18 additions and 4 deletions.
  1. +18 −4 mods/creative/inventory.lua
@@ -59,22 +59,36 @@ function creative.init_creative_inventory(player)
return player_inventory[player_name]
end

local function match(s, filter)
if filter == "" then
return 0
end
if s:lower():find(filter, 1, true) then
return #s - #filter
end
return nil
end

function creative.update_creative_inventory(player_name, tab_content)
local creative_list = {}
local inv = player_inventory[player_name] or
creative.init_creative_inventory(minetest.get_player_by_name(player_name))
local player_inv = minetest.get_inventory({type = "detached", name = "creative_" .. player_name})

local items = inventory_cache[tab_content] or init_creative_cache(tab_content)

local creative_list = {}
local order = {}
for name, def in pairs(items) do
if def.name:find(inv.filter, 1, true) or
def.description:lower():find(inv.filter, 1, true) then
local m = match(def.description, inv.filter) or match(def.name, inv.filter)
if m then
creative_list[#creative_list+1] = name
-- Sort by description length first so closer matches appear earlier
order[name] = string.format("%02d", m) .. name
end
end

table.sort(creative_list)
table.sort(creative_list, function(a, b) return order[a] < order[b] end)

player_inv:set_size("main", #creative_list)
player_inv:set_list("main", creative_list)
inv.size = #creative_list

0 comments on commit f9a9e87

Please sign in to comment.
You can’t perform that action at this time.