diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-01-05 20:18:16 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-01-05 20:18:16 -0500 |
commit | f976b5c0f095a881fc20f6ce5dcf5a50ebb2b5da (patch) | |
tree | 260fa7579e1c361283bda09c2616783c3fdb5b9a /src/SMAPI/Framework/SnapshotItemListDiff.cs | |
parent | d34f369d35290bca96cc7225d9765d1a8a66fa8b (diff) | |
parent | 48959375b9ef52abf7c7a9404d43aac6ba780047 (diff) | |
download | SMAPI-f976b5c0f095a881fc20f6ce5dcf5a50ebb2b5da.tar.gz SMAPI-f976b5c0f095a881fc20f6ce5dcf5a50ebb2b5da.tar.bz2 SMAPI-f976b5c0f095a881fc20f6ce5dcf5a50ebb2b5da.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/SnapshotItemListDiff.cs')
-rw-r--r-- | src/SMAPI/Framework/SnapshotItemListDiff.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/SnapshotItemListDiff.cs b/src/SMAPI/Framework/SnapshotItemListDiff.cs new file mode 100644 index 00000000..e8ab1b1e --- /dev/null +++ b/src/SMAPI/Framework/SnapshotItemListDiff.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using System.Linq; +using StardewModdingAPI.Events; +using StardewValley; + +namespace StardewModdingAPI.Framework +{ + /// <summary>A snapshot of a tracked item list.</summary> + internal class SnapshotItemListDiff + { + /********* + ** Accessors + *********/ + /// <summary>Whether the item list changed.</summary> + public bool IsChanged { get; } + + /// <summary>The removed values.</summary> + public Item[] Removed { get; } + + /// <summary>The added values.</summary> + public Item[] Added { get; } + + /// <summary>The items whose stack sizes changed.</summary> + public ItemStackSizeChange[] QuantityChanged { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Update the snapshot.</summary> + /// <param name="added">The added values.</param> + /// <param name="removed">The removed values.</param> + /// <param name="sizesChanged">The items whose stack sizes changed.</param> + 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; + } + + /// <summary>Get a snapshot diff if anything changed in the given data.</summary> + /// <param name="added">The added item stacks.</param> + /// <param name="removed">The removed item stacks.</param> + /// <param name="stackSizes">The items with their previous stack sizes.</param> + /// <param name="changes">The inventory changes, or <c>null</c> if nothing changed.</param> + /// <returns>Returns whether anything changed.</returns> + public static bool TryGetChanges(ISet<Item> added, ISet<Item> removed, IDictionary<Item, int> stackSizes, out SnapshotItemListDiff changes) + { + KeyValuePair<Item, int>[] 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; + } + } +} |