using System.Collections.Generic; using System.Linq; using Newtonsoft.Json; 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.Where(i => i != null).Count(); /// The actual contents of the inventory. public IList items; /// /// Items that are to be buffered into the inventory manager if possible. /// public IList bufferItems; /// 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; [JsonIgnore] public bool hasItemsInBuffer { get { return this.bufferItems.Count > 0; } } public int displayRows; public int displayColumns; [JsonIgnore] public bool requiresUpdate; public InventoryManager() { this.capacity = 0; this.setMaxLimit(0); this.items = new List(); this.bufferItems = new List(); } /// Construct an instance. public InventoryManager(List items,int DisplayRows=6,int DisplayColumns=6) { this.capacity = int.MaxValue; this.setMaxLimit(int.MaxValue); this.items = items; this.bufferItems = new List(); this.displayRows = DisplayRows; this.displayColumns = DisplayColumns; } public InventoryManager(IList items, int DisplayRows = 6, int DisplayColumns = 6) { this.capacity = int.MaxValue; this.setMaxLimit(int.MaxValue); this.items = items; this.bufferItems = new List(); this.displayRows = DisplayRows; this.displayColumns = DisplayColumns; } /// Construct an instance. public InventoryManager(int capacity, int DisplayRows = 6, int DisplayColumns = 6) { this.capacity = capacity; this.MaxCapacity = int.MaxValue; this.items = new List(); this.bufferItems = new List(); this.displayRows = DisplayRows; this.displayColumns = DisplayColumns; } /// Construct an instance. public InventoryManager(int capacity, int MaxCapacity, int DisplayRows = 6, int DisplayColumns = 6) { this.capacity = capacity; this.setMaxLimit(MaxCapacity); this.items = new List(); this.bufferItems = new List(); this.displayRows = DisplayRows; this.displayColumns = DisplayColumns; } /// Add the item to the inventory. public bool addItem(Item item) { if (this.IsFull) { return false; } else { for(int i = 0; i < this.items.Count; i++) { Item self = this.items[i]; if (self != null && self.canStackWith(item)) { self.addToStack(item.Stack); this.requiresUpdate = true; return true; } if (self == null) { self = item; this.requiresUpdate=true; return true; } } this.requiresUpdate = 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; this.requiresUpdate = true; item.Stack = item.Stack - 1; return item.getOne(); } /// Empty the inventory. public void clear() { this.requiresUpdate = true; 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; this.requiresUpdate = true; } } /// Sets the upper limity of the capacity size for the inventory. public void setMaxLimit(int amount) { this.MaxCapacity = amount; this.requiresUpdate = true; } public bool canReceieveThisItem(Item I) { if (this.IsFull) return false; else { return true; } } /// /// Returns a new inventory manager without the items but with the capacity limits. /// /// public InventoryManager Copy() { return new InventoryManager(this.capacity, this.MaxCapacity,this.displayRows,this.displayColumns); } public void dumpBufferToItems() { foreach(Item I in this.bufferItems) { this.addItem(I); } this.bufferItems.Clear(); } } }