using System;
using System.Collections.Generic;
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),
new SchemaNodeCompound("tag", new SchemaNodeCompound("") {
new SchemaNodeList("ench", TagType.TAG_COMPOUND, Enchantment.Schema, SchemaOptions.OPTIONAL),
new SchemaNodeScaler("title", TagType.TAG_STRING, SchemaOptions.OPTIONAL),
new SchemaNodeScaler("author", TagType.TAG_STRING, SchemaOptions.OPTIONAL),
new SchemaNodeList("pages", TagType.TAG_STRING, SchemaOptions.OPTIONAL),
}, SchemaOptions.OPTIONAL),
};
private TagNodeCompound _source;
private short _id;
private byte _count;
private short _damage;
private List _enchantments;
///
/// Constructs an empty instance.
///
public Item ()
{
_enchantments = new List();
_source = new TagNodeCompound();
}
///
/// Constructs an instance representing the given item id.
///
/// An item id.
public Item (int id)
: this()
{
_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 the list of s applied to this item.
///
public IList Enchantments
{
get { return _enchantments; }
}
///
/// Gets the source used to create this if it exists.
///
public TagNodeCompound Source
{
get { return _source; }
}
///
/// 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;
foreach (Enchantment e in _enchantments) {
item._enchantments.Add(e.Copy());
}
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;
}
_enchantments.Clear();
_id = ctree["id"].ToTagShort();
_count = ctree["Count"].ToTagByte();
_damage = ctree["Damage"].ToTagShort();
if (ctree.ContainsKey("tag")) {
TagNodeCompound tagtree = ctree["tag"].ToTagCompound();
if (tagtree.ContainsKey("ench")) {
TagNodeList enchList = tagtree["ench"].ToTagList();
foreach (TagNode tag in enchList) {
_enchantments.Add(new Enchantment().LoadTree(tag));
}
}
}
_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 (_enchantments.Count > 0) {
TagNodeList enchList = new TagNodeList(TagType.TAG_COMPOUND);
foreach (Enchantment e in _enchantments) {
enchList.Add(e.BuildTree());
}
TagNodeCompound tagtree = new TagNodeCompound();
tagtree["ench"] = enchList;
if (_source != null && _source.ContainsKey("tag")) {
tagtree.MergeFrom(_source["tag"].ToTagCompound());
}
tree["tag"] = tagtree;
}
if (_source != null) {
tree.MergeFrom(_source);
}
return tree;
}
///
public bool ValidateTree (TagNode tree)
{
return new NbtVerifier(tree, _schema).Verify();
}
#endregion
}
}