3 item_texture_cache = {},
7 function nametags.get_wield_entity(player)
8 for _, child in pairs(minetest.get_objects_inside_radius(player:get_pos(), 1)) do
9 if child:get_attach() == player then
10 local child_prop = child:get_properties()
11 if child_prop.visual == "wielditem" then
18 function nametags.resize_image(image)
19 return image .. "^[resize:32x32"
22 function nametags.colorize_tiledef(tile, node_color)
23 if tile.name == "" then
27 local color = tile.color or node_color
29 return "(" .. tile.name .. ")^[multiply:" .. minetest.rgba(color.r, color.g, color.b, color.a)
35 function nametags.render_tile(nodedef, n)
36 local img = nametags.colorize_tiledef(nodedef.tiles[n], nodedef.color)
37 local overlay = nametags.colorize_tiledef(nodedef.overlay_tiles[n], nodedef.color)
40 img = img .. "^(" .. overlay .. ")"
46 function nametags.create_item_texture(itemname)
47 local itemdef = minetest.get_item_def(itemname)
48 local nodedef = minetest.get_node_def(itemname)
50 if itemdef.inventory_image ~= "" then
51 return itemdef.inventory_image
53 if nodedef.drawtype == "mesh" or nodedef.drawtype == "nodebox" then
54 return nodedef.tiles[1].name
56 return minetest.inventorycube(
57 nametags.render_tile(nodedef, 1),
58 nametags.render_tile(nodedef, 6),
59 nametags.render_tile(nodedef, 5)
66 function nametags.get_item_texture(itemname)
67 local cached_texture = nametags.item_texture_cache[itemname]
68 if cached_texture then
71 local texture = nametags.create_item_texture(itemname)
72 nametags.item_texture_cache[itemname] = texture
76 function nametags.parse_armor_texture(armor_texture)
77 if armor_texture == "blank.png" then
80 local armor_pieces = {}
81 local armor_textures = armor_texture:sub(2, #armor_texture - 1):split(")^(")
82 for _, texture in ipairs(armor_textures) do
83 local itemname = nametags.armor_cache[texture]
85 local piece_textures = texture:split("^")
86 local enchanted = false
87 if #piece_textures > 1 and piece_textures[2] == "[colorize:purple:50" then
90 local components = (piece_textures[1] or ""):split("_")
91 local modname = table.remove(components, 1) or ""
93 if modname == "mcl" then
94 modname = modname .. "_" .. (table.remove(components, 1) or "")
96 local subname = table.concat(components, "_"):split(".")[1]
97 itemname = modname .. ":" .. subname
99 itemname = itemname .. "_enchanted"
102 nametags.armor_cache[texture] = itemname
104 table.insert(armor_pieces, 1, itemname)
109 function nametags.wield_entity_get_itemname(obj)
110 local prop = obj and obj:get_properties()
111 return prop and prop.textures[1] or ""
114 local blank_resized = nametags.resize_image("blank.png")
116 function nametags.update_nametag(player)
117 if player:is_player() and not player:is_local_player() then
118 local props = player:get_properties()
120 local nametag = props.nametag
121 local hp = player:get_hp()
123 local idx = nametag:find("♥")
126 nametag = nametag:sub(1, idx + 2) .. hp
128 nametag = nametag .. minetest.get_color_escape_sequence("#FF0000") .. " ♥" .. hp
131 player:set_properties({
133 nametag_bgcolor = {a = 128, r = 0, g = 0, b = 0}
136 local cache = nametags.nametag_cache[player]
140 armor_texture = "blank.png",
142 wield_texture = "blank.png",
143 wield_texture_resized = blank_resized,
144 wield_entity_itemname = "",
145 wield_entity = nametags.get_wield_entity(player),
146 nametag_images = {blank_resized},
148 nametags.nametag_cache[player] = cache
151 local wield_texture = props.textures[3]
152 local wield_texture_blank = wield_texture == "blank.png"
153 local wield_entity_itemname = ""
154 local wield_cached = cache.wield_texture == wield_texture
156 if wield_texture_blank then
157 wield_entity_itemname = nametags.wield_entity_get_itemname(cache.wield_entity)
158 wield_cached = wield_cached and cache.wield_entity_itemname == wield_entity_itemname
161 local armor_texture = props.textures[2]
162 local armor_cached = cache.armor_texture == armor_texture
166 if wield_cached and armor_cached then
167 nametag_images = cache.nametag_images
169 local wield_texture_resized
172 wield_texture_resized = cache.wield_texture_resized
174 if wield_texture_blank then
175 if wield_entity_itemname ~= "" then
176 wield_texture = nametags.get_item_texture(wield_entity_itemname) or "blank.png"
179 wield_texture_resized = nametags.resize_image(wield_texture)
180 cache.wield_texture = wield_texture
181 cache.wield_texture_resized = wield_texture_resized
187 armor_textures = cache.armor_textures
190 for _, piece in ipairs(nametags.parse_armor_texture(armor_texture)) do
191 local texture = nametags.get_item_texture(piece)
193 table.insert(armor_textures, nametags.resize_image(texture))
196 cache.armor_texture = armor_texture
197 cache.armor_textures = armor_textures
200 nametag_images = {wield_texture_resized}
201 for _, texture in ipairs(armor_textures) do
202 table.insert(nametag_images, texture)
204 cache.nametag_images = nametag_images
207 player:set_nametag_images(nametag_images)
211 minetest.register_on_object_properties_change(nametags.update_nametag)
212 minetest.register_on_object_hp_change(nametags.update_nametag)
214 minetest.register_on_object_add(function(obj)
215 local attach = obj:get_attach()
216 if attach and attach:is_player() and not attach:is_local_player() then
217 local cache = nametags.nametag_cache[attach]
220 cache.wield_entity = obj