Entity / TileEntity work

This commit is contained in:
Justin Aquadro 2011-04-05 01:34:21 +00:00
parent 4d87a5cc04
commit f60a1f3f77
5 changed files with 531 additions and 6 deletions

View file

@ -367,8 +367,8 @@ namespace NBToolkit.Map
Wool = new BlockInfo(35, "Wool");
YellowFlower = new BlockInfo(37, "Yellow Flower").SetOpacity(0);
RedRose = new BlockInfo(38, "Red Rose").SetOpacity(0);
BrownMushroom = new BlockInfo(39, "Brown Mushroom").SetOpacity(0);
RedMushroom = new BlockInfo(40, "Red Mushroom").SetOpacity(0);
BrownMushroom = new BlockInfo(39, "Brown Mushroom").SetOpacity(0).SetLuminance(1);
RedMushroom = new BlockInfo(40, "Red Mushroom").SetOpacity(0).SetLuminance(1);
GoldBlock = new BlockInfo(41, "Gold Block");
IronBlock = new BlockInfo(42, "Iron Block");
DoubleSlab = new BlockInfo(43, "Double Slab");

View file

@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit.Map
{
using NBT;
using Utility;
public class Entity
{
private NBT_Compound _tree;
public NBT_Compound Root
{
get { return _tree; }
}
public static readonly NBTCompoundNode BaseSchema = new NBTCompoundNode("")
{
new NBTScalerNode("id", NBT_Type.TAG_STRING),
new NBTListNode("Pos", NBT_Type.TAG_DOUBLE, 3),
new NBTListNode("Motion", NBT_Type.TAG_DOUBLE, 3),
new NBTListNode("Rotation", NBT_Type.TAG_FLOAT, 2),
new NBTScalerNode("FallDistance", NBT_Type.TAG_FLOAT),
new NBTScalerNode("Fire", NBT_Type.TAG_SHORT),
new NBTScalerNode("Air", NBT_Type.TAG_SHORT),
new NBTScalerNode("OnGround", NBT_Type.TAG_BYTE),
};
public static readonly NBTCompoundNode MobSchema = BaseSchema.MergeInto(new NBTCompoundNode("")
{
new NBTStringNode("id", "Mob"),
new NBTScalerNode("AttackTime", NBT_Type.TAG_SHORT),
new NBTScalerNode("DeathTime", NBT_Type.TAG_SHORT),
new NBTScalerNode("Health", NBT_Type.TAG_SHORT),
new NBTScalerNode("HurtTime", NBT_Type.TAG_SHORT),
});
public static readonly NBTCompoundNode MonsterSchema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTStringNode("id", "Monster"),
});
public static readonly NBTCompoundNode PigSchema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Saddle", NBT_Type.TAG_BYTE),
});
public static readonly NBTCompoundNode SheepSchema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Sheared", NBT_Type.TAG_BYTE),
new NBTScalerNode("Color", NBT_Type.TAG_BYTE),
});
public static readonly NBTCompoundNode Slimechema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Size", NBT_Type.TAG_INT),
});
public static readonly NBTCompoundNode WolfSchema = MobSchema.MergeInto(new NBTCompoundNode("")
{
new NBTScalerNode("Owner", NBT_Type.TAG_STRING),
new NBTScalerNode("Sitting", NBT_Type.TAG_BYTE),
new NBTScalerNode("Angry", NBT_Type.TAG_BYTE),
});
}
public class MobEntity : Entity
{
}
}

View file

