diff --git a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch8b.cs b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch8b.cs index 5215c4659..5030caaa7 100644 --- a/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch8b.cs +++ b/PKHeX.Core/Saves/Substructures/Inventory/InventoryPouch8b.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; @@ -37,6 +38,9 @@ public override void GetPouch(byte[] data) public override void SetPouch(byte[] data) { + HashSet processed = new(); + + // Write all the item slots still present in the pouch. Keep track of the item IDs processed. foreach (var item in Items) { var index = (ushort)item.Index; @@ -52,6 +56,18 @@ public override void SetPouch(byte[] data) var ofs = GetItemOffset(index, Offset); WriteItem(item, data, ofs); + + if (!processed.Contains(index)) // we will allow duplicate item definitions, but they'll overwrite instead of sum/separate. + processed.Add(index); + } + + // For all the items that were not present in the pouch, clear the data for them. + foreach (var index in LegalItems) + { + if (processed.Contains(index)) + continue; + var ofs = GetItemOffset(index, Offset); + WriteItem(new InventoryItem(), data, ofs); } }