Skip to content

Commit b1ab8d5

Browse files
authoredJun 5, 2020
Implement searching for translated names in creative inventory (#2675)
1 parent 6e345cf commit b1ab8d5

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed
 

Diff for: ‎mods/creative/inventory.lua

+26-4
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,23 @@ function creative.init_creative_inventory(player)
6161
return player_inventory[player_name]
6262
end
6363

64+
local NO_MATCH = 999
6465
local function match(s, filter)
6566
if filter == "" then
6667
return 0
6768
end
6869
if s:lower():find(filter, 1, true) then
6970
return #s - #filter
7071
end
71-
return nil
72+
return NO_MATCH
73+
end
74+
75+
local function description(def, lang_code)
76+
local s = def.description
77+
if lang_code then
78+
s = minetest.get_translated_string(lang_code, s)
79+
end
80+
return s:gsub("\n.*", "") -- First line only
7281
end
7382

7483
function creative.update_creative_inventory(player_name, tab_content)
@@ -84,13 +93,26 @@ function creative.update_creative_inventory(player_name, tab_content)
8493

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

96+
local lang
97+
local player_info = minetest.get_player_information(player_name)
98+
if player_info and player_info.lang_code ~= "" then
99+
lang = player_info.lang_code
100+
end
101+
87102
local creative_list = {}
88103
local order = {}
89104
for name, def in pairs(items) do
90-
local m = match(def.description, inv.filter) or match(def.name, inv.filter)
91-
if m then
105+
local m = match(description(def), inv.filter)
106+
if m > 0 then
107+
m = math.min(m, match(description(def, lang), inv.filter))
108+
end
109+
if m > 0 then
110+
m = math.min(m, match(name, inv.filter))
111+
end
112+
113+
if m < NO_MATCH then
92114
creative_list[#creative_list+1] = name
93-
-- Sort by description length first so closer matches appear earlier
115+
-- Sort by match value first so closer matches appear earlier
94116
order[name] = string.format("%02d", m) .. name
95117
end
96118
end

0 commit comments

Comments
 (0)