@ -0,0 +1,190 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit.Map
{
using NBT;
public interface IItemContainer
{
ItemCollection Items { get; }
}
public class Item
{
protected NBT_Compound _tree;
public NBT_Compound Root
{
get { return _tree; }
}
public int ID
{
get { return _tree["id"].ToNBTShort(); }
set { _tree["id"].ToNBTShort().Data = (short)value; }
}
public int Damage
{
get { return _tree["Damage"].ToNBTShort(); }
set { _tree["Count"].ToNBTShort().Data = (short)value; }
}
public int Count
{
get { return _tree["Count"].ToNBTByte(); }
set { _tree["Count"].ToNBTByte().Data = (byte) value; }
}
public Item (NBT_Compound tree)
{
_tree = tree;
}
}
public class ItemCollection
{
protected NBT_List _list;
protected int _capacity;
public ItemCollection (NBT_List list, int capacity)
{
_list = list;
_capacity = capacity;
}
public int Capacity
{
get { return _capacity; }
}
public int Count
{
get { return _list.Count; }
}
public Item this [int slot]
{
get
{
foreach (NBT_Compound tag in _list) {
if (tag["Slot"].ToNBTByte() == slot) {
return new Item(tag);
}
}
return null;
}
set
{
if (slot < 0 || slot >= _capacity) {
return;
}
foreach (NBT_Compound tag in _list) {
if (tag["Slot"].ToNBTByte() == slot) {
_list.Remove(tag);
break;
}
}
_list.Add(value.Root);
}
}
public int GetItemID (int slot)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return 0;
}
return tag["id"].ToNBTShort();
}
public int GetItemCount (int slot)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return 0;
}
return tag["Count"].ToNBTByte();
}
public int GetItemDamage (int slot)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return 0;
}
return tag["Damage"].ToNBTShort();
}
public bool SetItemID (int slot, int id)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return false;
}
tag["id"].ToNBTShort().Data = (short) id;
return true;
}
public bool SetItemCount (int slot, int count)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return false;
}
tag["Count"].ToNBTByte().Data = (byte) count;
return true;
}
public bool SetItemDamage (int slot, int damage)
{
NBT_Compound tag = FindTag(slot);
if (tag == null) {
return false;
}
tag["Damage"].ToNBTShort().Data = (short)damage;
return true;
}
public bool ClearItem (int slot)
{
foreach (NBT_Compound tag in _list) {
if (tag["Slot"].ToNBTByte() == slot) {
_list.Remove(tag);
return true;
}
}
return false;
}
public bool ItemExists (int slot)
{
return FindTag(slot) != null;
}
public void ClearAllItems ()
{
_list.Clear();
}
private NBT_Compound FindTag (int slot)
{
foreach (NBT_Compound tag in _list) {
if (tag["Slot"].ToNBTByte() == slot) {
return tag;
}
}
return null;
}
}
}

View file

