using System;
using Substrate.Core;
using Substrate.Nbt;
namespace Substrate
{
///
/// Represents a Tile Entity record, which provides additional data to a block.
///
/// Generally, this class should be subtyped into new concrete Tile Entity types, as this generic type is unable to
/// capture any of the custom data fields that make Tile Entities useful in the first place. It is however still possible to
/// create instances of objects, which may allow for graceful handling of unknown Tile Entities.
public class TileEntity : INBTObject, ICopyable
{
private static readonly SchemaNodeCompound _schema = new SchemaNodeCompound("")
{
new SchemaNodeScaler("id", TagType.TAG_STRING),
new SchemaNodeScaler("x", TagType.TAG_INT),
new SchemaNodeScaler("y", TagType.TAG_INT),
new SchemaNodeScaler("z", TagType.TAG_INT),
};
private string _id;
private int _x;
private int _y;
private int _z;
///
/// Gets the id (name) of the Tile Entity.
///
public string ID
{
get { return _id; }
}
///
/// Gets or sets the global X-coordinate of the block that this Tile Entity is associated with.
///
public int X
{
get { return _x; }
set { _x = value; }
}
///
/// Gets or sets the global Y-coordinate of the block that this Tile Entity is associated with.
///
public int Y
{
get { return _y; }
set { _y = value; }
}
///
/// Gets or sets the global Z-coordinate of the block that this Tile Entity is associated with.
///
public int Z
{
get { return _z; }
set { _z = value; }
}
///
/// Constructs a nonspecific with a given ID.
///
/// The id (name) of the Tile Entity.
public TileEntity (string id)
{
_id = id;
}
///
/// Constructs a by copying an existing one.
///
/// The to copy.
public TileEntity (TileEntity te)
{
_id = te._id;
_x = te._x;
_y = te._y;
_z = te._z;
}
///
/// Checks whether the Tile Entity is located (associated with a block) at the specific global coordinates.
///
/// The global X-coordinate to test.
/// The global Y-coordinate to test.
/// The global Z-coordinate to test.
/// Status indicating whether the Tile Entity is located at the specified global coordinates.
public bool LocatedAt (int x, int y, int z)
{
return _x == x && _y == y && _z == z;
}
#region ICopyable Members
///
/// Creates a deep-copy of the including any data defined in a subtype.
///
/// A deep-copy of the .
public virtual TileEntity Copy ()
{
return new TileEntity(this);
}
#endregion
#region INBTObject Members
///
/// Gets a representing the basic schema of a Tile Entity.
///
public static SchemaNodeCompound Schema
{
get { return _schema; }
}
///
/// Attempt to load a Tile Entity subtree into the without validation.
///
/// The root node of a Tile Entity subtree.
/// The returns itself on success, or null if the tree was unparsable.
public virtual TileEntity LoadTree (TagNode tree)
{
TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null) {
return null;
}
_id = ctree["id"].ToTagString();
_x = ctree["x"].ToTagInt();
_y = ctree["y"].ToTagInt();
_z = ctree["z"].ToTagInt();
return this;
}
///
/// Attempt to load a Tile Entity subtree into the with validation.
///
/// The root node of a Tile Entity subtree.
/// The returns itself on success, or null if the tree failed validation.
public virtual TileEntity LoadTreeSafe (TagNode tree)
{
if (!ValidateTree(tree)) {
return null;
}
return LoadTree(tree);
}
///
/// Builds a Tile Entity subtree from the current data.
///
/// The root node of a Tile Entity subtree representing the current data.
public virtual TagNode BuildTree ()
{
TagNodeCompound tree = new TagNodeCompound();
tree["id"] = new TagNodeString(_id);
tree["x"] = new TagNodeInt(_x);
tree["y"] = new TagNodeInt(_y);
tree["z"] = new TagNodeInt(_z);
return tree;
}
///
/// Validate a Tile Entity subtree against a basic schema.
///
/// The root node of a Tile Entity subtree.
/// Status indicating whether the tree was valid against the internal schema.
public virtual bool ValidateTree (TagNode tree)
{
return new NBTVerifier(tree, _schema).Verify();
}
#endregion
}
}