this is due to the CanStackItem hook
to fix, replace:
if (item.contents != targetItem.contents) { return false; }
with:
if (!IdenticalContents(item.contents, targetItem.contents)) return false;
and add this:
private bool IdenticalContents(ItemContainer a, ItemContainer b)
{
if (a == null)
{
return b == null;
}
if (b == null || a.itemList.Count != b.itemList.Count)
{
return false;
}
for (int i = 0; i < a.itemList.Count; i++)
{
Item x = a.itemList[i];
Item y = b.FindItemByItemID(x.info.itemid);
if (y == null || x.amount != y.amount || x.fuel != y.fuel || x.instanceData?.dataInt != y.instanceData?.dataInt)
{
return false;
}
}
return true;
}
private object OnItemSplit(Item source, int amount)
{
Item split = ItemManager.CreateByItemID(source.info.itemid, 1, source.skin);
if (split == null)
{
return null;
}
source.amount -= amount;
source.MarkDirty();
split.amount = amount;
split.OnVirginSpawn();
if (source.IsBlueprint())
{
split.blueprintTarget = source.blueprintTarget;
}
if (source.hasCondition)
{
split.condition = source.condition;
}
CopyInstanceData(source, split);
if (source.contents?.itemList.Count > 0)
{
foreach (Item content in source.contents.itemList)
{
Item copy = ItemManager.CreateByItemID(content.info.itemid, 1);
if (copy == null)
{
continue;
}
copy.amount = content.amount;
copy.fuel = content.fuel;
copy.OnVirginSpawn();
if (content.hasCondition)
{
copy.condition = content.condition;
}
CopyInstanceData(content, copy);
copy.MoveToContainer(split.contents);
}
}
split.MarkDirty();
return split;
}
private void CopyInstanceData(Item source, Item item)
{
if (source.instanceData != null && source.instanceData.dataInt != 0)
{
item.instanceData = new ProtoBuf.Item.InstanceData();
item.instanceData.dataInt = source.instanceData.dataInt;
item.instanceData.ShouldPool = false;
}
}
this will also fix numerous other issues such as not being able to stack items with water in them, prevent skins from being lost when split, etc
of course, test it first :p
Merged postMerged postah, forgot to check ammo in weapons.
also, fuel for flamethrowers can be duplicated by splitting 2 flamethrowers that have 0 fuel. the split item will get 50 fuel.