using System.Collections.Generic; using System.Linq; using StardewValley; namespace Revitalize.Framework.Utilities { /// Handles dealing with objects. public class InventoryManager { /// How many items the inventory can hold. public int capacity; /// The hard uper limit for # of items to be held in case of upgrading or resizing. public int MaxCapacity { get; private set; } /// How many items are currently stored in the inventory. public int ItemCount => this.items.Count; /// The actual contents of the inventory. public List items; /// Checks if the inventory is full or not. public bool IsFull => this.ItemCount >= this.capacity && this.items.Where(i=>i==null).Count()==0; /// Checks to see if this core object actually has a valid inventory. public bool HasInventory => this.capacity > 0; public InventoryManager() { this.capacity = 0; this.setMaxLimit(0); this.items = new List(); } /// Construct an instance. public InventoryManager(List items) { this.capacity = int.MaxValue; this.setMaxLimit(int.MaxValue); this.items = items; } /// Construct an instance. public InventoryManager(int capacity) { this.capacity = capacity; this.MaxCapacity = int.MaxValue; this.items = new List(); } /// Construct an instance. public InventoryManager(int capacity, int MaxCapacity) { this.capacity = capacity; this.setMaxLimit(MaxCapacity); this.items = new List(); } /// Add the item to the inventory. public bool addItem(Item item) { if (this.IsFull) { return false; } else { foreach (Item self in this.items) { if (self != null && self.canStackWith(item)) { self.addToStack(item.Stack); return true; } } this.items.Add(item); return true; } } /// Gets a reference to the object IF it exists in the inventory. public Item getItem(Item item) { foreach (Item i in this.items) { if (item == i) return item; } return null; } /// Get the item at the specific index. public Item getItemAtIndex(int index) { return this.items[index]; } /// Gets only one item from the stack. public Item getSingleItemFromStack(Item item) { if (item.Stack == 1) return item; item.Stack = item.Stack - 1; return item.getOne(); } /// Empty the inventory. public void clear() { this.items.Clear(); } /// Empty the inventory. public void empty() { this.clear(); } /// Resize how many items can be held by this object. public void resizeCapacity(int Amount) { if (this.capacity + Amount < this.MaxCapacity) this.capacity += Amount; } /// Sets the upper limity of the capacity size for the inventory. public void setMaxLimit(int amount) { this.MaxCapacity = amount; } } }