using System; using System.Collections.Generic; using Substrate.Nbt; using Substrate.Core; namespace Substrate { /// /// Represents a collection of items, such as a chest or an inventory. /// /// ItemCollections have a limited number of slots that depends on where they are used. public class ItemCollection : INbtObject, ICopyable { private static readonly SchemaNodeCompound _schema = Item.Schema.MergeInto(new SchemaNodeCompound("") { new SchemaNodeScaler("Slot", TagType.TAG_BYTE), }); private static readonly SchemaNodeList _listSchema = new SchemaNodeList("", TagType.TAG_COMPOUND, _schema); private Dictionary _items; private int _capacity; /// /// Constructs an with at most item slots. /// /// The upper bound on item slots available. /// The parameter does not necessarily indicate the true capacity of an item collection. /// The player object, for example, contains a conventional inventory, a range of invalid slots, and then equipment. Capacity in /// this case would refer to the highest equipment slot. public ItemCollection (int capacity) { _capacity = capacity; _items = new Dictionary(); } #region Properties /// /// Gets the capacity of the item collection. /// public int Capacity { get { return _capacity; } } /// /// Gets the current number of item slots actually used in the collection. /// public int Count { get { return _items.Count; } } /// /// Gets or sets an item in a given item slot. /// /// The item slot to query or insert an item or item stack into. public Item this[int slot] { get { Item item; _items.TryGetValue(slot, out item); return item; } set { if (slot < 0 || slot >= _capacity) { return; } _items[slot] = value; } } /// /// Gets a representing the schema of an item collection. /// public static SchemaNodeCompound Schema { get { return _schema; } } #endregion /// /// Checks if an item exists in the given item slot. /// /// The item slot to check. /// True if an item or stack of items exists in the given slot. public bool ItemExists (int slot) { return _items.ContainsKey(slot); } /// /// Removes an item from the given item slot, if it exists. /// /// The item slot to clear. /// True if an item was removed; false otherwise. public bool Clear (int slot) { return _items.Remove(slot); } /// /// Removes all items from the item collection. /// public void ClearAllItems () { _items.Clear(); } #region ICopyable Members /// public ItemCollection Copy () { ItemCollection ic = new ItemCollection(_capacity); foreach (KeyValuePair item in _items) { ic[item.Key] = item.Value.Copy(); } return ic; } #endregion #region INBTObject Members /// public ItemCollection LoadTree (TagNode tree) { TagNodeList ltree = tree as TagNodeList; if (ltree == null) { return null; } foreach (TagNodeCompound item in ltree) { int slot = item["Slot"].ToTagByte(); _items[slot] = new Item().LoadTree(item); } return this; } /// public ItemCollection LoadTreeSafe (TagNode tree) { if (!ValidateTree(tree)) { return null; } return LoadTree(tree); } /// public TagNode BuildTree () { TagNodeList list = new TagNodeList(TagType.TAG_COMPOUND); foreach (KeyValuePair item in _items) { TagNodeCompound itemtree = item.Value.BuildTree() as TagNodeCompound; itemtree["Slot"] = new TagNodeByte((byte)item.Key); list.Add(itemtree); } return list; } /// public bool ValidateTree (TagNode tree) { return new NbtVerifier(tree, _listSchema).Verify(); } #endregion } }