From c6ffccfef5f9401176014692f07840d283fdce3c Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Fri, 9 Apr 2021 13:35:58 +0200 Subject: [PATCH] Add priority to bossbars; display bossbars of closest bosses first --- mods/HUD/mcl_bossbars/init.lua | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/mods/HUD/mcl_bossbars/init.lua b/mods/HUD/mcl_bossbars/init.lua index 3ab1a83c7c..a95d533ce1 100644 --- a/mods/HUD/mcl_bossbars/init.lua +++ b/mods/HUD/mcl_bossbars/init.lua @@ -36,12 +36,12 @@ end local last_id = 0 -function mcl_bossbars.add_bar(player, def) +function mcl_bossbars.add_bar(player, def, dynamic, priority) local name = player:get_player_name() local bars = mcl_bossbars.bars[name] - local bar = {text = def.text} + local bar = {text = def.text, priority = priority or 0} bar.color, bar.image = get_color_info(def.color, def.percentage) - if def.dynamic then + if dynamic then for _, other in pairs(bars) do if not other.id and other.color == bar.color and (other.original_text or other.text) == bar.text and other.image == bar.image then if not other.count then @@ -55,7 +55,7 @@ function mcl_bossbars.add_bar(player, def) end end table.insert(bars, bar) - if not def.dynamic then + if not dynamic then bar.raw_color = def.color bar.id = last_id + 1 last_id = bar.id @@ -69,10 +69,11 @@ function mcl_bossbars.remove_bar(id) mcl_bossbars.static[id] = nil end -function mcl_bossbars.update_bar(id, def) +function mcl_bossbars.update_bar(id, def, priority) local old = mcl_bossbars.static[id] old.color = get_color_info(def.color or old.raw_color, def.percentage or old.percentage) old.text = def.text or old.text + old.priority = priority or old.priority end function mcl_bossbars.update_boss(luaentity, name, color) @@ -81,15 +82,15 @@ function mcl_bossbars.update_boss(luaentity, name, color) text = luaentity.nametag, percentage = math.floor(luaentity.health / luaentity.hp_max * 100), color = color, - dynamic = true, } if not bardef.text or bardef.text == "" then bardef.text = name end local pos = object:get_pos() for _, player in pairs(minetest.get_connected_players()) do - if vector.distance(pos, player:get_pos()) <= 80 then - mcl_bossbars.add_bar(player, bardef) + local d = vector.distance(pos, player:get_pos()) + if d <= 80 then + mcl_bossbars.add_bar(player, bardef, true, d) end end end @@ -116,6 +117,7 @@ minetest.register_globalstep(function() local name = player:get_player_name() local bars = mcl_bossbars.bars[name] local huds = mcl_bossbars.huds[name] + table.sort(bars, function(a, b) return a.priority < b.priority end) local huds_new = {} local bars_new = {} local i = 0