using System;
using System.Collections.Generic;
using System.Text;
using Substrate.Core;
using Substrate.Nbt;
namespace Substrate
{
///
/// Represents an item (or item stack) within an item slot.
///
public class Item : INbtObject- , ICopyable
-
{
private static readonly SchemaNodeCompound _schema = new SchemaNodeCompound("")
{
new SchemaNodeScaler("id", TagType.TAG_SHORT),
new SchemaNodeScaler("Damage", TagType.TAG_SHORT),
new SchemaNodeScaler("Count", TagType.TAG_BYTE),
};
private TagNodeCompound _source;
private short _id;
private byte _count;
private short _damage;
///
/// Constructs an empty instance.
///
public Item ()
{
}
///
/// Constructs an instance representing the given item id.
///
/// An item id.
public Item (int id)
{
_id = (short)id;
}
#region Properties
///
/// Gets an entry for this item's type.
///
public ItemInfo Info
{
get { return ItemInfo.ItemTable[_id]; }
}
///
/// Gets or sets the current type (id) of the item.
///
public int ID
{
get { return _id; }
set { _id = (short)value; }
}
///
/// Gets or sets the damage value of the item.
///
/// The damage value may represent a generic data value for some items.
public int Damage
{
get { return _damage; }
set { _damage = (short)value; }
}
///
/// Gets or sets the number of this item stacked together in an item slot.
///
public int Count
{
get { return _count; }
set { _count = (byte)value; }
}
///
/// Gets a representing the schema of an item.
///
public static SchemaNodeCompound Schema
{
get { return _schema; }
}
#endregion
#region ICopyable
- Members
///
public Item Copy ()
{
Item item = new Item();
item._id = _id;
item._count = _count;
item._damage = _damage;
if (_source != null) {
item._source = _source.Copy() as TagNodeCompound;
}
return item;
}
#endregion
#region INBTObject
- Members
///
public Item LoadTree (TagNode tree)
{
TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null) {
return null;
}
_id = ctree["id"].ToTagShort();
_count = ctree["Count"].ToTagByte();
_damage = ctree["Damage"].ToTagShort();
_source = ctree.Copy() as TagNodeCompound;
return this;
}
///
public Item LoadTreeSafe (TagNode tree)
{
if (!ValidateTree(tree)) {
return null;
}
return LoadTree(tree);
}
///
public TagNode BuildTree ()
{
TagNodeCompound tree = new TagNodeCompound();
tree["id"] = new TagNodeShort(_id);
tree["Count"] = new TagNodeByte(_count);
tree["Damage"] = new TagNodeShort(_damage);
if (_source != null) {
tree.MergeFrom(_source);
}
return tree;
}
///
public bool ValidateTree (TagNode tree)
{
return new NbtVerifier(tree, _schema).Verify();
}
#endregion
}
///
/// 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
}
}