fixed a bug in stacksize same version

This commit is contained in:
erik 2025-06-08 23:38:22 +02:00
parent fda5c0417e
commit 8cf9a59061

View file

@ -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
/// </summary>
/// <param name="itemName">Name of the item to find</param>
/// <returns>Stack size or 0 if not found</returns>
/// <summary>
/// Return the total quantity of an item in the characters inventory,
/// adding up every stack that shares <paramref name="itemName"/>.
/// </summary>
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) arent 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
}
}
/// <summary>
/// Get the icon ID of a specific item by name
/// </summary>