Files
ShaguTweaks-ClassicAPI/mods/xpbar-text.lua
T
2026-08-30 16:04:14 +02:00

81 lines
2.5 KiB
Lua

local T = ShaguTweaks.T
local module = ShaguTweaks:register({
title = T["XP Bar Text"],
description = T["Always shows current XP and rested bonus percentage directly on the experience bar."],
expansions = { ["vanilla"] = true },
category = T["Interface"],
enabled = true,
order = 70,
})
module.enable = function(self)
-- Reuse the overlay if enable() is called again instead of stacking another
-- frame and another event handler on top of the experience bar.
if not self.exp then
self.exp = CreateFrame("Frame", nil, UIParent)
self.exp:SetAllPoints(MainMenuExpBar)
self.exp:SetFrameStrata("HIGH")
self.exp.text = self.exp:CreateFontString(nil, "OVERLAY", "GameFontWhite")
self.exp.text:SetFont("Fonts\\ARIALN.TTF", 14, "OUTLINE")
self.exp.text:SetPoint("CENTER", MainMenuExpBar, "CENTER", 0, 1)
self.exp.text:SetJustifyH("CENTER")
self.exp.text:SetTextColor(1, 1, 1)
end
local exp = self.exp
local function UpdateXPText()
local curr = UnitXP("player")
local max = UnitXPMax("player")
if not max or max == 0 then
if exp.text:IsShown() then exp.text:Hide() end
self.lastText = nil
return
end
local rest = GetXPExhaustion() or 0
local xpPct = math.floor(curr / max * 100)
local text
if rest > 0 then
-- Turtle WoW 1.18.1 reduced rested creature XP from +100% to +50%.
-- The effective GetXPExhaustion full-rest threshold is about 112.5% of
-- the current level XP, so normalize that pool to a 0-100% display.
local restPct = math.floor(math.min(rest / (max * 1.125) * 100, 100))
text = "|cffffffff" .. xpPct .. "%|r |cffaaaaaa(|cffa78aca" .. restPct .. "%|cffaaaaaa)|r"
else
text = "|cffffffff" .. xpPct .. "%|r"
end
if self.lastText ~= text then
exp.text:SetText(text)
self.lastText = text
end
if not exp.text:IsShown() then exp.text:Show() end
end
if not self.events then
self.events = CreateFrame("Frame", nil, UIParent)
self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
self.events:RegisterEvent("PLAYER_XP_UPDATE")
self.events:RegisterEvent("UPDATE_EXHAUSTION")
self.events:RegisterEvent("PLAYER_LEVEL_UP")
end
self.events:SetScript("OnEvent", function()
if event == "PLAYER_ENTERING_WORLD" then
-- Hide Blizzard's default overlay that renders the original XP text.
MainMenuBarOverlayFrame:Hide()
end
UpdateXPText()
end)
-- Refresh immediately in case the module is enabled after entering the world.
MainMenuBarOverlayFrame:Hide()
UpdateXPText()
end