wow-addons/FishKey/FishKey.lua

184 lines
5.7 KiB
Lua
Raw Normal View History

-- Fish Key: while Fishing channels, every key bound to Fishing is overridden to
-- "Interact with Target", so the same key clicks the bobber. The override is
-- dropped the moment the channel ends (catch, miss, timeout, move) or combat
-- starts. Saved keybinds are never touched.
local FISHING_SPELL = 7620
-- Soft interact range while fishing. The default (about 10 yards) is shorter
-- than a cast, so the bobber would never become the interact target.
local FISHING_RANGE = 30
local owner = CreateFrame("Frame")
local overridden = false
local clearAfterCombat = false
local raisedRange = false
local function Print(msg)
DEFAULT_CHAT_FRAME:AddMessage("|cff4fc3f7Fish Key|r: " .. msg)
end
local fishingName
local function FishingName()
fishingName = fishingName or C_Spell.GetSpellName(FISHING_SPELL)
return fishingName
end
local function IsFishing(spellID)
return spellID ~= nil and FishingName() ~= nil and C_Spell.GetSpellName(spellID) == FishingName()
end
local function ActionIsFishing(slot)
if type(slot) ~= "number" then
return false
end
local kind, id = GetActionInfo(slot)
if kind == "macro" then
return IsFishing(GetMacroSpell(id))
end
return kind == "spell" and IsFishing(id)
end
-- Blizzard bar buttons by their binding command (ACTIONBUTTON1, MULTIACTIONBAR1BUTTON3, ...).
local BAR_BUTTONS = {
"ActionButton", "MultiBarBottomLeftButton", "MultiBarBottomRightButton",
"MultiBarRightButton", "MultiBarLeftButton", "MultiBar5Button",
"MultiBar6Button", "MultiBar7Button",
}
local function ButtonsByCommand()
local map = {}
for _, prefix in ipairs(BAR_BUTTONS) do
for i = 1, 12 do
local button = _G[prefix .. i]
if button and button.bindingAction then
map[button.bindingAction] = button
end
end
end
return map
end
local function CommandIsFishing(command, buttons)
local spell = command:match("^SPELL (.+)$")
if spell then
local name = FishingName()
return name ~= nil and (spell == name or spell:sub(1, #name + 1) == name .. "(")
end
local macro = command:match("^MACRO (.+)$")
if macro then
local index = GetMacroIndexByName and GetMacroIndexByName(macro)
return index ~= nil and index > 0 and IsFishing(GetMacroSpell(index))
end
local frameName = command:match("^CLICK ([^:]+)")
local button = frameName and _G[frameName] or buttons[command]
if type(button) ~= "table" then
return false
end
local slot = button.action
if slot == nil and button.GetAttribute then
slot = button:GetAttribute("action")
end
return ActionIsFishing(tonumber(slot))
end
-- Every key whose binding casts Fishing, with modifiers (e.g. "SHIFT-F").
local function FishingKeys()
local keys = {}
local buttons = ButtonsByCommand()
for i = 1, GetNumBindings() do
local binding = { GetBinding(i) }
local command = binding[1]
if command and CommandIsFishing(command, buttons) then
for k = 3, #binding do
keys[#keys + 1] = binding[k]
end
end
end
return keys
end
local function RaiseRange()
local current = tonumber(C_CVar.GetCVar("SoftTargetInteractRange"))
if current and current < FISHING_RANGE then
C_CVar.SetTempCVar("SoftTargetInteractRange", tostring(FISHING_RANGE))
raisedRange = true
end
end
local function RestoreRange()
if raisedRange then
C_CVar.RemoveTempCVar("SoftTargetInteractRange")
raisedRange = false
end
end
local function Apply()
if not FishKeyDB.enabled or InCombatLockdown() then
return
end
local keys = FishingKeys()
if #keys == 0 then
return
end
for _, key in ipairs(keys) do
SetOverrideBinding(owner, true, key, "INTERACTTARGET")
end
overridden = true
RaiseRange()
end
local function Restore()
if overridden then
if InCombatLockdown() then
clearAfterCombat = true
else
ClearOverrideBindings(owner)
overridden = false
end
end
RestoreRange()
end
owner:RegisterEvent("ADDON_LOADED")
owner:RegisterEvent("PLAYER_REGEN_DISABLED")
owner:RegisterEvent("PLAYER_REGEN_ENABLED")
owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_START", "player")
owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_STOP", "player")
owner:SetScript("OnEvent", function(_, event, arg1, _, spellID)
if event == "ADDON_LOADED" then
if arg1 == "FishKey" then
FishKeyDB = FishKeyDB or {}
if FishKeyDB.enabled == nil then
FishKeyDB.enabled = true
end
end
elseif event == "UNIT_SPELLCAST_CHANNEL_START" then
if IsFishing(spellID) then
Apply()
end
elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" or event == "PLAYER_REGEN_DISABLED" then
-- Bindings still change inside PLAYER_REGEN_DISABLED; lockdown starts after it.
Restore()
elseif event == "PLAYER_REGEN_ENABLED" and clearAfterCombat then
clearAfterCombat = false
ClearOverrideBindings(owner)
overridden = false
end
end)
SLASH_FISHKEY1 = "/fish"
SlashCmdList.FISHKEY = function(msg)
msg = (msg or ""):lower():match("^%s*(.-)%s*$")
if msg == "on" or msg == "off" then
FishKeyDB.enabled = msg == "on"
if not FishKeyDB.enabled then
Restore()
end
elseif msg ~= "" then
Print("/fish shows status, /fish on, /fish off")
return
end
local keys = FishingKeys()
local where = #keys > 0 and table.concat(keys, ", ") or "no key bound to Fishing"
Print((FishKeyDB.enabled and "on" or "off") .. " (" .. where .. ")")
end