summaryrefslogtreecommitdiff
path: root/src/SMAPI/Events/InventoryChangedEventArgs.cs
blob: a081611b1749940b41d649420b2b68cf5523b065 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using StardewValley;

namespace StardewModdingAPI.Events
{
    /// <summary>Event arguments for an <see cref="IPlayerEvents.InventoryChanged"/> event.</summary>
    public class InventoryChangedEventArgs : EventArgs
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The player whose inventory changed.</summary>
        public Farmer Player { get; }

        /// <summary>The added items.</summary>
        public IEnumerable<Item> Added { get; }

        /// <summary>The removed items.</summary>
        public IEnumerable<Item> Removed { get; }

        /// <summary>The items whose stack sizes changed, with the relative change.</summary>
        public IEnumerable<ItemStackSizeChange> QuantityChanged { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="player">The player whose inventory changed.</param>
        /// <param name="changedItems">The inventory changes.</param>
        public InventoryChangedEventArgs(Farmer player, ItemStackChange[] changedItems)
        {
            this.Player = player;
            this.Added = changedItems
                .Where(n => n.ChangeType == ChangeType.Added)
                .Select(p => p.Item)
                .ToArray();

            this.Removed = changedItems
                .Where(n => n.ChangeType == ChangeType.Removed)
                .Select(p => p.Item)
                .ToArray();

            this.QuantityChanged = changedItems
                .Where(n => n.ChangeType == ChangeType.StackChange)
                .Select(change => new ItemStackSizeChange(
                    item: change.Item,
                    oldSize: change.Item.Stack - change.StackChange,
                    newSize: change.Item.Stack
                ))
                .ToArray();
        }
    }
}