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 SnapshotItemListDiff(new Item[0], new Item[0], new ItemStackSizeChange[0]);
/*********
** Accessors
*********/
/// The player being tracked.
public Farmer Player { get; }
/// The player's current location.
public SnapshotDiff Location { get; } = new SnapshotDiff();
/// Tracks changes to the player's skill levels.
public IDictionary> Skills { get; } =
Enum
.GetValues(typeof(SkillType))
.Cast()
.ToDictionary(skill => 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;
}
/// Update the tracked values.
/// The player watcher to snapshot.
public void Update(PlayerTracker watcher)
{
this.Location.Update(watcher.LocationWatcher);
foreach (var pair in this.Skills)
pair.Value.Update(watcher.SkillWatchers[pair.Key]);
this.Inventory = watcher.TryGetInventoryChanges(out SnapshotItemListDiff itemChanges)
? itemChanges
: this.EmptyItemListDiff;
}
}
}