diff --git a/mods/show_wielded_item/README.md b/mods/show_wielded_item/README.md index 6c775f3..4e9a58d 100644 --- a/mods/show_wielded_item/README.md +++ b/mods/show_wielded_item/README.md @@ -1,11 +1,24 @@ # Show Wielded Item [`show_wielded_item`] -This Luanti mod displays the name of the wielded item above the hotbar and +This mod displays the name of the wielded item above the hotbar and statbars. This mod is compatible with the HUD Bars [`hudbars`] mod. -Compability with other HUD-related mods is possible, but not guaranteed. +This mod *disables* itself if Unified Inventory is detected to show item names. +Compatibility with other HUD-related mods is possible, but not guaranteed. -Version: 1.0.0 +Version: 1.2.1 + +## A note for Unified Inventory users + +The mod Unified Inventory adds its own wielded item display (as of 16/08/2024). +So if you use that mod and have the item names features of that mod +enabled, then Unified Inventory takes precedence. + +If the Unified Inventory mod was detected, and the setting +`unified_inventory_item_names` is set to `true`, then +`show_wielded_item` won’t do anything and let Unified Inventory +display the wielded item instead. A message will appear in +the debug log if this happens. ## Credits Released by Wuzzy. diff --git a/mods/show_wielded_item/depends.txt b/mods/show_wielded_item/depends.txt new file mode 100644 index 0000000..f08c167 --- /dev/null +++ b/mods/show_wielded_item/depends.txt @@ -0,0 +1,2 @@ +hudbars? +unified_inventory? diff --git a/mods/show_wielded_item/init.lua b/mods/show_wielded_item/init.lua index 3a30efa..061af87 100644 --- a/mods/show_wielded_item/init.lua +++ b/mods/show_wielded_item/init.lua @@ -7,6 +7,23 @@ local dtimes = {} local dlimit = 3 -- HUD element will be hidden after this many seconds local hudbars_mod = minetest.get_modpath("hudbars") +local unified_inventory_mod = minetest.get_modpath("unified_inventory") + +-- Legacy support: Name of the HUD type field for 'hud_add'. +local hud_type_field_name +if minetest.features.hud_def_type_field then + -- engine version 5.9.0 and later + hud_type_field_name = "type" +else + -- All engine versions before 5.9.0 + hud_type_field_name = "hud_elem_type" +end + +-- Disable mod if Unified Inventory item names feature is enabled +if unified_inventory_mod and minetest.settings:get_bool("unified_inventory_item_names") ~= false then + minetest.log("action", "[show_wielded_item] Unified Inventory's item names feature was detected! Running show_wielded_item is pointless now, so it won't do anything") + return +end local function set_hud(player) if not player:is_player() then return end @@ -36,12 +53,13 @@ local function set_hud(player) end huds[player_name] = player:hud_add({ - hud_elem_type = "text", + [hud_type_field_name] = "text", position = {x=0.5, y=1}, offset = off, alignment = {x=0, y=0}, number = 0xFFFFFF , text = "", + z_index = 100, }) end @@ -59,6 +77,15 @@ minetest.register_on_leaveplayer(function(player) wieldindex[name] = nil end) +local function get_first_line(text) + -- Cut off text after first newline + local firstnewline = string.find(text, "\n") + if firstnewline then + text = string.sub(text, 1, firstnewline-1) + end + return text +end + minetest.register_globalstep(function(dtime) for _, player in pairs(minetest.get_connected_players()) do local player_name = player:get_player_name() @@ -81,26 +108,43 @@ minetest.register_globalstep(function(dtime) if huds[player_name] then + -- Get description (various fallback checks for old Luanti/Minetest versions) local def = minetest.registered_items[wname] - local meta = wstack:get_meta() - - --[[ Get description. Order of preference: - * description from metadata - * description from item definition - * itemstring ]] - local desc = meta:get_string("description") - if (desc == nil or desc == "") and def then - desc = def.description + local desc + if wstack.get_short_description then + -- get_short_description() + desc = wstack:get_short_description() end - if desc == nil or desc == "" then + if (not desc or desc == "") and wstack.get_description then + -- get_description() + desc = wstack:get_description() + desc = get_first_line(desc) + end + if (not desc or desc == "") and not wstack.get_description then + -- Metadata (old versions only) + local meta = wstack:get_meta() + desc = meta:get_string("description") + desc = get_first_line(desc) + end + if not desc or desc == "" then + -- Item definition + desc = def.description + desc = get_first_line(desc) + end + if not desc or desc == "" then + -- Final fallback: itemstring desc = wname end - -- Cut off item description after first newline - local firstnewline = string.find(desc, "\n") - if firstnewline then - desc = string.sub(desc, 1, firstnewline-1) + + -- Print description + if desc then + -- Optionally append the 'technical' itemname + local tech = minetest.settings:get_bool("show_wielded_item_itemname", false) + if tech and desc ~= "" then + desc = desc .. " ["..wname.."]" + end + player:hud_change(huds[player_name], 'text', desc) end - player:hud_change(huds[player_name], 'text', desc) end end end diff --git a/mods/show_wielded_item/locale/show_wielded_item.de.tr b/mods/show_wielded_item/locale/show_wielded_item.de.tr new file mode 100644 index 0000000..3106b17 --- /dev/null +++ b/mods/show_wielded_item/locale/show_wielded_item.de.tr @@ -0,0 +1,4 @@ +# textdomain: show_wielded_item +Show Wielded Item=Getragenen Gegenstand anzeigen +Displays the name of the wielded item.=Zeigt den Namen des getragenen Gegenstands an. + diff --git a/mods/show_wielded_item/locale/template.txt b/mods/show_wielded_item/locale/template.txt new file mode 100644 index 0000000..fbc893d --- /dev/null +++ b/mods/show_wielded_item/locale/template.txt @@ -0,0 +1,4 @@ +# textdomain: show_wielded_item +Show Wielded Item= +Displays the name of the wielded item.= + diff --git a/mods/show_wielded_item/mod.conf b/mods/show_wielded_item/mod.conf index 36bd5d1..bb10b70 100644 --- a/mods/show_wielded_item/mod.conf +++ b/mods/show_wielded_item/mod.conf @@ -1,3 +1,4 @@ name = show_wielded_item +title = Show Wielded Item description = Displays the name of the wielded item. -optional_depends = hudbars +optional_depends = hudbars, unified_inventory diff --git a/mods/show_wielded_item/settingtypes.txt b/mods/show_wielded_item/settingtypes.txt index 3f53edd..05abcf2 100644 --- a/mods/show_wielded_item/settingtypes.txt +++ b/mods/show_wielded_item/settingtypes.txt @@ -1,3 +1,6 @@ +#If true, also append the 'technical' itemname. +show_wielded_item_itemname (Show technical itemname) bool false + #Use this setting to manually set the vertical offset of the label which shows #the name of the wielded item. The offset is in pixels from the bottom of the #screen.