Files
pfUI/modules/unitxp.lua
T
roby-brok 69d778d6d6 Fix two dangling skin includes and four error paths (#39)
* skins: drop includes for two files that were never committed

init/skins.xml referenced custom_merchant.lua and arena_score.lua, neither of
which is tracked in git. Every install -- release zips included, since the
release workflow packages the repo -- throws two 'Error loading' lines at
login and ships without those two skins.

(cherry picked from commit b338b4a16d0def4ae89fde5c6026e58a796b8c00)

* map: don't re-anchor the world map to the frame GetPoint returned

Ctrl+scroll rescales the map and repositions it to keep the top-left fixed, but
it re-anchored using the relative frame GetPoint handed back. Once anything else
is anchored to WorldMapFrame that throws

  WorldMapFrame:SetPoint(): <unnamed> is dependent on this

and the error aborts the rest of the zoom handler, so SetScale never runs.

Anchor to the parent instead. That is what the rest of pfUI's movable system
already assumes -- LoadMovable uses the 3-arg form and SaveMovable stores only
xpos/ypos with no relative frame.

(cherry picked from commit bf055d87fc0a04ac912c13fa874a4331f47bdfa8)

* firstrun: return after bailing on a disabled chat module

All three chat setup steps printed 'Chat module is disabled' and then carried on
into the nil pfUI.chat they had just tested for.

(cherry picked from commit 1af427e3b38bb7c13f645ae7a48b8ade3e7455a9)

* unitxp: stop the free-frame distance poller on logout

The PLAYER_LOGOUT handler stops the indicators to avoid the UnitXP crash on
exit, but in free-frame distance mode the polling runs on a separate scanner
frame that was never exposed, so the handler could not reach it and its
OnUpdate kept calling into UnitXP during teardown.

Exposes the frame as pfUI.uf.target.distanceScanner and stops it alongside the
others.

* roll: bail out on an item the client has not cached

C_Item.GetItemInfo returns nil for an item that is not cached yet, and the next
line assigns pfUI.roll.cache[itemName], which throws "table index is nil" on a
nil key. Easy to hit on a fresh login when someone rolls on an item you have
never seen.

* cooldown: return after hiding on a nil parent

Without the return it falls straight through to parent:GetName() on the nil it
just tested for.
2026-08-10 01:00:47 -05:00

377 lines
13 KiB
Lua

-- UnitXP_SP3 integration module
-- Provides Line of Sight indicator, OS notifications, and enhanced targeting
-- Requires UnitXP_SP3 DLL: https://github.com/allfoxwy/UnitXP_SP3
pfUI:RegisterModule("unitxp", function ()
-- Check if UnitXP is available
local hasUnitXP = pcall(UnitXP, "nop", "nop")
if not hasUnitXP then return end
local rawborder, border = GetBorderSize()
-- Helper to create indicators after target frame exists
local function CreateTargetIndicators()
if not pfUI.uf or not pfUI.uf.target then return false end
local h = pfUI.uf.target:GetHeight() or 30
local slot = h / 3 -- divide frame into thirds
local fontSize = tonumber(C.unitframes.unitxp_font_size) or (C.global.font_size + 2)
local suffix = (C.unitframes.hide_distance_yd == "1") and "" or " yd"
-- Behind Indicator for all units (MIDDLE)
if C.unitframes.behind_indicator == "1" and not pfUI.uf.target.behindIndicator then
local behindFrame = CreateFrame("Frame", "pfBehindIndicator", pfUI.uf.target)
behindFrame:SetAllPoints(pfUI.uf.target)
behindFrame:SetFrameLevel(pfUI.uf.target:GetFrameLevel() + 10)
behindFrame.text = behindFrame:CreateFontString(nil, "OVERLAY")
behindFrame.text:SetFont(pfUI.font_default, fontSize, "OUTLINE")
behindFrame.text:SetPoint("RIGHT", behindFrame, "RIGHT", -1, 0)
behindFrame.text:SetTextColor(0.3, 1, 0.3, 1)
behindFrame.text:SetText(T["BEHIND"])
behindFrame.text:Hide()
local lastCheck = 0
behindFrame:SetScript("OnUpdate", function()
if GetTime() - lastCheck < 0.1 then return end
lastCheck = GetTime()
if not UnitExists("target") then
this.text:Hide()
return
end
local success, behind = pcall(UnitXP, "behind", "player", "target")
if success and behind then
this.text:Show()
else
this.text:Hide()
end
end)
pfUI.uf.target.behindIndicator = behindFrame
end
-- Line of Sight Indicator on Target Frame (BELOW BEHIND)
if C.unitframes.los_indicator == "1" and not pfUI.uf.target.losIndicator then
local losFrame = CreateFrame("Frame", "pfLoSIndicator", pfUI.uf.target)
losFrame:SetAllPoints(pfUI.uf.target)
losFrame:SetFrameLevel(pfUI.uf.target:GetFrameLevel() + 10)
losFrame.text = losFrame:CreateFontString(nil, "OVERLAY")
losFrame.text:SetFont(pfUI.font_default, fontSize, "OUTLINE")
losFrame.text:SetPoint("RIGHT", losFrame, "RIGHT", -1, -slot)
losFrame.text:SetTextColor(1, 0.3, 0.3, 1)
losFrame.text:SetText(T["NO LOS"])
losFrame.text:Hide()
local lastCheck = 0
losFrame:SetScript("OnUpdate", function()
if GetTime() - lastCheck < 0.2 then return end
lastCheck = GetTime()
if not UnitExists("target") then
this.text:Hide()
return
end
local success, inSight = pcall(UnitXP, "inSight", "player", "target")
if success and inSight == false then
this.text:Show()
else
this.text:Hide()
end
end)
pfUI.uf.target.losIndicator = losFrame
end
-- Distance Indicator
if C.unitframes.distance_indicator == "1" and not pfUI.uf.target.distanceIndicator then
if C.unitframes.distance_hook_portrait == "1" then
-- Hooked mode: text anchored below Behind/LOS on the target frame
local distFrame = CreateFrame("Frame", "pfDistanceIndicator", pfUI.uf.target)
distFrame:SetAllPoints(pfUI.uf.target)
distFrame:SetFrameLevel(pfUI.uf.target:GetFrameLevel() + 10)
distFrame.text = distFrame:CreateFontString(nil, "OVERLAY")
distFrame.text:SetFont(pfUI.font_default, fontSize, "OUTLINE")
distFrame.text:SetPoint("RIGHT", distFrame, "RIGHT", -1, slot)
distFrame.text:SetTextColor(1, 1, 1, 1)
distFrame.text:Hide()
local thresholds = {
{ 5, 0.3, 0.5, 1.0 }, -- melee (blue)
{ 8, 0.4, 0.7, 1.0 }, -- close (light blue)
{ 20, 0.4, 0.9, 1.0 }, -- short range (sky blue)
{ 30, 0.0, 1.0, 0.0 }, -- mid range (green)
{ 35, 0.8, 1.0, 0.0 }, -- yellow-green
{ 41, 1.0, 1.0, 0.0 }, -- yellow
}
local lastCheck = 0
distFrame:SetScript("OnUpdate", function()
if GetTime() - lastCheck < 0.1 then return end
lastCheck = GetTime()
if not UnitExists("target") then
this.text:Hide()
return
end
local success, distance = pcall(UnitXP, "distanceBetween", "player", "target")
if not success or not distance then
this.text:Hide()
return
end
local r, g, b = 1.0, 0.2, 0.2
for i = 1, table.getn(thresholds) do
if distance <= thresholds[i][1] then
r, g, b = thresholds[i][2], thresholds[i][3], thresholds[i][4]
break
end
end
this.text:SetTextColor(r, g, b, 1)
this.text:SetText(string.format("%.1f%s", distance, suffix))
this.text:Show()
end)
pfUI.uf.target.distanceIndicator = distFrame
else
-- Free frame mode: movable standalone frame, same as rangedisplay module
if not pfRangeDisplay then
local f = CreateFrame("Frame", "pfRangeDisplay", UIParent)
f:SetWidth(90)
f:SetHeight(20)
f:SetFrameStrata("MEDIUM")
f:SetPoint("CENTER", UIParent, "CENTER", 0, -100)
CreateBackdrop(f, nil, true)
CreateBackdropShadow(f)
UpdateMovable(f)
f.text = f:CreateFontString(nil, "OVERLAY")
f.text:SetFont(pfUI.font_default, C.global.font_size + 2, "OUTLINE")
f.text:SetPoint("CENTER", f, "CENTER")
f.text:SetTextColor(1, 1, 1, 1)
f.text:SetText("--")
local thresholds = {
{ 5, 0.0, 0.4, 1.0 },
{ 8, 0.2, 0.6, 1.0 },
{ 20, 0.3, 0.8, 1.0 },
{ 30, 0.0, 0.9, 0.0 },
{ 35, 0.7, 0.9, 0.0 },
{ 41, 1.0, 1.0, 0.0 },
}
local throttle = 0
local scanner = CreateFrame("Frame")
-- expose the poller frame so PLAYER_LOGOUT can stop its UnitXP OnUpdate
-- (it lives on this separate frame, not on distanceIndicator) -> crash 132
pfUI.uf.target.distanceScanner = scanner
scanner:SetScript("OnUpdate", function()
throttle = throttle + arg1
if throttle < 0.05 then return end
throttle = 0
if not UnitExists("target") then
f.text:SetText("--")
f.text:SetTextColor(1, 1, 1, 1)
f:Hide()
return
end
f:Show()
local success, distance = pcall(UnitXP, "distanceBetween", "player", "target")
if not success or not distance then
f.text:SetText("--")
f.text:SetTextColor(1, 1, 1, 1)
return
end
local successL, inSight = pcall(UnitXP, "inSight", "player", "target")
local alpha = (successL and inSight == false) and 0.5 or 1.0
f.text:SetAlpha(alpha)
local r, g, b = 1.0, 0.2, 0.2
for i = 1, table.getn(thresholds) do
if distance <= thresholds[i][1] then
r, g, b = thresholds[i][2], thresholds[i][3], thresholds[i][4]
break
end
end
f.text:SetTextColor(r, g, b, 1)
f.text:SetText(string.format("%.1f%s", distance, suffix))
end)
end
pfUI.uf.target.distanceIndicator = pfRangeDisplay
end
end
return true
end
-- Try to create indicators now
CreateTargetIndicators()
-- Also try on PLAYER_ENTERING_WORLD in case target frame wasn't ready
local initFrame = CreateFrame("Frame")
initFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
initFrame:RegisterEvent("PLAYER_LOGOUT")
initFrame:SetScript("OnEvent", function()
-- Handle shutdown to prevent crash 132
if event == "PLAYER_LOGOUT" then
this:UnregisterAllEvents()
this:SetScript("OnEvent", nil)
-- Stop indicator OnUpdate scripts
if pfUI.uf and pfUI.uf.target then
if pfUI.uf.target.behindIndicator then
pfUI.uf.target.behindIndicator:SetScript("OnUpdate", nil)
end
if pfUI.uf.target.losIndicator then
pfUI.uf.target.losIndicator:SetScript("OnUpdate", nil)
end
if pfUI.uf.target.distanceIndicator then
pfUI.uf.target.distanceIndicator:SetScript("OnUpdate", nil)
end
-- free-frame mode polls from its own scanner frame, not the indicator
if pfUI.uf.target.distanceScanner then
pfUI.uf.target.distanceScanner:SetScript("OnUpdate", nil)
end
end
return
end
CreateTargetIndicators()
this:UnregisterAllEvents()
end)
-- OS Notification Support
if C.unitframes.unitxp_notify == "1" then
local notifyFrame = CreateFrame("Frame")
notifyFrame:RegisterEvent("CHAT_MSG_WHISPER")
notifyFrame:RegisterEvent("CHAT_MSG_BN_WHISPER")
notifyFrame:RegisterEvent("READY_CHECK")
notifyFrame:RegisterEvent("RAID_INSTANCE_WELCOME")
notifyFrame:RegisterEvent("PLAYER_LOGOUT")
notifyFrame:SetScript("OnEvent", function()
-- Handle shutdown to prevent crash 132
if event == "PLAYER_LOGOUT" then
this:UnregisterAllEvents()
this:SetScript("OnEvent", nil)
return
end
pcall(UnitXP, "notify", "taskbarIcon")
pcall(UnitXP, "notify", "systemSound")
end)
-- Also notify on BG queue pop
local origBattlefieldPortShow = BattlefieldFrame_Show
if origBattlefieldPortShow then
BattlefieldFrame_Show = function()
pcall(UnitXP, "notify", "taskbarIcon")
pcall(UnitXP, "notify", "systemSound")
return origBattlefieldPortShow()
end
end
end
-- Enhanced Distance API
pfUI.api.GetPreciseDistance = function(unit1, unit2)
if not unit2 then
unit2 = unit1
unit1 = "player"
end
local success, distance = pcall(UnitXP, "distanceBetween", unit1, unit2)
if success then return distance end
return nil
end
pfUI.api.IsInMeleeRange = function(unit)
local success, distance = pcall(UnitXP, "distanceBetween", "player", unit, "meleeAutoAttack")
if success and distance then
return distance <= 5
end
return nil
end
pfUI.api.GetAoEDistance = function(unit1, unit2)
if not unit2 then
unit2 = unit1
unit1 = "player"
end
local success, distance = pcall(UnitXP, "distanceBetween", unit1, unit2, "AoE")
if success then return distance end
return nil
end
-- Smart Targeting Helpers
pfUI.api.TargetNearestEnemy = function()
local success, found = pcall(UnitXP, "target", "nearestEnemy")
return success and found
end
pfUI.api.TargetHighestHP = function()
local success, found = pcall(UnitXP, "target", "mostHP")
return success and found
end
pfUI.api.TargetNextEnemy = function()
local success, found = pcall(UnitXP, "target", "nextEnemyInCycle")
return success and found
end
pfUI.api.TargetPreviousEnemy = function()
local success, found = pcall(UnitXP, "target", "previousEnemyInCycle")
return success and found
end
pfUI.api.TargetNextMarked = function(order)
local success, found = pcall(UnitXP, "target", "nextMarkedEnemyInCycle", order)
return success and found
end
-- Debug command to test UnitXP indicators
pfUI.api.RegisterSlashCommand("PFUNITXP", { "/pfunitxp" }, function()
local chat = DEFAULT_CHAT_FRAME
chat:AddMessage("|cff33ffccpfUI|r: UnitXP Indicator Debug")
-- Check if target exists
if not UnitExists("target") then
chat:AddMessage(" |cffff0000No target selected|r")
return
end
-- Test behind
local successB, behind = pcall(UnitXP, "behind", "player", "target")
chat:AddMessage(" Behind check: success=" .. tostring(successB) .. " value=" .. tostring(behind) .. " type=" .. type(behind))
-- Test LOS
local successL, inSight = pcall(UnitXP, "inSight", "player", "target")
chat:AddMessage(" LOS check: success=" .. tostring(successL) .. " value=" .. tostring(inSight) .. " type=" .. type(inSight))
-- Check if indicator frames exist
if pfUI.uf and pfUI.uf.target then
chat:AddMessage(" Target frame: |cff00ff00exists|r")
if pfUI.uf.target.behindIndicator then
chat:AddMessage(" Behind indicator: |cff00ff00created|r, visible=" .. tostring(pfUI.uf.target.behindIndicator:IsVisible()))
else
chat:AddMessage(" Behind indicator: |cffff0000NOT created|r (check settings)")
end
if pfUI.uf.target.losIndicator then
chat:AddMessage(" LOS indicator: |cff00ff00created|r")
else
chat:AddMessage(" LOS indicator: |cffff0000NOT created|r (check settings)")
end
else
chat:AddMessage(" Target frame: |cffff0000NOT found|r")
end
end, true)
end)