diff --git a/PKHeX/Saves/Substructures/Inventory.cs b/PKHeX/Saves/Substructures/Inventory.cs index 085bc8849..b8f31c8a2 100644 --- a/PKHeX/Saves/Substructures/Inventory.cs +++ b/PKHeX/Saves/Substructures/Inventory.cs @@ -26,6 +26,16 @@ public InventoryItem Clone() { return new InventoryItem {Count = Count, Index = Index, New = New}; } + + // Check Pouch Compatibility + public bool Valid(ushort[] LegalItems, bool HaX, int MaxItemID) + { + if (Index == 0) + return true; + if (Index <= MaxItemID) + return HaX || LegalItems.Contains((ushort)Index); + return false; + } } public class InventoryPouch @@ -247,5 +257,11 @@ public void sortName(string[] names, bool reverse = false) Items = Items.Where(item => item.Index != 0).OrderBy(item => names[item.Index]) .Concat(Items.Where(item => item.Index == 0 || item.Index >= names.Length)).ToArray(); } + + public void sanitizePouch(bool HaX, int MaxItemID) + { + var x = Items.Where(item => item.Valid(LegalItems, HaX, MaxItemID)).ToArray(); + Items = x.Concat(new byte[MaxCount - x.Length].Select(i => new InventoryItem())).ToArray(); + } } } diff --git a/PKHeX/Subforms/Save Editors/SAV_Inventory.cs b/PKHeX/Subforms/Save Editors/SAV_Inventory.cs index 869d99f89..121883b58 100644 --- a/PKHeX/Subforms/Save Editors/SAV_Inventory.cs +++ b/PKHeX/Subforms/Save Editors/SAV_Inventory.cs @@ -153,18 +153,15 @@ private void getBags() var invalid = pouch.Items.Where(item => item.Index != 0 && !pouch.LegalItems.Contains((ushort)item.Index)).ToArray(); var outOfBounds = invalid.Where(item => item.Index >= itemlist.Length).ToArray(); var incorrectPouch = invalid.Where(item => item.Index < itemlist.Length).ToArray(); - pouch.Items = pouch.Items.Where(item => item.Index == 0 || pouch.LegalItems.Contains((ushort)item.Index)).ToArray(); if (outOfBounds.Any()) Util.Error("Unknown item detected.", "Item ID(s): " + string.Join(", ", outOfBounds.Select(item => item.Index))); - if (incorrectPouch.Any()) + if (!Main.HaX && incorrectPouch.Any()) Util.Alert($"The following item(s) have been removed from {pouch.Type} pouch.", string.Join(", ", incorrectPouch.Select(item => itemlist[item.Index])), "If you save changes, the item(s) will no longer be in the pouch."); - int purgedItems = outOfBounds.Length + incorrectPouch.Length; - pouch.Items = pouch.Items.Concat(new byte[purgedItems].Select(i => new InventoryItem())).ToArray(); - + pouch.sanitizePouch(Main.HaX, itemlist.Length - 1); getBag(dgv, pouch); } }