using System;
using System.Collections.Generic;
using System.Text;
using Substrate.Nbt;
using Substrate.Core;
namespace Substrate
{
///
/// Represents a TileTick record, which is used to queue a block for processing in the future.
///
public class TileTick : INbtObject, ICopyable
{
private static readonly SchemaNodeCompound _schema = new SchemaNodeCompound("")
{
new SchemaNodeScaler("i", TagType.TAG_INT),
new SchemaNodeScaler("t", TagType.TAG_INT),
new SchemaNodeScaler("x", TagType.TAG_INT),
new SchemaNodeScaler("y", TagType.TAG_INT),
new SchemaNodeScaler("z", TagType.TAG_INT),
};
private int _blockId;
private int _ticks;
private int _x;
private int _y;
private int _z;
private TagNodeCompound _source;
///
/// Constructs an empty object.
///
public TileTick ()
{
}
///
/// Constructs a by copying an existing one.
///
/// The to copy.
public TileTick (TileTick tt)
{
_blockId = tt._blockId;
_ticks = tt._ticks;
_x = tt._x;
_y = tt._y;
_z = tt._z;
if (tt._source != null) {
_source = tt._source.Copy() as TagNodeCompound;
}
}
///
/// Gets or sets the ID (type) of the block that this is associated with.
///
public int ID
{
get { return _blockId; }
set { _blockId = value; }
}
///
/// Gets or sets the number of ticks remaining until the associated block is processed.
///
public int Ticks
{
get { return _ticks; }
set { _ticks = value; }
}
///
/// Gets or sets the global X-coordinate of the block that this is associated with.
///
public int X
{
get { return _x; }
set { _x = value; }
}
///
/// Gets or sets the global Y-coordinate of the block that this is associated with.
///
public int Y
{
get { return _y; }
set { _y = value; }
}
///
/// Gets or sets the global Z-coordinate of the block that this is associated with.
///
public int Z
{
get { return _z; }
set { _z = value; }
}
///
/// Checks whether the 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 is located at the specified global coordinates.
public bool LocatedAt (int x, int y, int z)
{
return _x == x && _y == y && _z == z;
}
///
/// Moves the by given block offsets.
///
/// The X-offset to move by, in blocks.
/// The Y-offset to move by, in blocks.
/// The Z-offset to move by, in blocks.
public virtual void MoveBy (int diffX, int diffY, int diffZ)
{
_x += diffX;
_y += diffY;
_z += diffZ;
}
///
/// Attempt to construct a new from a Tile Entity subtree without validation.
///
/// The root node of a subtree.
/// A new on success, or null if the tree was unparsable.
public static TileTick FromTree (TagNode tree)
{
return new TileTick().LoadTree(tree);
}
///
/// Attempt to construct a new from a Tile Entity subtree with validation.
///
/// The root node of a subtree.
/// A new on success, or null if the tree failed validation.
public static TileTick FromTreeSafe (TagNode tree)
{
return new TileTick().LoadTreeSafe(tree);
}
#region INbtObject Members
///
/// Gets a representing the basic schema of a .
///
public static SchemaNodeCompound Schema
{
get { return _schema; }
}
///
/// Attempt to load a subtree into the without validation.
///
/// The root node of a subtree.
/// The returns itself on success, or null if the tree was unparsable.
public TileTick LoadTree (TagNode tree)
{
TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null) {
return null;
}
_blockId = ctree["i"].ToTagInt();
_ticks = ctree["t"].ToTagInt();
_x = ctree["x"].ToTagInt();
_y = ctree["y"].ToTagInt();
_z = ctree["z"].ToTagInt();
_source = ctree.Copy() as TagNodeCompound;
return this;
}
///
/// Attempt to load a subtree into the with validation.
///
/// The root node of a subtree.
/// The returns itself on success, or null if the tree failed validation.
public TileTick LoadTreeSafe (TagNode tree)
{
if (!ValidateTree(tree)) {
return null;
}
return LoadTree(tree);
}
///
/// Builds a subtree from the current data.
///
/// The root node of a subtree representing the current data.
public TagNode BuildTree ()
{
TagNodeCompound tree = new TagNodeCompound();
tree["i"] = new TagNodeInt(_blockId);
tree["t"] = new TagNodeInt(_ticks);
tree["x"] = new TagNodeInt(_x);
tree["y"] = new TagNodeInt(_y);
tree["z"] = new TagNodeInt(_z);
if (_source != null) {
tree.MergeFrom(_source);
}
return tree;
}
///
/// Validate a subtree against a basic schema.
///
/// The root node of a subtree.
/// Status indicating whether the tree was valid against the internal schema.
public bool ValidateTree (TagNode tree)
{
return new NbtVerifier(tree, _schema).Verify();
}
#endregion
#region ICopyable Members
///
/// Creates a deep-copy of the including any data defined in a subtype.
///
/// A deep-copy of the .
public virtual TileTick Copy ()
{
return new TileTick(this);
}
#endregion
}
}