From 586e3efac45c72b266c7ff0020effa298f1bf3c5 Mon Sep 17 00:00:00 2001 From: Dusk-92 Date: Mon, 24 Aug 2026 19:13:12 +0200 Subject: [PATCH] Cache exact nameplate class colors from ClassicAPI --- mods/nameplate-classcolor.lua | 63 ++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/mods/nameplate-classcolor.lua b/mods/nameplate-classcolor.lua index e5f405f..d6041d6 100644 --- a/mods/nameplate-classcolor.lua +++ b/mods/nameplate-classcolor.lua @@ -1,6 +1,7 @@ local _G = ShaguTweaks.GetGlobalEnv() local T = ShaguTweaks.T local GetUnitData = ShaguTweaks.GetUnitData +local API = ShaguTweaks.API local module = ShaguTweaks:register({ title = T["Nameplate Class Colors"], @@ -13,9 +14,69 @@ local module = ShaguTweaks:register({ module.enable = function(self) if ShaguPlates then return end + local colorsByName = {} + local useClassicTokens = API and API.eventutils and API.GetNamePlateForUnit + and _G.C_EventUtils + and _G.C_EventUtils.IsEventValid("NAME_PLATE_UNIT_ADDED") + and _G.C_EventUtils.IsEventValid("NAME_PLATE_UNIT_REMOVED") + + if useClassicTokens then + local listener = CreateFrame("Frame") + listener:RegisterEvent("NAME_PLATE_UNIT_ADDED") + listener:RegisterEvent("NAME_PLATE_UNIT_REMOVED") + listener:SetScript("OnEvent", function() + local unit = arg1 + if not unit then return end + + local plate = API.GetNamePlateForUnit(unit) + if event == "NAME_PLATE_UNIT_ADDED" then + if not plate then return end + + local color + if UnitIsPlayer(unit) then + local _, class = UnitClass(unit) + color = class and RAID_CLASS_COLORS[class] or nil + end + + -- Cache the exact unit result once. The native nameplate may repaint + -- its reaction color later, so the lightweight OnUpdate below only + -- reapplies this cached color and performs no unit/name scan. + plate.stClassColor = color or false + plate.stClassUnit = unit + + local name = UnitName(unit) + if name then colorsByName[name] = color or false end + elseif event == "NAME_PLATE_UNIT_REMOVED" then + -- ClassicAPI keeps the token resolvable during REMOVED. Clear fields + -- before the native frame can be recycled for another unit. + if plate and plate.stClassUnit == unit then + plate.stClassColor = nil + plate.stClassUnit = nil + end + end + end) + end + table.insert(ShaguTweaks.libnameplate.OnUpdate, function() + if useClassicTokens then + local color = this.stClassColor + + -- The ClassicAPI wrapper returned by GetNamePlateForUnit normally is + -- the same Lua frame. Keep a name-keyed cache as a harmless fallback + -- for wrapper recreation without falling back to active unit scanning. + if color == nil and this.name then + color = colorsByName[this.name:GetText()] + end + + if color then + this.healthbar:SetStatusBarColor(color.r, color.g, color.b, 1) + end + return + end + + -- Plain Vanilla / old ClassicAPI compatibility path. local name = this.name:GetText() - local class, _, elite, player = GetUnitData(name, true) + local class, _, _, player = GetUnitData(name, true) if class and player then local color = RAID_CLASS_COLORS[class] or { r = .5, g = .5, b = .5, a = 1 } this.healthbar:SetStatusBarColor(color.r, color.g, color.b, 1)