@ -159,6 +159,13 @@ namespace NBToolkit.Map.NBT
_type = type;
}
public NBTListNode (string name, NBT_Type type, int length)
: base(name)
{
_type = type;
_length = length;
}
public NBTListNode (string name, NBT_Type type, NBTSchemaNode subschema)
: base(name)
{

View file

@ -9,7 +9,7 @@ namespace NBToolkit.Map
public class TileEntity : ICopyable<TileEntity>
{
private NBT_Compound _tree;
protected NBT_Compound _tree;
public NBT_Compound Root
{
@ -58,7 +58,7 @@ namespace NBToolkit.Map
_tree = schema.BuildDefaultTree() as NBT_Compound;
}
public bool Verify ()
public virtual bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, BaseSchema);
return v.Verify();
@ -77,6 +77,15 @@ namespace NBToolkit.Map
_tree["z"].ToNBTInt().Data == z;
}
#region ICopyable<TileEntity> Members
public virtual TileEntity Copy ()
{
return new TileEntity(_tree.Copy() as NBT_Compound);
}
#endregion
#region Predefined Schemas
public static readonly NBTCompoundNode InventorySchema = new NBTCompoundNode("")
@ -138,12 +147,258 @@ namespace NBToolkit.Map
});
#endregion
}
public class TileEntityFurnace : TileEntity, IItemContainer
{
protected const int _capacity = 3;
protected ItemCollection _items;
public int BurnTime
{
get { return _tree["BurnTime"].ToNBTShort(); }
set { _tree["BurnTime"] = new NBT_Short((short)value); }
}
public int CookTime
{
get { return _tree["CookTime"].ToNBTShort(); }
set { _tree["CookTime"] = new NBT_Short((short)value); }
}
public TileEntityFurnace (NBT_Compound tree)
: base(tree)
{
NBT_List items = tree["Items"].ToNBTList();
if (items.Count == 0) {
tree["Items"] = new NBT_List(NBT_Type.TAG_COMPOUND);
items = _tree["Items"].ToNBTList();
}
_items = new ItemCollection(items, _capacity);
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.FurnaceSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public TileEntity Copy ()
public override TileEntity Copy ()
{
return new TileEntity(_tree.Copy() as NBT_Compound);
return new TileEntityFurnace(_tree.Copy() as NBT_Compound);
}
#endregion
#region IItemContainer Members
public ItemCollection Items
{
get { return _items; }
}
#endregion
}
public class TileEntitySign : TileEntity
{
public string Text1
{
get { return _tree["Text1"].ToNBTString(); }
set { _tree["Text1"] = new NBT_String(value.Substring(0, 12)); }
}
public string Text2
{
get { return _tree["Text2"].ToNBTString(); }
set { _tree["Text2"] = new NBT_String(value.Substring(0, 12)); }
}
public string Text3
{
get { return _tree["Text3"].ToNBTString(); }
set { _tree["Text3"] = new NBT_String(value.Substring(0, 12)); }
}
public string Text4
{
get { return _tree["Text4"].ToNBTString(); }
set { _tree["Text4"] = new NBT_String(value.Substring(0, 12)); }
}
public TileEntitySign (NBT_Compound tree)
: base(tree)
{
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.SignSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public override TileEntity Copy ()
{
return new TileEntitySign(_tree.Copy() as NBT_Compound);
}
#endregion
}
public class TileEntityMobSpawner : TileEntity
{
public int Delay
{
get { return _tree["Delay"].ToNBTShort(); }
set { _tree["Delay"] = new NBT_Short((short)value); }
}
public string EntityID
{
get { return _tree["EntityID"].ToNBTString(); }
set { _tree["EntityID"] = new NBT_String(value); }
}
public TileEntityMobSpawner (NBT_Compound tree)
: base(tree)
{
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.MobSpawnerSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public override TileEntity Copy ()
{
return new TileEntityMobSpawner(_tree.Copy() as NBT_Compound);
}
#endregion
}
public class TileEntityChest : TileEntity, IItemContainer
{
protected const int _capacity = 27;
protected ItemCollection _items;
public TileEntityChest (NBT_Compound tree)
: base(tree)
{
NBT_List items = tree["Items"].ToNBTList();
if (items.Count == 0) {
tree["Items"] = new NBT_List(NBT_Type.TAG_COMPOUND);
items = _tree["Items"].ToNBTList();
}
_items = new ItemCollection(items, _capacity);
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.ChestSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public override TileEntity Copy ()
{
return new TileEntityChest(_tree.Copy() as NBT_Compound);
}
#endregion
#region IItemContainer Members
public ItemCollection Items
{
get { return _items; }
}
#endregion
}
public class TileEntityMusic : TileEntity
{
public int Note
{
get { return _tree["Note"].ToNBTByte(); }
set { _tree["Note"] = new NBT_Byte((byte)value); }
}
public TileEntityMusic (NBT_Compound tree)
: base(tree)
{
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.MusicSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public override TileEntity Copy ()
{
return new TileEntityMusic(_tree.Copy() as NBT_Compound);
}
#endregion
}
public class TileEntityTrap : TileEntity, IItemContainer
{
protected const int _capacity = 8;
protected ItemCollection _items;
public TileEntityTrap (NBT_Compound tree)
: base(tree)
{
NBT_List items = tree["Items"].ToNBTList();
if (items.Count == 0) {
tree["Items"] = new NBT_List(NBT_Type.TAG_COMPOUND);
items = _tree["Items"].ToNBTList();
}
_items = new ItemCollection(items, _capacity);
}
public override bool Verify ()
{
NBTVerifier v = new NBTVerifier(Root, TileEntity.TrapSchema);
return v.Verify();
}
#region ICopyable<TileEntity> Members
public override TileEntity Copy ()
{
return new TileEntityTrap(_tree.Copy() as NBT_Compound);
}
#endregion
#region IItemContainer Members
public ItemCollection Items
{
get { return _items; }
}
#endregion