From 8cf9a5906131248050e5e6a1520eaebff6529b9e Mon Sep 17 00:00:00 2001 From: erik Date: Sun, 8 Jun 2025 23:38:22 +0200 Subject: [PATCH] fixed a bug in stacksize same version --- MosswartMassacre/Utils.cs | 42 +++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/MosswartMassacre/Utils.cs b/MosswartMassacre/Utils.cs index d9b22da..dc8fbdf 100644 --- a/MosswartMassacre/Utils.cs +++ b/MosswartMassacre/Utils.cs @@ -3,6 +3,7 @@ using Decal.Adapter; using Decal.Adapter.Wrappers; using System.Numerics; using Mag.Shared.Constants; +using System.Linq; namespace MosswartMassacre { @@ -87,14 +88,15 @@ namespace MosswartMassacre { try { - var worldFilter = CoreManager.Current.WorldFilter; - var playerInv = CoreManager.Current.CharacterFilter.Id; - + //var worldFilter = CoreManager.Current.WorldFilter; + //var playerInv = CoreManager.Current.CharacterFilter.Id; + // Search inventory - foreach (WorldObject item in worldFilter.GetByContainer(playerInv)) + + foreach (WorldObject wo in CoreManager.Current.WorldFilter.GetInventory()) { - if (string.Equals(item.Name, itemName, StringComparison.OrdinalIgnoreCase)) - return item; + if (string.Equals(wo.Name, itemName, StringComparison.OrdinalIgnoreCase)) + return wo; } return null; @@ -110,12 +112,35 @@ namespace MosswartMassacre /// /// Name of the item to find /// Stack size or 0 if not found + /// + /// Return the total quantity of an item in the character’s inventory, + /// adding up every stack that shares . + /// public static int GetItemStackSize(string itemName) { try { - var item = FindItemByName(itemName); - return item?.Values(LongValueKey.StackCount) ?? 0; + // 1. Pull every WorldObject in bags + containers + var inv = CoreManager.Current.WorldFilter.GetInventory(); + + // 2. Keep only those whose display name matches (case-insensitive) + // 3. For each one, use StackCount if it exists, otherwise treat as 1 + return inv.Where(wo => + string.Equals(wo.Name, itemName, + StringComparison.OrdinalIgnoreCase)) + .Sum(wo => + { + // Some items (weapons, armor) aren’t stackable; + // Values(LongValueKey.StackCount) throws if the key is absent. + try + { + return wo.Values(LongValueKey.StackCount); + } + catch + { + return 1; // non-stackable item = quantity 1 + } + }); } catch { @@ -123,6 +148,7 @@ namespace MosswartMassacre } } + /// /// Get the icon ID of a specific item by name ///