Move inventory item validity checking to class

#619
Expanded sanitize check to re-add items to fill the pouch back up.
This commit is contained in:
Kurt 2016-12-09 20:12:37 -08:00
parent 844e1df508
commit ae63dceaae
2 changed files with 18 additions and 5 deletions

View File

@ -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();
}
}
}

View File

@ -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);
}
}