2018-12-25 07:04:18 +08:00
|
|
|
using System.Collections.Generic;
|
2019-08-06 14:10:56 +08:00
|
|
|
using System.Linq;
|
2019-08-28 07:47:22 +08:00
|
|
|
using Newtonsoft.Json;
|
2018-12-30 18:00:05 +08:00
|
|
|
using StardewValley;
|
2018-12-25 07:04:18 +08:00
|
|
|
|
|
|
|
namespace Revitalize.Framework.Utilities
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Handles dealing with objects.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public class InventoryManager
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>How many items the inventory can hold.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public int capacity;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>The hard uper limit for # of items to be held in case of upgrading or resizing.</summary>
|
|
|
|
public int MaxCapacity { get; private set; }
|
2018-12-25 07:04:18 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>How many items are currently stored in the inventory.</summary>
|
2019-09-19 05:30:37 +08:00
|
|
|
public int ItemCount => this.items.Where(i => i != null).Count();
|
2018-12-25 07:04:18 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>The actual contents of the inventory.</summary>
|
2019-08-23 04:43:11 +08:00
|
|
|
public IList<Item> items;
|
2018-12-25 07:04:18 +08:00
|
|
|
|
2019-09-12 09:04:12 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Items that are to be buffered into the inventory manager if possible.
|
|
|
|
/// </summary>
|
|
|
|
public IList<Item> bufferItems;
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks if the inventory is full or not.</summary>
|
2019-08-06 14:10:56 +08:00
|
|
|
public bool IsFull => this.ItemCount >= this.capacity && this.items.Where(i=>i==null).Count()==0;
|
2018-12-25 07:04:18 +08:00
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Checks to see if this core object actually has a valid inventory.</summary>
|
|
|
|
public bool HasInventory => this.capacity > 0;
|
2018-12-25 07:06:42 +08:00
|
|
|
|
2019-09-12 09:04:12 +08:00
|
|
|
[JsonIgnore]
|
|
|
|
public bool hasItemsInBuffer
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return this.bufferItems.Count > 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-19 04:51:42 +08:00
|
|
|
public int displayRows;
|
|
|
|
public int displayColumns;
|
|
|
|
|
2019-08-28 07:47:22 +08:00
|
|
|
[JsonIgnore]
|
|
|
|
public bool requiresUpdate;
|
2018-12-25 07:04:18 +08:00
|
|
|
public InventoryManager()
|
|
|
|
{
|
|
|
|
this.capacity = 0;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.setMaxLimit(0);
|
2018-12-25 07:04:18 +08:00
|
|
|
this.items = new List<Item>();
|
2019-09-12 09:04:12 +08:00
|
|
|
this.bufferItems = new List<Item>();
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2019-09-19 04:51:42 +08:00
|
|
|
public InventoryManager(List<Item> items,int DisplayRows=6,int DisplayColumns=6)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.capacity = int.MaxValue;
|
|
|
|
this.setMaxLimit(int.MaxValue);
|
2018-12-25 07:04:18 +08:00
|
|
|
this.items = items;
|
2019-09-12 09:04:12 +08:00
|
|
|
this.bufferItems = new List<Item>();
|
2019-09-19 04:51:42 +08:00
|
|
|
this.displayRows = DisplayRows;
|
|
|
|
this.displayColumns = DisplayColumns;
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 15:18:45 +08:00
|
|
|
public InventoryManager(IList<Item> items, int Capacity= int.MaxValue, int DisplayRows = 6, int DisplayColumns = 6)
|
2019-08-23 04:43:11 +08:00
|
|
|
{
|
2019-10-09 15:18:45 +08:00
|
|
|
this.capacity = Capacity;
|
2019-08-23 04:43:11 +08:00
|
|
|
this.setMaxLimit(int.MaxValue);
|
|
|
|
this.items = items;
|
2019-09-12 09:04:12 +08:00
|
|
|
this.bufferItems = new List<Item>();
|
2019-09-19 04:51:42 +08:00
|
|
|
this.displayRows = DisplayRows;
|
|
|
|
this.displayColumns = DisplayColumns;
|
2019-08-23 04:43:11 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2019-09-19 04:51:42 +08:00
|
|
|
public InventoryManager(int capacity, int DisplayRows = 6, int DisplayColumns = 6)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
|
|
|
this.capacity = capacity;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.MaxCapacity = int.MaxValue;
|
2018-12-25 07:04:18 +08:00
|
|
|
this.items = new List<Item>();
|
2019-09-12 09:04:12 +08:00
|
|
|
this.bufferItems = new List<Item>();
|
2019-09-19 04:51:42 +08:00
|
|
|
this.displayRows = DisplayRows;
|
|
|
|
this.displayColumns = DisplayColumns;
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Construct an instance.</summary>
|
2019-09-19 04:51:42 +08:00
|
|
|
public InventoryManager(int capacity, int MaxCapacity, int DisplayRows = 6, int DisplayColumns = 6)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
|
|
|
this.capacity = capacity;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.setMaxLimit(MaxCapacity);
|
2018-12-25 07:04:18 +08:00
|
|
|
this.items = new List<Item>();
|
2019-09-12 09:04:12 +08:00
|
|
|
this.bufferItems = new List<Item>();
|
2019-09-19 04:51:42 +08:00
|
|
|
this.displayRows = DisplayRows;
|
|
|
|
this.displayColumns = DisplayColumns;
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Add the item to the inventory.</summary>
|
|
|
|
public bool addItem(Item item)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (this.IsFull)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-09-12 09:04:12 +08:00
|
|
|
for(int i = 0; i < this.items.Count; i++)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2019-09-12 09:04:12 +08:00
|
|
|
Item self = this.items[i];
|
2018-12-30 18:00:05 +08:00
|
|
|
if (self != null && self.canStackWith(item))
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
self.addToStack(item.Stack);
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-25 07:04:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-12 09:04:12 +08:00
|
|
|
if (self == null)
|
|
|
|
{
|
|
|
|
self = item;
|
|
|
|
this.requiresUpdate=true;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
2019-09-12 09:04:12 +08:00
|
|
|
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-30 18:00:05 +08:00
|
|
|
this.items.Add(item);
|
2018-12-25 07:04:18 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Gets a reference to the object IF it exists in the inventory.</summary>
|
|
|
|
public Item getItem(Item item)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
foreach (Item i in this.items)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (item == i)
|
|
|
|
return item;
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Get the item at the specific index.</summary>
|
|
|
|
public Item getItemAtIndex(int index)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
return this.items[index];
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Gets only one item from the stack.</summary>
|
|
|
|
public Item getSingleItemFromStack(Item item)
|
2018-12-25 07:04:18 +08:00
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (item.Stack == 1)
|
|
|
|
return item;
|
|
|
|
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-30 18:00:05 +08:00
|
|
|
item.Stack = item.Stack - 1;
|
|
|
|
return item.getOne();
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Empty the inventory.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public void clear()
|
|
|
|
{
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-25 07:04:18 +08:00
|
|
|
this.items.Clear();
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Empty the inventory.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public void empty()
|
|
|
|
{
|
|
|
|
this.clear();
|
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Resize how many items can be held by this object.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public void resizeCapacity(int Amount)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
if (this.capacity + Amount < this.MaxCapacity)
|
2019-08-28 07:47:22 +08:00
|
|
|
{
|
2018-12-25 07:04:18 +08:00
|
|
|
this.capacity += Amount;
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
|
|
|
}
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
|
2018-12-30 18:00:05 +08:00
|
|
|
/// <summary>Sets the upper limity of the capacity size for the inventory.</summary>
|
2018-12-25 07:04:18 +08:00
|
|
|
public void setMaxLimit(int amount)
|
|
|
|
{
|
2018-12-30 18:00:05 +08:00
|
|
|
this.MaxCapacity = amount;
|
2019-08-28 07:47:22 +08:00
|
|
|
this.requiresUpdate = true;
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
2019-08-17 06:32:14 +08:00
|
|
|
|
2019-09-20 05:44:46 +08:00
|
|
|
public bool canReceieveThisItem(Item I)
|
|
|
|
{
|
|
|
|
if (this.IsFull) return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 06:32:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Returns a new inventory manager without the items but with the capacity limits.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public InventoryManager Copy()
|
|
|
|
{
|
2019-09-19 04:51:42 +08:00
|
|
|
return new InventoryManager(this.capacity, this.MaxCapacity,this.displayRows,this.displayColumns);
|
2019-08-17 06:32:14 +08:00
|
|
|
}
|
2019-09-12 09:04:12 +08:00
|
|
|
|
|
|
|
public void dumpBufferToItems()
|
|
|
|
{
|
|
|
|
foreach(Item I in this.bufferItems)
|
|
|
|
{
|
|
|
|
this.addItem(I);
|
|
|
|
}
|
|
|
|
this.bufferItems.Clear();
|
|
|
|
}
|
2018-12-25 07:04:18 +08:00
|
|
|
}
|
|
|
|
}
|