From bca47beda6524422c096fe6bd90264e9fd94fbfe Mon Sep 17 00:00:00 2001 From: kanym Date: Sat, 1 Aug 2026 13:25:02 +0200 Subject: [PATCH] Ignore race ability input while a GUI has focus UnityEngine.Input reads the device directly, so abilities bound to keys or mouse buttons kept firing with the map, the inventory, a chest, the chat or a menu open - clicking an item in a container also swung the transformed attack. Route that input through Player.TakeInput, which is what Valheim runs its own controls through and already reports false for those states (InventoryGui covers the backpack and chests alike). Aswid's transform keys were the only place already doing this; the shared helper replaces that one-off and covers Skadi's cold spell, form keys, flight toggle, form attacks and flight boost, Fenrir's form keys and the attacks of all four form behaviors, Trol's rock throw and true form, and Aswid's devour toggle. Cultist fire is a held button, so it is gated per read rather than around the block: opening a GUI mid-stream now falls through to StopFire() instead of leaving the flames running. --- JotunnModStub/Aswid/AswidPatches.cs | 2 +- JotunnModStub/Aswid/AswidRace.cs | 3 +- JotunnModStub/Fenrir/FenrirBehaviors.cs | 18 +++++++----- JotunnModStub/Fenrir/FenrirRace.cs | 2 ++ JotunnModStub/RaceInput.cs | 36 ++++++++++++++++++++++++ JotunnModStub/Skadi/DrakeFormBehavior.cs | 4 +-- JotunnModStub/Skadi/SkadiPatches.cs | 2 +- JotunnModStub/Skadi/SkadiRace.cs | 5 +++- JotunnModStub/Trol/TrolRace.cs | 15 ++++++---- 9 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 JotunnModStub/RaceInput.cs diff --git a/JotunnModStub/Aswid/AswidPatches.cs b/JotunnModStub/Aswid/AswidPatches.cs index 19e26df..bc8becb 100644 --- a/JotunnModStub/Aswid/AswidPatches.cs +++ b/JotunnModStub/Aswid/AswidPatches.cs @@ -506,7 +506,7 @@ namespace RacesOfValheim ZNetView nview = __instance.GetComponent(); if (nview != null && nview.IsValid() && nview.GetZDO().GetString("RacesOfValheim_Race", "Human") == "Aswid") { - if (__instance == Player.m_localPlayer && Input.GetKeyDown(KeyCode.I)) + if (__instance == Player.m_localPlayer && RaceInput.Allowed && Input.GetKeyDown(KeyCode.I)) { AswidRace.DevourEnabled = !AswidRace.DevourEnabled; string msg = AswidRace.DevourEnabled ? "Devour Ability: ENABLED" : "Devour Ability: DISABLED"; diff --git a/JotunnModStub/Aswid/AswidRace.cs b/JotunnModStub/Aswid/AswidRace.cs index 124b63e..62fc547 100644 --- a/JotunnModStub/Aswid/AswidRace.cs +++ b/JotunnModStub/Aswid/AswidRace.cs @@ -515,8 +515,7 @@ namespace RacesOfValheim int state = BossProgressionManager.GetCurrentProgressionState(); - bool takeInput = HarmonyLib.Traverse.Create(Player.m_localPlayer).Method("TakeInput").GetValue(); - if (takeInput) + if (RaceInput.Allowed) { if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(AswidProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.H)) { TryToggleMistForm(); return; } diff --git a/JotunnModStub/Fenrir/FenrirBehaviors.cs b/JotunnModStub/Fenrir/FenrirBehaviors.cs index 7954de1..e2628aa 100644 --- a/JotunnModStub/Fenrir/FenrirBehaviors.cs +++ b/JotunnModStub/Fenrir/FenrirBehaviors.cs @@ -56,7 +56,7 @@ namespace RacesOfValheim fenrirAnim.SetFloat("sideway_speed", playerAnim.GetFloat("sideway_speed")); } } - if (isGrounded) + if (isGrounded && RaceInput.Allowed) { if (Input.GetKeyDown(KeyCode.O)) { @@ -311,8 +311,12 @@ namespace RacesOfValheim if (isGrounded) { + // Gated per-read rather than around the block, so opening a GUI mid-stream + // still falls through to StopFire() instead of leaving the flames running. + bool canAct = RaceInput.Allowed; + // Fire attack on Right Click - if (Input.GetMouseButton(1)) + if (canAct && Input.GetMouseButton(1)) { if (Player.m_localPlayer.GetEitr() > 0) { @@ -335,18 +339,18 @@ namespace RacesOfValheim StopFire(); } - if (Input.GetKeyDown(KeyCode.O)) + if (canAct && Input.GetKeyDown(KeyCode.O)) { StartCoroutine(HowlRoutine()); } // Attacks on Left Click - if (Input.GetMouseButtonDown(0) && !isShootingFire) + if (canAct && Input.GetMouseButtonDown(0) && !isShootingFire) { StartCoroutine(ClawRoutine()); // Single claw } // Double claw on Middle Click - else if (Input.GetMouseButtonDown(2) && !isShootingFire) + else if (canAct && Input.GetMouseButtonDown(2) && !isShootingFire) { StartCoroutine(DoubleClawRoutine()); } @@ -671,7 +675,7 @@ namespace RacesOfValheim } } - if (isGrounded) + if (isGrounded && RaceInput.Allowed) { if (Input.GetMouseButtonDown(0)) // Left Click { @@ -844,7 +848,7 @@ namespace RacesOfValheim } } - if (isGrounded) + if (isGrounded && RaceInput.Allowed) { if (Input.GetMouseButtonDown(0)) // Left Click { diff --git a/JotunnModStub/Fenrir/FenrirRace.cs b/JotunnModStub/Fenrir/FenrirRace.cs index 3768190..5adc796 100644 --- a/JotunnModStub/Fenrir/FenrirRace.cs +++ b/JotunnModStub/Fenrir/FenrirRace.cs @@ -143,6 +143,8 @@ namespace RacesOfValheim { } } + if (!RaceInput.Allowed) return; + int state = BossProgressionManager.GetCurrentProgressionState(); if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(FenrirProgression.GetGKeyRules(state)); return; } diff --git a/JotunnModStub/RaceInput.cs b/JotunnModStub/RaceInput.cs new file mode 100644 index 0000000..4c92a55 --- /dev/null +++ b/JotunnModStub/RaceInput.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using HarmonyLib; + +namespace RacesOfValheim +{ + /// + /// Gate for race ability input. + /// + /// UnityEngine.Input reads the device directly, so abilities bound to keys or mouse buttons + /// kept firing while the map, the inventory, a chest, the chat or a menu had focus - dragging + /// an item inside a container also swung the transformed attack, for instance. Valheim runs + /// its own controls through Player.TakeInput, which reports false for exactly those states + /// (InventoryGui covers the backpack and chests alike), so mirror that instead of maintaining + /// a parallel list of GUI checks that would drift as the game changes. + /// + public static class RaceInput + { + // Not public on Player, hence the reflection; resolved once rather than per frame. + private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput"); + + public static bool Allowed + { + get + { + Player player = Player.m_localPlayer; + if (player == null) return false; + + // Fail open: if a game update ever renames the method, abilities stay usable + // rather than silently going dead. + if (TakeInputMethod == null) return true; + + return (bool)TakeInputMethod.Invoke(player, null); + } + } + } +} diff --git a/JotunnModStub/Skadi/DrakeFormBehavior.cs b/JotunnModStub/Skadi/DrakeFormBehavior.cs index 75a6a6e..62941b0 100644 --- a/JotunnModStub/Skadi/DrakeFormBehavior.cs +++ b/JotunnModStub/Skadi/DrakeFormBehavior.cs @@ -67,7 +67,7 @@ namespace RacesOfValheim if (Player.m_localPlayer == null || Player.m_localPlayer.IsDead()) return; // Handle Flight Toggle (RMB) - Disabled for Fader - if (Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon) + if (RaceInput.Allowed && Input.GetMouseButtonDown(1) && formRules != null && formRules.Form != SkadiProgression.FormType.SkeletonDragon) { if (!isFlying && !isLanding) { @@ -122,7 +122,7 @@ namespace RacesOfValheim } // Handle Attack - if (CanAttackNow()) + if (CanAttackNow() && RaceInput.Allowed) { if (Input.GetMouseButtonDown(0)) { diff --git a/JotunnModStub/Skadi/SkadiPatches.cs b/JotunnModStub/Skadi/SkadiPatches.cs index fc1976f..e38a81e 100644 --- a/JotunnModStub/Skadi/SkadiPatches.cs +++ b/JotunnModStub/Skadi/SkadiPatches.cs @@ -232,7 +232,7 @@ namespace RacesOfValheim var traverse = Traverse.Create(__instance); Vector3 lookDir = Player.m_localPlayer.GetLookDir(); - bool boost = Input.GetKey(KeyCode.W); + bool boost = RaceInput.Allowed && Input.GetKey(KeyCode.W); bool onGround = __instance.IsOnGround(); traverse.Field("m_run").SetValue(boost); diff --git a/JotunnModStub/Skadi/SkadiRace.cs b/JotunnModStub/Skadi/SkadiRace.cs index 071dac5..1b45633 100644 --- a/JotunnModStub/Skadi/SkadiRace.cs +++ b/JotunnModStub/Skadi/SkadiRace.cs @@ -150,7 +150,7 @@ namespace RacesOfValheim } } - if (rules.ColdSpell) + if (rules.ColdSpell && RaceInput.Allowed) { HandleColdSpell(rules); } @@ -167,6 +167,9 @@ namespace RacesOfValheim return; } + // Form keys and the debug dump are ability input: they stay inert while a GUI has focus. + if (!RaceInput.Allowed) return; + int state = BossProgressionManager.GetCurrentProgressionState(); if (Input.GetKeyDown(KeyCode.G)) { TryTransformWithRules(SkadiProgression.GetGKeyRules(state)); return; } if (Input.GetKeyDown(KeyCode.H)) { TryTransformWithRules(SkadiProgression.GetHKeyRules(state)); return; } diff --git a/JotunnModStub/Trol/TrolRace.cs b/JotunnModStub/Trol/TrolRace.cs index ee82086..5b7eb15 100644 --- a/JotunnModStub/Trol/TrolRace.cs +++ b/JotunnModStub/Trol/TrolRace.cs @@ -119,14 +119,17 @@ namespace RacesOfValheim { if (!IsTrol()) return; - if (Input.GetKeyDown(KeyCode.G)) + if (RaceInput.Allowed) { - TryThrowRock(); - } + if (Input.GetKeyDown(KeyCode.G)) + { + TryThrowRock(); + } - if (Input.GetKeyDown(KeyCode.H)) - { - TryTrueForm(); + if (Input.GetKeyDown(KeyCode.H)) + { + TryTrueForm(); + } } // Check log durability