I was able to reproduce the bug very easily... I'm not sure how I didn't notice it before.
I wrote some unit tests for the MultiStackItemCollection so now if it ever breaks again due to external changes I should know before I make it public.
In the MultiStackCollection you may create replace "maxStackSize" by "remainderStack" on line 195
Code:
if (remainderStack != 0) {
var newItemStack = GenericObjectPool.Get<ItemStack>();
newItemStack.Initialize((itemInfo.Item, remainderStack), this);
m_ItemStacks.Add(newItemStack);
}
This solves about 90% of the issues.
Then on line 171 replacing the condition comparing item IDs by this:
Code:
if (itemInfo.Item.IsUnique || m_ItemStacks[i].Item.StackableEquivalentTo(itemInfo.Item) == false) { continue; }
That solves issues that was allowing Unique items from being stacked
There was also issues when removing items. On line 78 the for loop should have been inversed like this. In addition I had to add another condition otherwise the item could be removed twice. So now that for loop looks like this:
Code:
for (int i = m_ItemStacks.Count - 1; i >= 0; i--) {
if (m_ItemStacks[i].Item.ID != itemInfo.Item.ID) { continue; }
if(itemStackRemoved == m_ItemStacks[i]) { continue; }
itemStackRemoved = m_ItemStacks[i];
previousIndexWithItem = RemoveItemFromStack(i, itemInfo, previousIndexWithItem,
maxStackSize, ref amountToRemove, ref removed);
}
With those changes the MultiStackItemCollection now passes the 50+ tests I wrote for the base ItemCollection.
I'm very sorry about the delay, and I hope that fixes most of the issues you had.
If you get any other issues please do let me know and I'll write more unit tests for the MultiStack Collection.
I made some other changes to clean up the code, but nothing that changes functionality. That will be available in the next update planned for late January