RacesOfValheim/JotunnModStub/RaceItemCleanup.cs
kanym f51afd3a80 fix(claws): stop permanent claws from getting stuck in inventory after a race switch
Aswid's Claws of the Beast, Fenrir's Cursed Claws and Sverm's Vilebone
Maulclaws are all m_questItem so the game refuses to drop, trade or
tombstone them - the mod's way of saying "these are part of your
body". But none of the code enforcing that ever checked whether the
player was STILL that race, and nothing removed the item when they
switched away, so a player who was ever Fenrir or Sverm and then
became anything else was left holding claws with no way to shed them:
not by dying, not by breaking them, not by relogging.

- RaceClaws.cs / SvermClawSlot.cs: the SetupDragItem prefixes that
  block moving/dropping the claws now also check that the item still
  belongs to the player's CURRENT race, so an already-mismatched pair
  becomes draggable again immediately.
- RaceItemCleanup.cs (new): central purge that unequips and removes
  any claws (plus the disabled Aswid spectral claw) that don't match
  the player's current race.
- Wired into ResetRace() - the Race Menu Ward's actual reset path -
  so a reset now clears every race-specific status effect (several
  cooldown SEs were missing entirely: Dvergar's Runic Ping/Overcharge,
  Trenta's Purple Rain/Portal, all of Sverm's) as well as the items.
- Also wired into PlayerSpawnPatch so an ALREADY-stuck save heals
  itself on next login, without the player needing to find the Ward.
2026-08-12 13:04:40 +02:00

82 lines
4.1 KiB
C#

using System.Collections.Generic;
namespace RacesOfValheim
{
/// <summary>
/// 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.
/// </summary>
public static class RaceItemCleanup
{
/// <summary>
/// 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.
/// </summary>
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<ItemDrop.ItemData> items = new List<ItemDrop.ItemData>(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.");
}
}
/// <summary>
/// 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.
/// </summary>
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;
}
}
}