diff --git a/JotunnModStub/RaceClaws.cs b/JotunnModStub/RaceClaws.cs index ba4fda8..41826d6 100644 --- a/JotunnModStub/RaceClaws.cs +++ b/JotunnModStub/RaceClaws.cs @@ -560,18 +560,25 @@ namespace RacesOfValheim /// The claws cannot be picked up with the mouse. SetupDragItem is the single door every manual /// move goes through - within the inventory, out to a chest, onto the ground, into a crafting /// slot - so refusing here closes all of them with one prefix. + /// + /// Only refused while the item is CURRENTLY that race's claws - checked against SetOwning + /// (whose set the item is) against RaceClawSet.For (what race the player is right now), not + /// merely "is this any race's claws at all". A player who has since switched race carries a + /// pair that RaceItemCleanup should have already removed, but if it has not run yet for any + /// reason, this is the difference between an item merely undraggable and one undraggable + /// forever: without the race check, nothing the player does - not death, not a tombstone, not + /// closing the item's own quest-item flag - would ever let them shed it again. /// [HarmonyPatch(typeof(InventoryGui), "SetupDragItem")] public static class RaceClaws_SetupDragItem_Patch { public static bool Prefix(ItemDrop.ItemData item) { - if (RaceClawSet.SetOwning(item) == null) return true; + RaceClawSet owner = RaceClawSet.SetOwning(item); + if (owner == null) return true; + if (Player.m_localPlayer == null || owner != RaceClawSet.For(Player.m_localPlayer)) return true; - if (Player.m_localPlayer != null) - { - Player.m_localPlayer.Message(MessageHud.MessageType.Center, "They are part of your hands."); - } + Player.m_localPlayer.Message(MessageHud.MessageType.Center, "They are part of your hands."); return false; } } diff --git a/JotunnModStub/RaceItemCleanup.cs b/JotunnModStub/RaceItemCleanup.cs new file mode 100644 index 0000000..b4a01c5 --- /dev/null +++ b/JotunnModStub/RaceItemCleanup.cs @@ -0,0 +1,82 @@ +using System.Collections.Generic; + +namespace RacesOfValheim +{ + /// + /// Strips every "part of the body" item that no longer matches the player's current race - the + /// permanent claws (Aswid, Fenrir, Sverm) and the disabled Aswid spectral claw. + /// + /// WHY THIS HAS TO EXIST SEPARATELY FROM RESETRACE'S OLD CLEANUP. Every one of these items is + /// m_questItem, which is what makes Valheim refuse to drop, trade or stack them - the cheapest + /// honest way each race had of saying "this is part of your body". That flag does not know + /// what race issued it or what race the player is now: it blocks the item unconditionally, + /// forever, for as long as the item exists. ResetRace used to clear the race ZDO, the skin + /// colour and a handful of status effects and stop there, so a player who had ever been + /// clawed, then switched to a race with no claws of its own, kept carrying an item that + /// nothing in the game - not death, not a tombstone, not the holster key, not closing and + /// reopening the world - could ever remove again. That is exactly the bug this file closes. + /// + /// Reuses the safe removal shape AswidSpectralClaw.RemoveClaw already proved (unequip, then + /// Inventory.RemoveItem) rather than the raw-backing-list bypass AswidSpectralClaw.PurgeAll + /// uses for corrupted entries: these items are never corrupted, only foreign, and going + /// through the normal API keeps the player's held-item state in sync when a purged pair + /// happens to be equipped at the moment of a race switch - which, since Maintain re-equips + /// empty hands on a 0.5s timer, it usually is. + /// + public static class RaceItemCleanup + { + /// + /// Called from ResetRace - the shared chokepoint for the console command and the Race Menu + /// Ward - and from PlayerSpawnPatch, so a save that is ALREADY stuck heals itself on the + /// next login without the player needing to find the Ward at all. + /// + public static void PurgeForeignRaceItems(Player player) + { + if (player == null) return; + + // Already unconditional (removes every copy regardless of race) and already proven + // against corrupted/duplicated entries - reused rather than re-implemented. + AswidSpectralClaw.PurgeAll(player); + + Inventory inventory = player.GetInventory(); + if (inventory == null) return; + + // Snapshotted first: GetAllItems returns the live backing list in the shipped + // assembly, and unequipping/removing while iterating it directly is exactly the kind + // of "modified during enumeration" that throws. + List items = new List(inventory.GetAllItems()); + int removed = 0; + + foreach (ItemDrop.ItemData item in items) + { + if (item == null || !IsForeign(player, item)) continue; + + if (player.IsItemEquiped(item)) player.UnequipItem(item, false); + inventory.RemoveItem(item); + removed++; + } + + if (removed > 0) + { + Jotunn.Logger.LogInfo( + $"[RacesOfValheim] Purged {removed} foreign-race claw item(s) no longer matching this character's race."); + } + } + + /// + /// True for an item that is some race's permanent claws, where that race is not the one + /// the player is right now. Aswid and Fenrir go through the shared RaceClawSet registry; + /// the Sverm keeps its own parallel copy of the same machinery (see SvermClawSlot.cs) and + /// is checked separately for that reason. + /// + private static bool IsForeign(Player player, ItemDrop.ItemData item) + { + RaceClawSet owner = RaceClawSet.SetOwning(item); + if (owner != null) return owner != RaceClawSet.For(player); + + if (SvermClawSlot.IsClaws(item)) return !SvermVisuals.IsSvermPlayer(player); + + return false; + } + } +} diff --git a/JotunnModStub/RacesOfValheim.cs b/JotunnModStub/RacesOfValheim.cs index 2435310..ec97d01 100644 --- a/JotunnModStub/RacesOfValheim.cs +++ b/JotunnModStub/RacesOfValheim.cs @@ -68,6 +68,13 @@ namespace RacesOfValheim /// /// Clears the current race and reopens the selection menu. Shared by the resetrace console /// command and the Race Menu Ward, so both paths clean up exactly the same state. + /// + /// Every status effect a race can leave running is removed here, not just the "stats" + /// ones - a cooldown SE left ticking after a reset is invisible until the player picks + /// the same race again and finds an ability still locked out for a timer nobody can see. + /// The item purge runs LAST, after the ZDO already reads "Human": RaceClawSet.For and + /// SvermVisuals.IsSvermPlayer both read the race live off the player, so purging before + /// the race actually changes would find nothing foreign yet. /// public static void ResetRace() { @@ -100,11 +107,21 @@ namespace RacesOfValheim player.GetSEMan().RemoveStatusEffect("SE_SkadiCooldown".GetStableHashCode()); player.GetSEMan().RemoveStatusEffect(DvergarConfig.StatsSE.GetStableHashCode()); player.GetSEMan().RemoveStatusEffect(DvergarConfig.OverchargeSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(DvergarConfig.OverchargeCdSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(DvergarConfig.RunicPingCdSE.GetStableHashCode()); player.GetSEMan().RemoveStatusEffect(TrentaConfig.StatsSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(TrentaConfig.PurpleRainCdSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(TrentaConfig.PortalCdSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(SvermConfig.StatsSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(SvermConfig.StingerSE.GetStableHashCode()); + player.GetSEMan().RemoveStatusEffect(SvermConfig.StingerCdSE.GetStableHashCode()); ZNetView nview = player.GetComponent(); if (nview != null && nview.IsValid()) nview.GetZDO().Set("RacesOfValheim_Race", "Human"); + // Only after the ZDO above already says "Human" - see the class doc comment. + RaceItemCleanup.PurgeForeignRaceItems(player); + if (Instance != null) { if (FenrirRace.Instance != null) FenrirRace.Instance.ForceRevert(); @@ -819,6 +836,12 @@ namespace RacesOfValheim // The Dvergar needs no entry here: its tint is applied by the UpdateColors postfix // every time Valheim refreshes the body, rather than being stored on the player. + // Heals a save that got stuck holding another race's permanent claws BEFORE this + // purge existed - runs on every spawn rather than only from ResetRace, so a + // player who already has the bug is fixed by logging in, not by finding the Ward + // and resetting a race they may not even want to leave. + RaceItemCleanup.PurgeForeignRaceItems(__instance); + // Check for Race Selection if (!__instance.m_customData.ContainsKey("Race")) { diff --git a/JotunnModStub/Sverm/SvermClawSlot.cs b/JotunnModStub/Sverm/SvermClawSlot.cs index 11b911c..50ad0e5 100644 --- a/JotunnModStub/Sverm/SvermClawSlot.cs +++ b/JotunnModStub/Sverm/SvermClawSlot.cs @@ -265,6 +265,13 @@ namespace RacesOfValheim /// in order to change exactly one of them, and it cost the player four slots. The claws are /// visible now, which is also more honest: they are part of the character, and the character /// can see them. + /// + /// Only refused while the player IS CURRENTLY a Sverm. A player who has switched race carries + /// a pair that RaceItemCleanup should have already removed, but this check is what stops a + /// leftover pair from being undraggable forever if that cleanup has not run yet for any + /// reason - the quest-item flag already keeps them off the ground and out of a tombstone, so + /// a race check missing here was the one thing standing between "stuck until logout" and + /// "stuck permanently". /// [HarmonyPatch(typeof(InventoryGui), "SetupDragItem")] public static class Sverm_SetupDragItem_Patch @@ -272,11 +279,9 @@ namespace RacesOfValheim public static bool Prefix(ItemDrop.ItemData item) { if (!SvermClawSlot.IsClaws(item)) return true; + if (Player.m_localPlayer == null || !SvermVisuals.IsSvermPlayer(Player.m_localPlayer)) return true; - if (Player.m_localPlayer != null) - { - Player.m_localPlayer.Message(MessageHud.MessageType.Center, "They are part of your hands."); - } + Player.m_localPlayer.Message(MessageHud.MessageType.Center, "They are part of your hands."); return false; } }