Files

166 lines
6.4 KiB
Lua

-- OctoTextSize: addon-local factory; Blizzard CreateFrame is unchanged.
local CreateFrame = OctoTextSize and OctoTextSize.WrapFactory(CreateFrame) or CreateFrame
-- Source's ShowHelm/ShowCloak options are server-backed PLAYER_FLAGS toggles.
-- Use the existing GWTURTLE visibility command and show only confirmed state.
local parts = {"helm", "cloak"}
local known, pending, controls = {}, {}, {}
local generation = 0
local betweenWorlds = false
local panel, status
local function refresh()
if not panel then return end
for _, part in ipairs(parts) do
local button = controls[part]
button:SetChecked(known[part] == "on")
button:SetEnabled(known[part] ~= nil and not pending[part])
end
end
local function note(text)
if status then status:SetText(text) end
refresh()
end
local function send(part, action)
local player = UnitName and UnitName("player")
if not player or not C_ChatInfo or not C_ChatInfo.SendAddonMessage then
note("Equipment appearance is available in the world.")
return false
end
local result = C_ChatInfo.SendAddonMessage("GWTURTLE", "visibility "..part.." "..action,
"WHISPER", player)
if result == 0 or result == true then return true end
if action == "status" then
note("Could not request equipment appearance. Reopen this panel to retry.")
else
note("Could not send the appearance change. Click again to retry.")
end
return false
end
local function query()
if betweenWorlds then return end
generation = generation + 1
local request = generation
known, pending = {}, {}
note("Reading equipment appearance from the server...")
if not send("helm", "status") then return end
if C_Timer and C_Timer.After then
C_Timer.After(5, function()
if request == generation and panel and panel:IsShown()
and (known.helm == nil or known.cloak == nil) then
note("Server state has not arrived. Close and reopen this panel to retry.")
end
end)
end
end
local function change(part)
local previous = known[part]
if previous == nil or pending[part] then refresh(); return end
local action = previous == "on" and "off" or "on"
local request = {}
pending[part] = request
note("Waiting for the server to confirm the change...")
if not send(part, action) then pending[part] = nil; refresh(); return end
if C_Timer and C_Timer.After then
C_Timer.After(5, function()
if pending[part] == request then
-- A status query is safe: it cannot toggle the setting again.
if send(part, "status") then
note("Checking the server's current appearance state...")
end
end
end)
C_Timer.After(10, function()
if pending[part] == request then
pending[part] = nil
known[part] = nil
note("Server confirmation is unavailable. Reopen this panel to retry.")
end
end)
end
end
local events = CreateFrame("Frame")
events:RegisterEvent("CHAT_MSG_SYSTEM")
events:RegisterEvent("PLAYER_LEAVING_WORLD")
events:RegisterEvent("PLAYER_ENTERING_WORLD")
events:SetScript("OnEvent", function(_, event, message)
if event == "PLAYER_LEAVING_WORLD" then
betweenWorlds = true
generation = generation + 1
known, pending = {}, {}
note("Equipment appearance is available in the world.")
return
end
if event == "PLAYER_ENTERING_WORLD" then
betweenWorlds = false
if panel and panel:IsVisible() then query() end
return
end
if betweenWorlds or (issecretvalue and issecretvalue(message)) or type(message) ~= "string" then return end
if message == "GWTURTLE1 VISIBILITY unavailable" then
generation = generation + 1
known, pending = {}, {}
note("Server visibility state is unavailable.")
return
end
local part, state, phase = message:match(
"^GWTURTLE1 VISIBILITY (%a+) (%a+) (%a+)$")
if (part ~= "helm" and part ~= "cloak") or
(state ~= "on" and state ~= "off") or
(phase ~= "pending" and phase ~= "confirmed") then return end
if phase == "confirmed" then
known[part] = state
pending[part] = nil
if known.helm and known.cloak and not pending.helm and not pending.cloak then
note("Equipment appearance is server confirmed.")
else
refresh()
end
elseif pending[part] then
note("Waiting for the server to confirm the change...")
end
end)
local function register()
if not Settings or not Settings.INTERFACE_CATEGORY_ID then return end
local parent = Settings.GetCategory(Settings.INTERFACE_CATEGORY_ID)
if not parent then return end
panel = CreateFrame("Frame", nil, UIParent)
panel:SetSize(560, 210)
local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
title:SetPoint("TOPLEFT", panel, "TOPLEFT", 8, -8)
title:SetText("Equipment appearance")
local detail = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
detail:SetPoint("TOPLEFT", title, "BOTTOMLEFT", 0, -12)
detail:SetText("These choices are saved by the realm for this character.")
for i, part in ipairs(parts) do
local button = CreateFrame("CheckButton", nil, panel, "UICheckButtonTemplate")
button:SetPoint("TOPLEFT", panel, "TOPLEFT", 8, -60 - (i - 1) * 38)
local label = panel:CreateFontString(nil, "ARTWORK", "GameFontHighlight")
label:SetPoint("LEFT", button, "RIGHT", 7, 0)
label:SetText(part == "helm" and "Show helm" or "Show cloak")
controls[part] = button
button:SetScript("OnClick", function() change(part) end)
end
status = panel:CreateFontString(nil, "ARTWORK", "GameFontDisableSmall")
status:SetPoint("TOPLEFT", panel, "TOPLEFT", 8, -150)
status:SetText("Equipment appearance is available in the world.")
panel:SetScript("OnShow", query)
refresh()
local category = Settings.RegisterCanvasLayoutSubcategory(
parent, panel, "Equipment appearance")
Settings.RegisterCategory(category)
end
if EventUtil and EventUtil.ContinueOnAddOnLoaded then
EventUtil.ContinueOnAddOnLoaded("Blizzard_SettingsDefinitions_Frame", function()
if SettingsRegistrar then SettingsRegistrar:AddRegistrant(register) end
end)
end