Skip to content

Commit

Permalink
Improve playertag code
Browse files Browse the repository at this point in the history
  • Loading branch information
LoneWolfHT committed May 21, 2024
1 parent d0a5a4f commit 2837285
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions mods/other/playertag/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,56 @@ playertag = {
TYPE_ENTITY = TYPE_ENTITY,
}

local function remove_entity_tag(player)
local tag = players[player:get_player_name()]
if tag then
if tag.entity then
tag.entity.object:remove()
tag.entity = nil
end

if tag.nametag_entity then
tag.nametag_entity.object:remove()
tag.nametag_entity = nil
end

if tag.symbol_entity then
tag.symbol_entity.object:remove()
tag.symbol_entity = nil
end
end
end

local function add_entity_tag(player, old_observers)
local pname = player:get_player_name()
local ppos = player:get_pos()

-- Hide fixed nametag
player:set_nametag_attributes({
color = {a = 0, r = 0, g = 0, b = 0}
})

local ent = minetest.add_entity(player:get_pos(), "playertag:tag")
remove_entity_tag(player)

local ent = minetest.add_entity(ppos, "playertag:tag")
local ent2 = false
local ent3 = false

if not ent then
minetest.after(1, add_entity_tag, player, old_observers)
return
end

if ent.set_observers then
ent2 = minetest.add_entity(player:get_pos(), "playertag:tag")
ent2 = minetest.add_entity(ppos, "playertag:tag")
ent2:set_observers(old_observers.nametag_entity or {})
ent2:set_properties({
nametag = player:get_player_name(),
nametag = pname,
nametag_color = "#EEFFFFDD",
nametag_bgcolor = "#0000002D"
})

ent3 = minetest.add_entity(player:get_pos(), "playertag:tag")
ent3 = minetest.add_entity(ppos, "playertag:tag")
ent3:set_observers(old_observers.symbol_entity or {})
ent3:set_properties({
collisionbox = { 0, 0, 0, 0, 0, 0 },
Expand All @@ -40,9 +70,9 @@ local function add_entity_tag(player, old_observers)

-- Build name from font texture
local texture = "npcf_tag_bg.png"
local x = math.floor(134 - ((player:get_player_name():len() * 11) / 2))
local x = math.floor(134 - ((pname:len() * 11) / 2))
local i = 0
player:get_player_name():gsub(".", function(char)
pname:gsub(".", function(char)
local n = "_"
if char:byte() > 96 and char:byte() < 123 or char:byte() > 47 and char:byte() < 58 or char == "-" then
n = char
Expand All @@ -64,24 +94,9 @@ local function add_entity_tag(player, old_observers)
end

-- Store
players[player:get_player_name()].entity = ent:get_luaentity()
players[player:get_player_name()].nametag_entity = ent2 and ent2:get_luaentity()
players[player:get_player_name()].symbol_entity = ent3 and ent3:get_luaentity()
end

local function remove_entity_tag(player)
local tag = players[player:get_player_name()]
if tag and tag.entity then
tag.entity.object:remove()

if tag.nametag_entity then
tag.nametag_entity.object:remove()
end

if tag.symbol_entity then
tag.symbol_entity.object:remove()
end
end
players[pname].entity = ent:get_luaentity()
players[pname].nametag_entity = ent2 and ent2:get_luaentity()
players[pname].symbol_entity = ent3 and ent3:get_luaentity()
end

local function update(player, settings)
Expand Down

0 comments on commit 2837285

Please sign in to comment.