Possible bug in ItemSlotCollectionView.CanAddItem

Zaddo

Active member
I recently upgraded 1.2.15 to 1.2.18. The method CanAddItem in ItemSlotCollectionView now has another condition to check if the item is already in the collection.

I am adding a new item to the collection with this bit of code:
C#:
ItemInfo hotbarInfo = new ItemInfo(item, qty);
newItemInfo = HotBarSlots.AddItem(hotbarInfo, position);

The ItemStack on the ItemInfo I create is null and so this new condition is crashing. I don't understand what the circled condition is checking and I think it might be wrong? Or, do I need to do some additional initialization on the ItemInfo before I try to add to the collection?

1695379150979.png

I changed this code as follows, because I think that is what the check should be?

C#:
            //Check if the item you are trying to add isn't already in this collection at that index:
            if (itemInfo.ItemStack != null && itemInfo.ItemStack == GetItemAt(index).ItemStack && itemInfo.Item.ID == GetItemAt(index).ItemStack.Item.ID) {
                return false;
            }
 
The Condition you circled in red is checking if the item you are adding has the same ID as the one in the itemstack you are trying to add it to. (The first condition is checking if the item stacks are the same so whether you do GetItemAt(index).ItemStack.Item.ID or itemInfo.ItemStack.Item:ID it will do the exact same thing).

The issue you are having is that the ItemStack is null, which in most casses shouldn't be the case. But it is in yours and your code is completely valid.
So this is indeed a bug.

Could you give this code a try instead:
Code:
/// <summary>
/// Can the item be added to the item view slot.
/// </summary>
/// <param name="itemInfo">The item info.</param>
/// <param name="index">The index.</param>
/// <returns>True if the item can be added.</returns>
public override bool CanAddItem(ItemInfo itemInfo, int index)
{
    var canAddBase = base.CanAddItem(itemInfo, index);
    if (canAddBase == false) { return false; }
    if (Inventory.CanAddItem(itemInfo, m_ItemSlotCollection) == null) { return false; }
    
    //Check there is an item in the slot already:
    var itemAtIndex = GetItemAt(index);
    if (itemAtIndex == ItemInfo.None || itemAtIndex.ItemStack == null) {
        return true;
    }
    //Check if the item you are trying to add isn't already in this collection at that index:
    if (itemInfo.ItemStack == itemAtIndex.ItemStack && itemInfo.Item.ID == itemInfo.ItemStack.Item.ID) {
        return false;
    }
    return true;
}
 
Top