using System; using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Enums; using StardewModdingAPI.Events; using StardewValley; namespace StardewModdingAPI.Framework.StateTracking.Snapshots { /// A frozen snapshot of a tracked player. internal class PlayerSnapshot { /********* ** Fields *********/ /// An empty item list diff. private readonly SnapshotItemListDiff EmptyItemListDiff = new(Array.Empty(), Array.Empty(), Array.Empty()); /********* ** Accessors *********/ /// The player being tracked. public Farmer Player { get; } /// The player's current location. public SnapshotDiff Location { get; } = new(); /// Tracks changes to the player's skill levels. public IDictionary> Skills { get; } = Enum .GetValues(typeof(SkillType)) .Cast() .ToDictionary(skill => skill, _ => new SnapshotDiff()); /// Get a list of inventory changes. public SnapshotItemListDiff Inventory { get; private set; } /********* ** Public methods *********/ /// Construct an instance. /// The player being tracked. public PlayerSnapshot(Farmer player) { this.Player = player; this.Inventory = this.EmptyItemListDiff; } /// Update the tracked values. /// The player watcher to snapshot. public void Update(PlayerTracker watcher) { this.Location.Update(watcher.LocationWatcher!); foreach ((SkillType skill, var value) in this.Skills) value.Update(watcher.SkillWatchers[skill]); this.Inventory = watcher.TryGetInventoryChanges(out SnapshotItemListDiff? itemChanges) ? itemChanges : this.EmptyItemListDiff; } } }