Add unified_inventory_enable_item_names setting (#235)

Show only names settings: in case of multi-line descriptions, only the first line is shown
Shorten long descriptions: Removes all characters after x index, and add `[...]`
Max length before truncation: x for Shorten long descriptions
Notes: If Max length == 0, then item names won't be displayed
This commit is contained in:
fmmaks 2023-10-15 22:02:35 +03:00 committed by GitHub
parent 5d233a0f0a
commit 43c9b50800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

@ -191,7 +191,8 @@ dofile(modpath.."/register.lua")
if minetest.settings:get_bool("unified_inventory_bags") ~= false then if minetest.settings:get_bool("unified_inventory_bags") ~= false then
dofile(modpath.."/bags.lua") dofile(modpath.."/bags.lua")
end end
if minetest.settings:get_bool("unified_inventory_item_names") ~= false then
dofile(modpath.."/item_names.lua") dofile(modpath.."/item_names.lua")
end
dofile(modpath.."/waypoints.lua") dofile(modpath.."/waypoints.lua")
dofile(modpath.."/legacy.lua") -- mod compatibility dofile(modpath.."/legacy.lua") -- mod compatibility

@ -3,6 +3,8 @@
local item_names = {} -- [player_name] = { hud, dtime, itemname } local item_names = {} -- [player_name] = { hud, dtime, itemname }
local dlimit = 3 -- HUD element will be hidden after this many seconds local dlimit = 3 -- HUD element will be hidden after this many seconds
local hudbars_mod = minetest.get_modpath("hudbars") local hudbars_mod = minetest.get_modpath("hudbars")
local only_names = minetest.settings:get_bool("unified_inventory_only_names", true)
local max_length = tonumber(minetest.settings:get("unified_inventory_max_item_name_length")) or 80
local function set_hud(player) local function set_hud(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
@ -60,6 +62,7 @@ minetest.register_globalstep(function(dtime)
data.itemname = itemname data.itemname = itemname
data.index = index data.index = index
data.dtime = 0 data.dtime = 0
local lang_code = minetest.get_player_information(player:get_player_name()).lang_code
local desc = stack.get_meta local desc = stack.get_meta
and stack:get_meta():get_string("description") and stack:get_meta():get_string("description")
@ -69,6 +72,14 @@ minetest.register_globalstep(function(dtime)
local def = minetest.registered_items[itemname] local def = minetest.registered_items[itemname]
desc = def and def.description or "" desc = def and def.description or ""
end end
if only_names and desc and string.find(desc, "\n") then
desc = string.match(desc, "([^\n]*)")
end
desc = minetest.get_translated_string(lang_code, desc)
desc = minetest.strip_colors(desc)
if string.len(desc) > max_length and max_length > 0 then
desc = string.sub(desc, 1, max_length) .. " [...]"
end
player:hud_change(data.hud, 'text', desc) player:hud_change(data.hud, 'text', desc)
end end
end end

@ -15,3 +15,9 @@ unified_inventory_hide_disabled_buttons (Hide disabled buttons) bool false
unified_inventory_automatic_categorization (Items automatically added to categories) bool true unified_inventory_automatic_categorization (Items automatically added to categories) bool true
unified_inventory_item_names (Item names are shown above hotbar) bool true
unified_inventory_only_names (Show only item name) bool true
unified_inventory_max_item_name_length (Maximum length of an item name before it's truncated, 0 disables option) int 80