fixed a bug in stacksize same version
This commit is contained in:
parent
fda5c0417e
commit
8cf9a59061
1 changed files with 34 additions and 8 deletions
|
|
@ -3,6 +3,7 @@ using Decal.Adapter;
|
||||||
using Decal.Adapter.Wrappers;
|
using Decal.Adapter.Wrappers;
|
||||||
using System.Numerics;
|
using System.Numerics;
|
||||||
using Mag.Shared.Constants;
|
using Mag.Shared.Constants;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace MosswartMassacre
|
namespace MosswartMassacre
|
||||||
{
|
{
|
||||||
|
|
@ -87,14 +88,15 @@ namespace MosswartMassacre
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var worldFilter = CoreManager.Current.WorldFilter;
|
//var worldFilter = CoreManager.Current.WorldFilter;
|
||||||
var playerInv = CoreManager.Current.CharacterFilter.Id;
|
//var playerInv = CoreManager.Current.CharacterFilter.Id;
|
||||||
|
|
||||||
// Search inventory
|
// 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))
|
if (string.Equals(wo.Name, itemName, StringComparison.OrdinalIgnoreCase))
|
||||||
return item;
|
return wo;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
@ -110,12 +112,35 @@ namespace MosswartMassacre
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="itemName">Name of the item to find</param>
|
/// <param name="itemName">Name of the item to find</param>
|
||||||
/// <returns>Stack size or 0 if not found</returns>
|
/// <returns>Stack size or 0 if not found</returns>
|
||||||
|
/// <summary>
|
||||||
|
/// Return the total quantity of an item in the character’s inventory,
|
||||||
|
/// adding up every stack that shares <paramref name="itemName"/>.
|
||||||
|
/// </summary>
|
||||||
public static int GetItemStackSize(string itemName)
|
public static int GetItemStackSize(string itemName)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var item = FindItemByName(itemName);
|
// 1. Pull every WorldObject in bags + containers
|
||||||
return item?.Values(LongValueKey.StackCount) ?? 0;
|
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
|
catch
|
||||||
{
|
{
|
||||||
|
|
@ -123,6 +148,7 @@ namespace MosswartMassacre
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Get the icon ID of a specific item by name
|
/// Get the icon ID of a specific item by name
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue