Fish Key: restore after loot and key release, press and hold off

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rope 2026-11-11 17:27:31 +00:00
parent 2d2b31aaf8
commit 02c88282cf
3 changed files with 197 additions and 39 deletions

View file

@ -1,25 +1,46 @@
-- Fish Key: while Fishing channels, every key bound to Fishing is overridden to
-- "Interact with Target", so the same key clicks the bobber, and the Interact
-- Key setting is switched on for the cast. The override is
-- dropped the moment the channel ends (catch, miss, timeout, move) or combat
-- starts. Saved keybinds are never touched.
-- "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.
local FISHING_SPELL = 7620
-- Settings raised while fishing, as temp CVars that are never saved, and
-- dropped again when the cast ends. Each is only raised if it is below the value.
-- How long to wait for the loot window after the cast ends before treating it
-- as a miss.
local LOOT_WAIT = 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 },
{ 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 },
{ 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" },
}
local owner = CreateFrame("Frame")
local overridden = false
local clearAfterCombat = false
local raisedCVars = {}
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
-- 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)
@ -104,43 +125,53 @@ local function FishingKeys()
return keys
end
local function RaiseCVars()
-- "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 ChangeCVars()
for _, cvar in ipairs(FISHING_CVARS) do
local current = tonumber(C_CVar.GetCVar(cvar.name))
if current and current < cvar.value and not raisedCVars[cvar.name] then
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
raisedCVars[cvar.name] = true
changedCVars[cvar.name] = true
elseif not warned then
warned = true
Print("couldn't change " .. cvar.name .. ", turn Interact Key on in Settings")
Print("couldn't change " .. cvar.name .. ", set it in Settings")
end
end
end
end
local function RestoreCVars()
for name in pairs(raisedCVars) do
for name in pairs(changedCVars) do
pcall(C_CVar.RemoveTempCVar, name)
raisedCVars[name] = nil
changedCVars[name] = nil
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
RaiseCVars()
end
local function Restore()
generation = generation + 1
state = nil
looting = false
if overridden then
if InCombatLockdown() then
clearAfterCombat = true
@ -149,12 +180,56 @@ local function Restore()
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 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
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
-- No loot window by then: the cast missed, timed out or was interrupted.
C_Timer.After(LOOT_WAIT, function() TryFinish(gen) 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:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_START", "player")
owner:RegisterUnitEvent("UNIT_SPELLCAST_CHANNEL_STOP", "player")
owner:SetScript("OnEvent", function(_, event, arg1, _, spellID)
@ -169,7 +244,19 @@ owner:SetScript("OnEvent", function(_, event, arg1, _, spellID)
if IsFishing(spellID) then
Apply()
end
elseif event == "UNIT_SPELLCAST_CHANNEL_STOP" or event == "PLAYER_REGEN_DISABLED" then
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
end
elseif event == "LOOT_CLOSED" then
if looting then
looting = false
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