2011-04-06 04:43:54 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
2011-04-06 22:01:22 +00:00
|
|
|
|
namespace Substrate
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
using NBT;
|
|
|
|
|
using Utility;
|
|
|
|
|
|
|
|
|
|
public interface IItemContainer
|
|
|
|
|
{
|
|
|
|
|
ItemCollection Items { get; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Item : INBTObject<Item>, ICopyable<Item>
|
|
|
|
|
{
|
|
|
|
|
public static readonly NBTCompoundNode ItemSchema = new NBTCompoundNode("")
|
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
new NBTScalerNode("id", TagType.TAG_SHORT),
|
|
|
|
|
new NBTScalerNode("Damage", TagType.TAG_SHORT),
|
|
|
|
|
new NBTScalerNode("Count", TagType.TAG_BYTE),
|
2011-04-06 04:43:54 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private short _id;
|
|
|
|
|
private byte _count;
|
|
|
|
|
private short _damage;
|
|
|
|
|
|
|
|
|
|
public int ID
|
|
|
|
|
{
|
|
|
|
|
get { return _id; }
|
|
|
|
|
set { _id = (short)value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Damage
|
|
|
|
|
{
|
|
|
|
|
get { return _damage; }
|
|
|
|
|
set { _damage = (short)value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
get { return _count; }
|
|
|
|
|
set { _count = (byte)value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Item ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ICopyable<Item> Members
|
|
|
|
|
|
|
|
|
|
public Item Copy ()
|
|
|
|
|
{
|
|
|
|
|
Item item = new Item();
|
|
|
|
|
item._id = _id;
|
|
|
|
|
item._count = _count;
|
|
|
|
|
item._damage = _damage;
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region INBTObject<Item> Members
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public Item LoadTree (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
TagCompound ctree = tree as TagCompound;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
if (ctree == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
_id = ctree["id"].ToTagShort();
|
|
|
|
|
_count = ctree["Count"].ToTagByte();
|
|
|
|
|
_damage = ctree["Damage"].ToTagShort();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public Item LoadTreeSafe (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ValidateTree(tree)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LoadTree(tree);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public TagValue BuildTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
TagCompound tree = new TagCompound();
|
|
|
|
|
tree["id"] = new TagShort(_id);
|
|
|
|
|
tree["Count"] = new TagByte(_count);
|
|
|
|
|
tree["Damage"] = new TagShort(_damage);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public bool ValidateTree (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return new NBTVerifier(tree, ItemSchema).Verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ItemCollection : INBTObject<ItemCollection>, ICopyable<ItemCollection>
|
|
|
|
|
{
|
|
|
|
|
public static readonly NBTCompoundNode InventorySchema = Item.ItemSchema.MergeInto(new NBTCompoundNode("")
|
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
new NBTScalerNode("Slot", TagType.TAG_BYTE),
|
2011-04-06 04:43:54 +00:00
|
|
|
|
});
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public static readonly NBTListNode ListSchema = new NBTListNode("", TagType.TAG_COMPOUND, InventorySchema);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
protected Dictionary<int, Item> _items;
|
|
|
|
|
protected int _capacity;
|
|
|
|
|
|
|
|
|
|
public ItemCollection (int capacity)
|
|
|
|
|
{
|
|
|
|
|
_capacity = capacity;
|
|
|
|
|
_items = new Dictionary<int, Item>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Capacity
|
|
|
|
|
{
|
|
|
|
|
get { return _capacity; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int Count
|
|
|
|
|
{
|
|
|
|
|
get { return _items.Count; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool ItemExists (int slot)
|
|
|
|
|
{
|
|
|
|
|
return _items.ContainsKey(slot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Clear (int slot)
|
|
|
|
|
{
|
|
|
|
|
return _items.Remove(slot);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ClearAllItems ()
|
|
|
|
|
{
|
|
|
|
|
_items.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ICopyable<ItemCollection> Members
|
|
|
|
|
|
|
|
|
|
public ItemCollection Copy ()
|
|
|
|
|
{
|
|
|
|
|
ItemCollection ic = new ItemCollection(_capacity);
|
|
|
|
|
foreach (KeyValuePair<int, Item> item in _items) {
|
|
|
|
|
ic[item.Key] = item.Value.Copy();
|
|
|
|
|
}
|
|
|
|
|
return ic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region INBTObject<ItemCollection> Members
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public ItemCollection LoadTree (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
TagList ltree = tree as TagList;
|
2011-04-06 04:43:54 +00:00
|
|
|
|
if (ltree == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
foreach (TagCompound item in ltree) {
|
|
|
|
|
int slot = item["Slot"].ToTagByte();
|
2011-04-06 04:43:54 +00:00
|
|
|
|
_items[slot] = new Item().LoadTree(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public ItemCollection LoadTreeSafe (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
if (!ValidateTree(tree)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return LoadTree(tree);
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public TagValue BuildTree ()
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
2011-04-09 02:50:42 +00:00
|
|
|
|
TagList list = new TagList(TagType.TAG_COMPOUND);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
|
|
|
|
|
foreach (KeyValuePair<int, Item> item in _items) {
|
2011-04-09 02:50:42 +00:00
|
|
|
|
TagCompound itemtree = item.Value.BuildTree() as TagCompound;
|
|
|
|
|
itemtree["Slot"] = new TagByte((byte)item.Key);
|
2011-04-06 04:43:54 +00:00
|
|
|
|
list.Add(itemtree);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2011-04-09 02:50:42 +00:00
|
|
|
|
public bool ValidateTree (TagValue tree)
|
2011-04-06 04:43:54 +00:00
|
|
|
|
{
|
|
|
|
|
return new NBTVerifier(tree, ListSchema).Verify();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|