forked from Pathoschild/StardewMods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrackedItemCollection.cs
114 lines (96 loc) · 3.72 KB
/
TrackedItemCollection.cs
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Generic;
using System.Linq;
using Pathoschild.Stardew.Common;
using StardewValley;
namespace Pathoschild.Stardew.Automate
{
/// <summary>An item stack which wraps an underlying collection of stacks.</summary>
public class TrackedItemCollection : ITrackedStack
{
/*********
** Fields
*********/
/// <summary>The underlying item stacks.</summary>
private readonly IList<ITrackedStack> Stacks = new List<ITrackedStack>();
/*********
** Accessors
*********/
/// <summary>A sample item for comparison.</summary>
/// <remarks>This should be equivalent to the underlying item (except in stack size), but *not* a reference to it.</remarks>
public Item Sample { get; private set; }
/// <summary>The underlying item type.</summary>
public ItemType Type { get; private set; } = ItemType.Unknown;
/// <summary>The number of items in the stack.</summary>
public int Count => this.Stacks.Sum(p => p.Count);
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="stacks">The underlying item stacks.</param>
public TrackedItemCollection(params ITrackedStack[] stacks)
{
foreach (ITrackedStack stack in stacks)
this.Add(stack);
}
/// <summary>Add a stack to the collection.</summary>
/// <param name="stack">The item stack to add.</param>
public void Add(ITrackedStack stack)
{
if (stack?.Sample == null)
throw new InvalidOperationException("Can't track an item with no underlying item.");
this.Stacks.Add(stack);
if (this.Sample == null)
{
this.Sample = stack.Sample;
this.Type = (ItemType)this.Sample.GetItemType();
}
}
/// <summary>Get whether the underlying items can stack with the items in another stack, based on their respective <see cref="Sample"/> values.</summary>
/// <param name="stack">The other stack to check.</param>
public bool CanStackWith(ITrackedStack stack)
{
if (stack?.Sample == null)
return false;
return this.Sample == null || this.Sample.canStackWith(stack.Sample);
}
/// <summary>Remove the specified number of this item from the stack.</summary>
/// <param name="count">The number to consume.</param>
public void Reduce(int count)
{
if (count <= 0 || !this.Stacks.Any())
return;
// reduce
int left = count;
foreach (ITrackedStack stack in this.Stacks)
{
// skip, stack empty
if (stack.Count <= 0)
continue;
// take entire stack
if (stack.Count < left)
{
left -= stack.Count;
stack.Reduce(stack.Count);
continue;
}
// take remaining items
stack.Reduce(left);
break;
}
}
/// <summary>Remove the specified number of this item from the stack and return a new stack matching the count.</summary>
/// <param name="count">The number to get.</param>
public Item Take(int count)
{
if (count <= 0 || !this.Stacks.Any())
return null;
// reduce
this.Reduce(count);
// create new stack
Item item = this.Sample.getOne();
item.Stack = count;
return item;
}
}
}