using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Events; using StardewValley; namespace StardewModdingAPI.Framework { /// A snapshot of a tracked item list. internal class SnapshotItemListDiff { /********* ** Accessors *********/ /// Whether the item list changed. public bool IsChanged { get; } /// The removed values. public Item[] Removed { get; } /// The added values. public Item[] Added { get; } /// The items whose stack sizes changed. public ItemStackSizeChange[] QuantityChanged { get; } /********* ** Public methods *********/ /// Update the snapshot. /// The added values. /// The removed values. /// The items whose stack sizes changed. public SnapshotItemListDiff(Item[] added, Item[] removed, ItemStackSizeChange[] sizesChanged) { this.Removed = removed; this.Added = added; this.QuantityChanged = sizesChanged; this.IsChanged = removed.Length > 0 || added.Length > 0 || sizesChanged.Length > 0; } /// Get a snapshot diff if anything changed in the given data. /// The added item stacks. /// The removed item stacks. /// The items with their previous stack sizes. /// The inventory changes, or null if nothing changed. /// Returns whether anything changed. public static bool TryGetChanges(ISet added, ISet removed, IDictionary stackSizes, [NotNullWhen(true)] out SnapshotItemListDiff? changes) { KeyValuePair[] sizesChanged = stackSizes.Where(p => p.Key.Stack != p.Value).ToArray(); if (sizesChanged.Any() || added.Any() || removed.Any()) { changes = new SnapshotItemListDiff( added: added.ToArray(), removed: removed.ToArray(), sizesChanged: sizesChanged.Select(p => new ItemStackSizeChange(p.Key, p.Value, p.Key.Stack)).ToArray() ); return true; } changes = null; return false; } } }