A Rage Talon Dragon Guard
Join Date: Aug 2009
Posts: 325
|
Originally Posted by Phanx
1. For the love of kittens and unicorns, use some freaking variables instead of doing long table lookups over and over. Seeing c['media'].fontNormal written out 500 times is just excruciating.
2. This kind of thing causes Bad Taint, and will definitely get your addon blamed (and rightfully so) for ACTION_BLOCKED errors:
Code:
-- Make the new locations stay
PartyMemberFrame1.SetPoint = function() end;
PartyMemberFrame2.SetPoint = function() end;
PartyMemberFrame3.SetPoint = function() end;
PartyMemberFrame4.SetPoint = function() end;
You cannot overwrite functions or (anything else) on a secure frame without breaking something. Your function is insecure, and taints the entire frame. Instead, you need to figure out which Blizzard function(s) move those frames, and post-hook them to move the frames back to the desired location after the default UI repositions them, but only if you're not in combat.
3. Globals are bad. Don't make anything a global unless you have a Really Good Reason. If you're not 100% sure something needs to be global, it doesn't need to be global, so make it local. On a similar topic, always define variables in the narrowest/strictest/lowest scope necessary. For example, here you are doing two things "wrong":
Code:
if C["unitframes"].arena.tracker == true then
trinkets = {};
-- ^^ This is a global (bad) and a very generically named
-- global at that (even worse). Add a "local" in front of it.
local arenaFrame,trinket;
-- ^^ These variables do not need to exist in this scope.
-- Get rid of this line, and add a "local" in front of each one where
-- they are defined INSIDE the loop below, since that is the only
-- place they are used.
for i = 1, 5 do
arenaFrame = "ArenaEnemyFrame"..i;
trinket = CreateFrame("Cooldown", arenaFrame.."Trinket", ArenaEnemyFrames);
(Actually you're doing a third thing "wrong" by parenting all 5 trinket icons the "ArenaEnemyFrames" frame, whatever that is, instead of parenting each one to the specific frame it's associated with. Change "ArenaEnemyFrames" to "arenaFrame" there.)
Finally, ArenaJunkies is a horrible place to get any kind of code. I've never seen any code snippet attributed to ArenaJunkies that wasn't a contender for the Worst Code Ever Written prize.
|
So I tried taking your advice and cleaned up all the reparative code.
Old Unitframes.lua:
Lua Code:
local B, C, DB = unpack(select(2, ...)) -- Import: B - function; C - config; DB - Database if C["unitframes"].enable ~= true then return end local _G = _G -- Player Frame if C["unitframes"].player.enable then -- Frame Scale _G["PlayerFrame"]:SetScale(C["unitframes"].player.scale); PlayerFrameHealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].player.fontSize,"THINOUTLINE"); PlayerFrameManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].player.fontSize, "THINOUTLINE"); PlayerFrameAlternateManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].player.fontSize, "THINOUTLINE"); PetFrameHealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].player.fontSizepet,"THINOUTLINE"); PetFrameManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].player.fontSizepet, "THINOUTLINE"); end -- Target Frame if C["unitframes"].target.enable then -- Frame Scale _G["TargetFrame"]:SetScale(C["unitframes"].target.scale); TargetFrameTextureFrameHealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].target.fontSize, "THINOUTLINE"); TargetFrameTextureFrameManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].target.fontSize, "THINOUTLINE"); end; -- Focus Frame if C["unitframes"].focus.enable then -- Frame Scale _G["FocusFrame"]:SetScale(C["unitframes"].focus.scale) FocusFrameTextureFrameHealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].focus.fontSize,"THINOUTLINE") FocusFrameTextureFrameManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].focus.fontSize,"THINOUTLINE") end; -- Party Frames -- if C["unitframes"].party.enable then -- Clear all old settings PartyMemberFrame1:ClearAllPoints(); PartyMemberFrame2:ClearAllPoints(); PartyMemberFrame3:ClearAllPoints(); PartyMemberFrame4:ClearAllPoints(); -- Create new locations PartyMemberFrame1:SetPoint(C['unitframes'].party.position.relAnchor, UIParent, C['unitframes'].party.position.offSetX, C['unitframes'].party.position.offSetY); PartyMemberFrame2:SetPoint("TOPLEFT", PartyMemberFrame1, 0, -75); PartyMemberFrame3:SetPoint("TOPLEFT", PartyMemberFrame2, 0, -75); PartyMemberFrame4:SetPoint("TOPLEFT", PartyMemberFrame3, 0, -75); -- Make the new locations stay PartyMemberFrame1.SetPoint = function() end; PartyMemberFrame2.SetPoint = function() end; PartyMemberFrame3.SetPoint = function() end; PartyMemberFrame4.SetPoint = function() end; -- Set the scale of all the frames PartyMemberFrame1:SetScale(C["unitframes"].party.scale); PartyMemberFrame2:SetScale(C["unitframes"].party.scale); PartyMemberFrame3:SetScale(C["unitframes"].party.scale); PartyMemberFrame4:SetScale(C["unitframes"].party.scale); -- Set Font Size PartyMemberFrame1HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame1ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame2HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame2ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame3HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame3ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame4HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") PartyMemberFrame4ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].party.fontSize, "THINOUTLINE") end; -- Arena Frames if C["unitframes"].arena.enable then LoadAddOn("Blizzard_ArenaUI"); -- You only need to run this once. You can safely delete any copies of this line. ArenaEnemyFrames:SetScale(C["unitframes"].arena.scale); ArenaEnemyFrame1HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize,"THINOUTLINE"); ArenaEnemyFrame1ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize, "THINOUTLINE"); ArenaEnemyFrame2HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize,"THINOUTLINE"); ArenaEnemyFrame2ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize, "THINOUTLINE"); ArenaEnemyFrame3HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize,"THINOUTLINE"); ArenaEnemyFrame3ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize, "THINOUTLINE"); ArenaEnemyFrame4HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize,"THINOUTLINE"); ArenaEnemyFrame4ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize, "THINOUTLINE"); ArenaEnemyFrame5HealthBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize,"THINOUTLINE"); ArenaEnemyFrame5ManaBarText:SetFont(C['media'].fontNormal, C["unitframes"].arena.fontSize, "THINOUTLINE"); if C["unitframes"].arena.tracker == true then trinkets = {}; local arenaFrame,trinket; for i = 1, 5 do arenaFrame = "ArenaEnemyFrame"..i; trinket = CreateFrame("Cooldown", arenaFrame.."Trinket", ArenaEnemyFrames); trinket:SetPoint("TOPRIGHT", arenaFrame, 30, -6); trinket:SetSize(24, 24); trinket.icon = trinket:CreateTexture(nil, "BACKGROUND"); trinket.icon:SetAllPoints(); trinket.icon:SetTexture("Interface\\Icons\\inv_jewelry_trinketpvp_01"); trinket:Hide(); trinkets["arena"..i] = trinket; end; local events = CreateFrame("Frame"); function events:UNIT_SPELLCAST_SUCCEEDED(unitID, spell, rank, lineID, spellID) if not trinkets[unitID] then return; end ; if spellID == 59752 or spellID == 42292 then CooldownFrame_SetTimer(trinkets[unitID], GetTime(), 120, 1); SendChatMessage("Trinket used by: "..GetUnitName(unitID, true), "PARTY"); end; end; function events:PLAYER_ENTERING_WORLD() local _, instanceType = IsInInstance(); if instanceType == "arena" then self:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED"); elseif self:IsEventRegistered("UNIT_SPELLCAST_SUCCEEDED") then self:UnregisterEvent("UNIT_SPELLCAST_SUCCEEDED"); for _, trinket in pairs(trinkets) do trinket:SetCooldown(0, 0); trinket:Hide(); end; end; end; events:SetScript("OnEvent", function(self, event, ...) return self[event](self, ...) end); events:RegisterEvent("PLAYER_ENTERING_WORLD"); end; end; -- Boss Frames if C["unitframes"].boss.enable then for i = 1,4 do local boss = _G["Boss"..i.."TargetFrame"]; if boss then boss:SetScale(C["unitframes"].boss.scale) boss:ClearAllPoints(); boss:SetPoint(C['unitframes'].boss.position.relAnchor, UIParent, C['unitframes'].boss.position.offSetX, C['unitframes'].boss.position.offSetY); --boss.ClearAllPoints = function() end; --boss.SetPoint = function() end; end; end; end; -- Font Style thanks to Phanx from WoWinterface. local shorts = { { 1e10, 1e9, "%.0fB" }, -- 10b+ as 12b { 1e9, 1e9, "%.1fB" }, -- 1b+ as 8.3b { 1e7, 1e6, "%.0fM" }, -- 10m+ as 14m { 1e6, 1e6, "%.1fM" }, -- 1m+ as 7.4m { 1e5, 1e3, "%.0fK" }, -- 100k+ as 840k { 1e3, 1e3, "%.1fK" }, -- 1k+ as 2.5k { 0, 1, "%d" }, -- < 1k as 974 } for i = 1, #shorts do shorts[i][4] = shorts[i][3] .. " (%.0f%%)" end hooksecurefunc("TextStatusBar_UpdateTextStringWithValues", function(statusBar, fontString, value, valueMin, valueMax) if value == 0 then return fontString:SetText("") end local style = GetCVar("statusTextDisplay") if style == "PERCENT" then return fontString:SetFormattedText("%.0f%%", value / valueMax * 100) end for i = 1, #shorts do local t = shorts[i] if value >= t[1] then if style == "BOTH" then return fontString:SetFormattedText(t[4], value / t[2], value / valueMax * 100) else if value < valueMax then for j = 1, #shorts do local v = shorts[j] if valueMax >= v[1] then return fontString:SetFormattedText(t[3] .. " / " .. v[3], value / t[2], valueMax / v[2]) end end end return fontString:SetFormattedText(t[3], value / t[2]) end end end end) hooksecurefunc("UnitFrame_Update", function(self) if not self.name then return end local unit, color = self.unit if UnitPlayerControlled(unit) then if UnitIsPlayer(unit) then local _, class = UnitClass(unit) color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class] else color = HIGHLIGHT_FONT_COLOR end elseif UnitIsDead(unit) or UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then color = GRAY_FONT_COLOR else color = FACTION_BAR_COLORS[UnitIsEnemy(unit, "player") and 1 or UnitReaction(unit, "player") or 5] end if not color then color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)["PRIEST"] end self.name:SetTextColor(color.r, color.g, color.b) end) -- Disable healing/damage spam over player/pet frame: PlayerHitIndicator:SetText(nil) PlayerHitIndicator.SetText = function() end PetHitIndicator:SetText(nil) PetHitIndicator.SetText = function() end
New Unitframes.lua:
Lua Code:
local B, C, DB = unpack(select(2, ...)) -- Import: B - function; C - config; DB - Database if C["unitframes"].enable ~= true then return end local _G = _G local font = C['media'].fontNormal -- Player Frame if C["unitframes"].player.enable then if _G["PlayerFrame"] then _G["PlayerFrame"]:SetScale(C["unitframes"].player.scale); _G["PlayerFrameHealthBarText"]:SetFont(font, C["unitframes"].player.fontSize,"THINOUTLINE"); _G["PlayerFrameManaBarText"]:SetFont(font, C["unitframes"].player.fontSize, "THINOUTLINE"); _G["PlayerFrameAlternateManaBarText"]:SetFont(font, C["unitframes"].player.fontSize, "THINOUTLINE"); _G["PetFrameHealthBarText"]:SetFont(font, C["unitframes"].player.fontSizepet,"THINOUTLINE"); _G["PetFrameManaBarText"]:SetFont(font, C["unitframes"].player.fontSizepet, "THINOUTLINE"); end end -- Target Frame if C["unitframes"].target.enable then if _G["TargetFrame"] then _G["TargetFrame"]:SetScale(C["unitframes"].target.scale); _G["TargetFrameTextureFrameHealthBarText"]:SetFont(font, C["unitframes"].target.fontSize, "THINOUTLINE"); _G["TargetFrameTextureFrameManaBarText"]:SetFont(font, C["unitframes"].target.fontSize, "THINOUTLINE"); end; end; -- Focus Frame if C["unitframes"].focus.enable then if _G["FocusFrame"] then _G["FocusFrame"]:SetScale(C["unitframes"].focus.scale) _G["FocusFrameTextureFrameHealthBarText"]:SetFont(font, C["unitframes"].focus.fontSize,"THINOUTLINE") _G["FocusFrameTextureFrameManaBarText"]:SetFont(font, C["unitframes"].focus.fontSize,"THINOUTLINE") end; end; -- Party Frames -- if C["unitframes"].party.enable then for i = 1, MAX_PARTY_MEMBERS do local partyFrame = "PartyMemberFrame"..i _G[partyFrame]:SetScale(C["unitframes"].party.scale); _G[partyFrame.."HealthBarText"]:SetFont(font, C["unitframes"].party.fontSize, "THINOUTLINE"); _G[partyFrame.."ManaBarText"]:SetFont(font, C["unitframes"].party.fontSize, "THINOUTLINE"); end end; -- Arena Frames if C["unitframes"].arena.enable then hooksecurefunc("Arena_LoadUI", function() for i = 1, MAX_ARENA_ENEMIES do arenaFrame = "ArenaEnemyFrame"..i _G[arenaFrame]:SetScale(C["unitframes"].arena.scale); _G[arenaFrame.."HealthBarText"]:SetFont(font, C["unitframes"].arena.fontSize,"THINOUTLINE"); _G[arenaFrame.."ManaBarText"]:SetFont(font, C["unitframes"].arena.fontSize, "THINOUTLINE"); end end) end; -- Boss Frames if C["unitframes"].boss.enable then for i = 1, MAX_BOSS_FRAMES do local bossFrame = "Boss"..i.."TargetFrame" _G[bossFrame.."NameBackground"]:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]]) _G[bossFrame]:SetScale(C["unitframes"].boss.scale); _G[bossFrame]:SetPoint(C['unitframes'].boss.position.relAnchor, UIParent, C['unitframes'].boss.position.offSetX, C['unitframes'].boss.position.offSetY); end; end; -- Font Style / Color thanks to Phanx from WoWinterface. -- Font Style local shorts = { { 1e10, 1e9, "%.0fB" }, -- 10b+ as 12b { 1e9, 1e9, "%.1fB" }, -- 1b+ as 8.3b { 1e7, 1e6, "%.0fM" }, -- 10m+ as 14m { 1e6, 1e6, "%.1fM" }, -- 1m+ as 7.4m { 1e5, 1e3, "%.0fK" }, -- 100k+ as 840k { 1e3, 1e3, "%.1fK" }, -- 1k+ as 2.5k { 0, 1, "%d" }, -- < 1k as 974 } for i = 1, #shorts do shorts[i][4] = shorts[i][3] .. " (%.0f%%)" end hooksecurefunc("TextStatusBar_UpdateTextStringWithValues", function(statusBar, fontString, value, valueMin, valueMax) if value == 0 then return fontString:SetText("") end local style = GetCVar("statusTextDisplay") if style == "PERCENT" then return fontString:SetFormattedText("%.0f%%", value / valueMax * 100) end for i = 1, #shorts do local t = shorts[i] if value >= t[1] then if style == "BOTH" then return fontString:SetFormattedText(t[4], value / t[2], value / valueMax * 100) else if value < valueMax then for j = 1, #shorts do local v = shorts[j] if valueMax >= v[1] then return fontString:SetFormattedText(t[3] .. " / " .. v[3], value / t[2], valueMax / v[2]) end end end return fontString:SetFormattedText(t[3], value / t[2]) end end end end) -- Font Color hooksecurefunc("UnitFrame_Update", function(self) if not self.name then return end local PET_COLOR = { r = 157/255, g = 197/255, b = 255/255 } local unit, color = self.unit if UnitPlayerControlled(unit) then if UnitIsPlayer(unit) then local _, class = UnitClass(unit) color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)[class] else color = PET_COLOR end elseif UnitIsDead(unit) or UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) then color = GRAY_FONT_COLOR else color = FACTION_BAR_COLORS[UnitIsEnemy(unit, "player") and 1 or UnitReaction(unit, "player") or 5] end if not color then color = (CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS)["PRIEST"] end self.name:SetTextColor(color.r, color.g, color.b) end) local frame = CreateFrame("FRAME") frame:RegisterEvent("GROUP_ROSTER_UPDATE") frame:RegisterEvent("PLAYER_TARGET_CHANGED") frame:RegisterEvent("PLAYER_FOCUS_CHANGED") frame:RegisterEvent("UNIT_FACTION") local unitRegions = { target = TargetFrameNameBackground, focus = FocusFrameNameBackground, } frame:SetScript("OnEvent", function(self, event, ...) for unit, region in pairs(unitRegions) do if UnitIsPlayer(unit) then region:SetTexture([[Interface\DialogFrame\UI-DialogBox-Background]]) end end end) -- Disable healing/damage spam over player/pet frame: PlayerHitIndicator:SetText(nil) PlayerHitIndicator.SetText = function() end PetHitIndicator:SetText(nil) PetHitIndicator.SetText = function() end
So far no errors with bugsack but then again I haven't joined a group or raid or arena yet.
Coke
Last edited by cokedrivers : 03-14-14 at 12:07 PM.
|