Added new fixed nametags mod

This commit is contained in:
Billy S 2019-04-21 21:05:11 -04:00
parent 59a676458b
commit f3729bd03f
3 changed files with 131 additions and 3 deletions

40
mods/nametags/init.lua Normal file
View File

@ -0,0 +1,40 @@
local nametag_scale = 0.2
minetest.register_entity("nametags:nametag", {
hp_max = 1,
physical = false,
weight = 0,
visual = "sprite",
selectionbox = {0, 0, 0, 0, 0, 0},
collisionbox = {0, 0, 0, 0, 0, 0},
on_activate = function(e, sdata)
-- Check if valid sdata was given
if sdata == nil or sdata == "" then
e.object:remove()
return
end
-- Check if player is online
local pobj = minetest.get_player_by_name(sdata)
if pobj == nil then
e.object:remove()
end
-- Finalize nametag
local tex, xsize = signs_lib.make_line_texture({sdata})
e._pname = sdata
e.object:set_properties({
textures = {tex},
visual_size = {x = xsize * nametag_scale, y = nametag_scale},
})
e.object:set_attach(pobj, "", {x = 0, y = 20, z = 0}, {x = 0, y = 0, z = 0})
end,
on_step = function(e)
if minetest.get_player_by_name(e._pname) == nil then
e.object:remove()
end
end
})
minetest.register_on_joinplayer(function(pobj)
minetest.add_entity(pobj:get_pos(), "nametags:nametag", pobj:get_player_name())
pobj:set_nametag_attributes({text = " "})
end)

2
mods/nametags/mod.conf Normal file
View File

@ -0,0 +1,2 @@
name = nametags
depends = signs_lib

View File

@ -311,8 +311,10 @@ local function make_line_texture(line, lineno, pos)
local maxw = 0
local words = { }
local n = minetest.registered_nodes[minetest.get_node(pos).name]
local default_color = n.default_color or 0
local default_color
local n = minetest.registered_nodes[minetest.get_node(pos).name]
default_color = n.default_color or 0
local cur_color = tonumber(default_color, 16)
@ -487,7 +489,7 @@ signs_lib.update_sign = function(pos, fields, owner)
meta:set_string("infotext", ownstr..string.gsub(make_infotext(fields.text), "@KEYWORD", current_keyword).." ")
meta:set_string("text", fields.text)
meta:set_int("__signslib_new_format", 1)
new = true
else
@ -1157,3 +1159,87 @@ minetest.register_craft( {
if minetest.setting_get("log_mods") then
minetest.log("action", S("signs loaded"))
end
-- Added by BillyS
-- Useful function for the nametag mod
function signs_lib.make_line_texture(line)
local width = 0
local maxw = 0
local words = { }
local cur_color = 15
local lineno = 0
-- We check which chars are available here.
for word_i, word in ipairs(line) do
local chars = { }
local ch_offs = 0
local word_l = #word
local i = 1
while i <= word_l do
local c = word:sub(i, i)
if c == "#" then
local cc = tonumber(word:sub(i+1, i+1), 16)
if cc then
i = i + 1
cur_color = cc
end
else
local w = charwidth[c]
if w then
width = width + w
maxw = math_max(width, maxw)
if #chars < MAX_INPUT_CHARS then
table.insert(chars, {
off=ch_offs,
tex=FONT_FMT_SIMPLE:format(c:byte()),
col=("%X"):format(cur_color),
})
end
ch_offs = ch_offs + w
end
end
i = i + 1
end
table.insert(words, { chars=chars, w=ch_offs })
end
-- Okay, we actually build the "line texture" here.
local texture = { }
--local start_xpos = math.floor((SIGN_WIDTH - maxw) / 2)
local start_xpos = 0
local xpos = 0
local ypos = 0
cur_color = nil
for word_i, word in ipairs(words) do
local xoffs = (xpos - start_xpos)
if (xoffs > 0) and ((xoffs + word.w) > maxw) then
table.insert(texture, fill_line(xpos, ypos, maxw, "n"))
xpos = start_xpos
ypos = ypos + LINE_HEIGHT
lineno = lineno + 1
if lineno >= NUMBER_OF_LINES then break end
table.insert(texture, fill_line(xpos, ypos, maxw, cur_color))
end
for ch_i, ch in ipairs(word.chars) do
if ch.col ~= cur_color then
cur_color = ch.col
table.insert(texture, fill_line(xpos + ch.off, ypos, maxw, cur_color))
end
table.insert(texture, (":%d,%d=%s"):format(xpos + ch.off, ypos, ch.tex))
end
table.insert(texture, (":%d,%d=hdf_20.png"):format(xpos + word.w, ypos))
xpos = xpos + word.w + charwidth[" "]
if xpos >= (SIGN_WIDTH + charwidth[" "]) then break end
end
table.insert(texture, fill_line(xpos, ypos, maxw, "n"))
table.insert(texture, fill_line(start_xpos, ypos + LINE_HEIGHT, maxw, "n"))
return ("[combine:%dx%d]"):format(width, LINE_HEIGHT) .. table.concat(texture) .. "^[makealpha:0,0,0", width / LINE_HEIGHT
end