-- Fish Key: while Fishing channels, every key bound to Fishing is overridden to -- "Interact with Target", so the same key clicks the bobber. A few settings are -- changed for the cast (Interact Key on, longer interact range, press and hold -- casting off). Saved keybinds and settings are never touched. -- -- Everything is put back only once the catch is looted (or the cast ended -- without loot) AND every Fishing key is released. Restoring earlier lets the -- still-held key, a double tap, or the key release cast Fishing again, which -- replaces the bobber and loses the fish. Combat start restores at once. -- -- Loot can only be coming if the interact key was pressed just before the cast -- ended. Otherwise (timeout, moving, another spell) there is nothing to wait for. local FISHING_SPELL = 7620 -- An interact this recent counts as the one that ended the cast. local PRESS_WINDOW = 1 -- After a press with neither a loot window nor an error, wait this long plus -- two round trips before treating it as a miss. local LOOT_WAIT_BASE = 0.25 local LOOT_WAIT_MAX = 1.5 local POLL = 0.05 -- Settings changed while fishing, as temp CVars that are never saved, and -- removed again afterwards. "min" raises to at least the value, "max" lowers -- to at most it; a setting already there is left alone. local FISHING_CVARS = { -- Interact Key: 0 off, 1 gamepad, 2 mouse and keyboard, 3 always. { name = "SoftTargetInteract", value = 3, mode = "min" }, -- The default (about 10 yards) is shorter than a cast, so the bobber -- would never become the interact target. { name = "SoftTargetInteractRange", value = 30, mode = "min" }, -- Press and hold casting repeats the spell while the key is held and -- casts again on release. { name = "ActionButtonUseKeyHeldSpell", value = 0, mode = "max" }, } -- Quiet everything but sound effects so the splash stands out (/fish sound). -- There is no per-sound volume, so other effects (spells, footsteps) still play. local SOUND_CVARS = { { name = "Sound_EnableMusic", value = 0, mode = "max" }, { name = "Sound_EnableAmbience", value = 0, mode = "max" }, { name = "Sound_EnableDialog", value = 0, mode = "max" }, { name = "Sound_EnablePetSounds", value = 0, mode = "max" }, { name = "Sound_EnableErrorSpeech", value = 0, mode = "max" }, { name = "Sound_EnableSFX", value = 1, mode = "min" }, { name = "Sound_SFXVolume", value = 1, mode = "min" }, -- Keep hearing it while another window has focus. { name = "Sound_EnableSoundWhenGameIsInBG", value = 1, mode = "min" }, -- Distant sounds are muffled; the bobber is 15-20 yards out. { name = "Sound_EnablePositionalLowPassFilter", value = 0, mode = "max" }, } local owner = CreateFrame("Frame") local overridden = false local clearAfterCombat = false local changedCVars = {} local warned = false -- Keys currently overridden to interact. local activeKeys = {} -- "fishing" while the channel runs, "ending" while waiting for loot or key -- release, nil otherwise. local state local looting = false -- Interact was pressed as the cast ended; a loot window may be on its way. local awaitingLoot = false -- GetTime() of the last interact during the cast. local lastInteract -- Bumped on every new cast and restore, so stale timers do nothing. local generation = 0 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 -- "SHIFT-F" -> "F", "CTRL--" -> "-". local function BaseKey(key) local base = key repeat local stripped = base:gsub("^ALT%-(.)", "%1"):gsub("^CTRL%-(.)", "%1") :gsub("^SHIFT%-(.)", "%1"):gsub("^META%-(.)", "%1") local changed = stripped ~= base base = stripped until not changed return base end local function AnyKeyHeld() for _, key in ipairs(activeKeys) do if IsKeyDown(BaseKey(key)) then return true end end return false end local function ChangeList(list) for _, cvar in ipairs(list) do local current = tonumber(C_CVar.GetCVar(cvar.name)) local wrong = current and (cvar.mode == "min" and current < cvar.value or cvar.mode == "max" and current > cvar.value) if wrong and not changedCVars[cvar.name] then if pcall(C_CVar.SetTempCVar, cvar.name, tostring(cvar.value)) then changedCVars[cvar.name] = true elseif not warned then warned = true Print("couldn't change " .. cvar.name .. ", set it in Settings") end end end end local function ChangeCVars() ChangeList(FISHING_CVARS) if FishKeyDB.sound then ChangeList(SOUND_CVARS) end end local function RestoreCVars() for name in pairs(changedCVars) do pcall(C_CVar.RemoveTempCVar, name) changedCVars[name] = nil end end local function Restore() generation = generation + 1 state = nil looting = false awaitingLoot = false lastInteract = nil if overridden then if InCombatLockdown() then clearAfterCombat = true else ClearOverrideBindings(owner) overridden = false end end activeKeys = {} RestoreCVars() end -- Restores once nothing can still turn a press into a new cast. local function TryFinish(gen) if gen ~= generation or state ~= "ending" or looting or awaitingLoot then return end if AnyKeyHeld() then C_Timer.After(POLL, function() TryFinish(gen) end) return end Restore() end local function Apply() if not FishKeyDB.enabled or InCombatLockdown() then return end generation = generation + 1 looting = false awaitingLoot = false lastInteract = nil local keys = FishingKeys() if #keys == 0 then return end for _, key in ipairs(keys) do SetOverrideBinding(owner, true, key, "INTERACTTARGET") end activeKeys = keys overridden = true state = "fishing" ChangeCVars() end local function ChannelEnded() if state ~= "fishing" then return end state = "ending" local gen = generation if not looting and lastInteract and GetTime() - lastInteract <= PRESS_WINDOW then awaitingLoot = true local _, _, _, latency = GetNetStats() local wait = math.min(LOOT_WAIT_MAX, LOOT_WAIT_BASE + 2 * (latency or 0) / 1000) C_Timer.After(wait, function() if gen == generation and awaitingLoot then awaitingLoot = false TryFinish(gen) end end) return end TryFinish(gen) end -- Runs after the binding (or anything else) interacts; only notes the time. hooksecurefunc(C_PlayerInteractionManager, "InteractUnit", function() if state == "fishing" then lastInteract = GetTime() end end) owner:RegisterEvent("ADDON_LOADED") owner:RegisterEvent("PLAYER_REGEN_DISABLED") owner:RegisterEvent("PLAYER_REGEN_ENABLED") owner:RegisterEvent("LOOT_OPENED") owner:RegisterEvent("LOOT_CLOSED") owner:RegisterEvent("UI_ERROR_MESSAGE") 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 if FishKeyDB.sound == nil then FishKeyDB.sound = true end end elseif event == "UNIT_SPELLCAST_CHANNEL_START" then if IsFishing(spellID) then Apply() end elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" then ChannelEnded() elseif event == "LOOT_OPENED" then -- The loot can arrive just before or just after the channel stops. if state then looting = true awaitingLoot = false end elseif event == "UI_ERROR_MESSAGE" then -- "No fish are hooked", "Your fish got away" and the like: no loot coming. if awaitingLoot then awaitingLoot = false TryFinish(generation) end elseif event == "LOOT_CLOSED" then if looting then looting = false if state == "fishing" then -- Looted before the channel-stop event arrived: nothing left to wait for. lastInteract = nil end TryFinish(generation) end elseif 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 == "sound on" or msg == "sound off" then -- Takes effect from the next cast. FishKeyDB.sound = msg == "sound on" elseif msg ~= "" then Print("/fish shows status, /fish on|off, /fish sound on|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 .. "), quiet sound " .. (FishKeyDB.sound and "on" or "off")) end