More refactoring, more renaming, AlphaBlock and AlphaBlockRef documentation.

This commit is contained in:
Justin Aquadro 2011-06-30 03:59:20 +00:00
parent 8ed6977260
commit ec2342d767
42 changed files with 697 additions and 557 deletions

View file

@ -0,0 +1,188 @@
using System;
using Substrate.Core;
namespace Substrate
{
/// <summary>
/// A single Alpha-compatible block with context-independent data.
/// </summary>
/// <remarks><para>In general, you should prefer other types for accessing block data including <see cref="AlphaBlockRef"/>,
/// <see cref="BlockManager"/>, and the <see cref="AlphaBlockCollection"/> property of <see cref="Chunk"/> and<see cref="ChunkRef"/>.</para>
/// <para>You should use the <see cref="AlphaBlock"/> type when you need to copy individual blocks into a custom collection or
/// container, and context-depdendent data such as coordinates and lighting have no well-defined meaning. <see cref="AlphaBlock"/>
/// offers a relatively compact footprint for storing the unique identity of a block's manifestation in the world.</para>
/// <para>A single <see cref="AlphaBlock"/> object may also provide a convenient way to paste a block into many locations in
/// a block collection type.</para></remarks>
public class AlphaBlock : IDataBlock, IPropertyBlock, ICopyable<AlphaBlock>
{
private int _id;
private int _data;
private TileEntity _tileEntity;
/// <summary>
/// Create a new <see cref="AlphaBlock"/> instance of the given type with default data.
/// </summary>
/// <param name="id">The id (type) of the block.</param>
/// <remarks>If the specified block type requires a Tile Entity as part of its definition, a default
/// <see cref="TileEntity"/> of the appropriate type will automatically be created.</remarks>
public AlphaBlock (int id)
{
_id = id;
UpdateTileEntity(0, id);
}
/// <summary>
/// Create a new <see cref="AlphaBlock"/> instance of the given type and data value.
/// </summary>
/// <param name="id">The id (type) of the block.</param>
/// <param name="data">The block's supplementary data value, currently limited to the range [0-15].</param>
/// <remarks>If the specified block type requires a Tile Entity as part of its definition, a default
/// <see cref="TileEntity"/> of the appropriate type will automatically be created.</remarks>
public AlphaBlock (int id, int data)
{
_id = id;
_data = data;
UpdateTileEntity(0, id);
}
/// <summary>
/// Crrates a new <see cref="AlphaBlock"/> from a given block in an existing <see cref="AlphaBlockCollection"/>.
/// </summary>
/// <param name="chunk">The block collection to reference.</param>
/// <param name="lx">The local X-coordinate of a block within the collection.</param>
/// <param name="ly">The local Y-coordinate of a block within the collection.</param>
/// <param name="lz">The local Z-coordinate of a block within the collection.</param>
public AlphaBlock (IAlphaBlockCollection chunk, int lx, int ly, int lz)
{
_id = chunk.GetID(lx, ly, lz);
_data = chunk.GetData(lx, ly, lz);
_tileEntity = chunk.GetTileEntity(lx, ly, lz).Copy();
}
#region IBlock Members
/// <summary>
/// Gets information on the type of the block.
/// </summary>
public BlockInfo Info
{
get { return BlockInfo.BlockTable[_id]; }
}
/// <summary>
/// Gets or sets the id (type) of the block.
/// </summary>
/// <remarks>If the new or old type have non-matching Tile Entity requirements, the embedded Tile Entity data
/// will be updated to keep consistent with the new block type.</remarks>
public int ID
{
get { return _id; }
set
{
UpdateTileEntity(_id, value);
_id = value;
}
}
#endregion
#region IDataBlock Members
/// <summary>
/// Gets or sets the supplementary data value of the block.
/// </summary>
public int Data
{
get { return _data; }
set
{
/*if (BlockManager.EnforceDataLimits && BlockInfo.BlockTable[_id] != null) {
if (!BlockInfo.BlockTable[_id].TestData(value)) {
return;
}
}*/
_data = value;
}
}
#endregion
#region IPropertyBlock Members
/// <summary>
/// Gets the Tile Entity record of the block if it has one.
/// </summary>
/// <returns>The <see cref="TileEntity"/> attached to this block, or null if the block type does not require a Tile Entity.</returns>
public TileEntity GetTileEntity ()
{
return _tileEntity;
}
/// <summary>
/// Sets a new Tile Entity record for the block.
/// </summary>
/// <param name="te">A Tile Entity record compatible with the block's type.</param>
/// <exception cref="ArgumentException">Thrown when an incompatible <see cref="TileEntity"/> is added to a block.</exception>
/// <exception cref="InvalidOperationException">Thrown when a <see cref="TileEntity"/> is added to a block that does not use tile entities.</exception>
public void SetTileEntity (TileEntity te)
{
BlockInfoEx info = BlockInfo.BlockTable[_id] as BlockInfoEx;
if (info == null) {
throw new InvalidOperationException("The current block type does not accept a Tile Entity");
}
if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) {
throw new ArgumentException("The current block type is not compatible with the given Tile Entity", "te");
}
_tileEntity = te;
}
/// <summary>
/// Removes any Tile Entity currently attached to the block.
/// </summary>
public void ClearTileEntity ()
{
_tileEntity = null;
}
#endregion
#region ICopyable<Block> Members
/// <summary>
/// Creates a deep copy of the <see cref="AlphaBlock"/>.
/// </summary>
/// <returns>A new <see cref="AlphaBlock"/> representing the same data.</returns>
public AlphaBlock Copy ()
{
AlphaBlock block = new AlphaBlock(_id, _data);
block._tileEntity = _tileEntity.Copy();
return block;
}
#endregion
private void UpdateTileEntity (int old, int value)
{
BlockInfoEx info1 = BlockInfo.BlockTable[old] as BlockInfoEx;
BlockInfoEx info2 = BlockInfo.BlockTable[value] as BlockInfoEx;
if (info1 != info2) {
if (info1 != null) {
_tileEntity = null;
}
if (info2 != null) {
_tileEntity = TileEntityFactory.Create(info2.TileEntityName);
}
}
}
}
}

View file

@ -128,42 +128,42 @@ namespace Substrate
} }
/// <summary> /// <summary>
/// Returns a new <see cref="Block"/> object from local coordinates relative to this collection. /// Returns a new <see cref="AlphaBlock"/> object from local coordinates relative to this collection.
/// </summary> /// </summary>
/// <param name="x">Local X-coordinate of block.</param> /// <param name="x">Local X-coordinate of block.</param>
/// <param name="y">Local Y-coordinate of block.</param> /// <param name="y">Local Y-coordinate of block.</param>
/// <param name="z">Local Z-coordiante of block.</param> /// <param name="z">Local Z-coordiante of block.</param>
/// <returns>A new <see cref="Block"/> object representing context-independent data of a single block.</returns> /// <returns>A new <see cref="AlphaBlock"/> object representing context-independent data of a single block.</returns>
/// <remarks>Context-independent data excludes data such as lighting. <see cref="Block"/> object actually contain a copy /// <remarks>Context-independent data excludes data such as lighting. <see cref="AlphaBlock"/> object actually contain a copy
/// of the data they represent, so changes to the <see cref="Block"/> will not affect this container, and vice-versa.</remarks> /// of the data they represent, so changes to the <see cref="AlphaBlock"/> will not affect this container, and vice-versa.</remarks>
public Block GetBlock (int x, int y, int z) public AlphaBlock GetBlock (int x, int y, int z)
{ {
return new Block(this, x, y, z); return new AlphaBlock(this, x, y, z);
} }
/// <summary> /// <summary>
/// Returns a new <see cref="BlockRef"/> object from local coordaintes relative to this collection. /// Returns a new <see cref="AlphaBlockRef"/> object from local coordaintes relative to this collection.
/// </summary> /// </summary>
/// <param name="x">Local X-coordinate of block.</param> /// <param name="x">Local X-coordinate of block.</param>
/// <param name="y">Local Y-coordinate of block.</param> /// <param name="y">Local Y-coordinate of block.</param>
/// <param name="z">Local Z-coordinate of block.</param> /// <param name="z">Local Z-coordinate of block.</param>
/// <returns>A new <see cref="BlockRef"/> object representing context-dependent data of a single block.</returns> /// <returns>A new <see cref="AlphaBlockRef"/> object representing context-dependent data of a single block.</returns>
/// <remarks>Context-depdendent data includes all data associated with this block. Since a <see cref="BlockRef"/> represents /// <remarks>Context-depdendent data includes all data associated with this block. Since a <see cref="AlphaBlockRef"/> represents
/// a view of a block within this container, any updates to data in the container will be reflected in the <see cref="BlockRef"/>, /// a view of a block within this container, any updates to data in the container will be reflected in the <see cref="AlphaBlockRef"/>,
/// and vice-versa for updates to the <see cref="BlockRef"/>.</remarks> /// and vice-versa for updates to the <see cref="AlphaBlockRef"/>.</remarks>
public BlockRef GetBlockRef (int x, int y, int z) public AlphaBlockRef GetBlockRef (int x, int y, int z)
{ {
return new BlockRef(this, x, y, z); return new AlphaBlockRef(this, _blocks.GetIndex(x, y, z));
} }
/// <summary> /// <summary>
/// Updates a block in this collection with values from a <see cref="Block"/> object. /// Updates a block in this collection with values from a <see cref="AlphaBlock"/> object.
/// </summary> /// </summary>
/// <param name="x">Local X-coordinate of a block.</param> /// <param name="x">Local X-coordinate of a block.</param>
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <param name="block">A <see cref="Block"/> object to copy block data from.</param> /// <param name="block">A <see cref="AlphaBlock"/> object to copy block data from.</param>
public void SetBlock (int x, int y, int z, Block block) public void SetBlock (int x, int y, int z, AlphaBlock block)
{ {
SetID(x, y, z, block.ID); SetID(x, y, z, block.ID);
SetData(x, y, z, block.Data); SetData(x, y, z, block.Data);
@ -204,7 +204,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IBlock"/>-compatible object.</returns> /// <returns>An <see cref="IBlock"/>-compatible object.</returns>
/// <seealso cref="Block"/> /// <seealso cref="AlphaBlock"/>
IBlock IBlockCollection.GetBlock (int x, int y, int z) IBlock IBlockCollection.GetBlock (int x, int y, int z)
{ {
return GetBlock(x, y, z); return GetBlock(x, y, z);
@ -217,7 +217,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IBlock"/>-compatible reference object.</returns> /// <returns>An <see cref="IBlock"/>-compatible reference object.</returns>
/// <seealso cref="BlockRef"/> /// <seealso cref="AlphaBlockRef"/>
IBlock IBlockCollection.GetBlockRef (int x, int y, int z) IBlock IBlockCollection.GetBlockRef (int x, int y, int z)
{ {
return GetBlockRef(x, y, z); return GetBlockRef(x, y, z);
@ -339,13 +339,8 @@ namespace Substrate
internal void SetID (int index, int id) internal void SetID (int index, int id)
{ {
int yzdim = _ydim * _zdim; int x, y, z;
_blocks.GetMultiIndex(index, out x, out y, out z);
int x = index / yzdim;
int zy = index - (x * yzdim);
int z = zy / _ydim;
int y = zy - (z * _ydim);
SetID(x, y, z, id); SetID(x, y, z, id);
} }
@ -379,7 +374,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IDataBlock"/>-compatible object.</returns> /// <returns>An <see cref="IDataBlock"/>-compatible object.</returns>
/// <seealso cref="Block"/> /// <seealso cref="AlphaBlock"/>
IDataBlock IDataBlockCollection.GetBlock (int x, int y, int z) IDataBlock IDataBlockCollection.GetBlock (int x, int y, int z)
{ {
return GetBlock(x, y, z); return GetBlock(x, y, z);
@ -392,7 +387,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IDataBlock"/>-compatible reference object.</returns> /// <returns>An <see cref="IDataBlock"/>-compatible reference object.</returns>
/// <seealso cref="BlockRef"/> /// <seealso cref="AlphaBlockRef"/>
IDataBlock IDataBlockCollection.GetBlockRef (int x, int y, int z) IDataBlock IDataBlockCollection.GetBlockRef (int x, int y, int z)
{ {
return GetBlockRef(x, y, z); return GetBlockRef(x, y, z);
@ -487,7 +482,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="ILitBlock"/>-compatible object.</returns> /// <returns>An <see cref="ILitBlock"/>-compatible object.</returns>
/// <seealso cref="Block"/> /// <seealso cref="AlphaBlock"/>
ILitBlock ILitBlockCollection.GetBlock (int x, int y, int z) ILitBlock ILitBlockCollection.GetBlock (int x, int y, int z)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -500,7 +495,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="ILitBlock"/>-compatible reference object.</returns> /// <returns>An <see cref="ILitBlock"/>-compatible reference object.</returns>
/// <seealso cref="BlockRef"/> /// <seealso cref="AlphaBlockRef"/>
ILitBlock ILitBlockCollection.GetBlockRef (int x, int y, int z) ILitBlock ILitBlockCollection.GetBlockRef (int x, int y, int z)
{ {
return GetBlockRef(x, y, z); return GetBlockRef(x, y, z);
@ -785,7 +780,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IPropertyBlock"/>-compatible object.</returns> /// <returns>An <see cref="IPropertyBlock"/>-compatible object.</returns>
/// <seealso cref="Block"/> /// <seealso cref="AlphaBlock"/>
IPropertyBlock IPropertyBlockCollection.GetBlock (int x, int y, int z) IPropertyBlock IPropertyBlockCollection.GetBlock (int x, int y, int z)
{ {
return GetBlock(x, y, z); return GetBlock(x, y, z);
@ -798,7 +793,7 @@ namespace Substrate
/// <param name="y">Local Y-coordinate of a block.</param> /// <param name="y">Local Y-coordinate of a block.</param>
/// <param name="z">Local Z-coordinate of a block.</param> /// <param name="z">Local Z-coordinate of a block.</param>
/// <returns>An <see cref="IPropertyBlock"/>-compatible reference object.</returns> /// <returns>An <see cref="IPropertyBlock"/>-compatible reference object.</returns>
/// <seealso cref="BlockRef"/> /// <seealso cref="AlphaBlockRef"/>
IPropertyBlock IPropertyBlockCollection.GetBlockRef (int x, int y, int z) IPropertyBlock IPropertyBlockCollection.GetBlockRef (int x, int y, int z)
{ {
return GetBlockRef(x, y, z); return GetBlockRef(x, y, z);
@ -829,6 +824,14 @@ namespace Substrate
return _tileEntityManager.GetTileEntity(x, y, z); return _tileEntityManager.GetTileEntity(x, y, z);
} }
internal TileEntity GetTileEntity (int index)
{
int x, y, z;
_blocks.GetMultiIndex(index, out x, out y, out z);
return _tileEntityManager.GetTileEntity(x, y, z);
}
/// <summary> /// <summary>
/// Sets a <see cref="TileEntity"/> record for a block at the given local coordinates. /// Sets a <see cref="TileEntity"/> record for a block at the given local coordinates.
/// </summary> /// </summary>
@ -844,6 +847,15 @@ namespace Substrate
_dirty = true; _dirty = true;
} }
internal void SetTileEntity (int index, TileEntity te)
{
int x, y, z;
_blocks.GetMultiIndex(index, out x, out y, out z);
_tileEntityManager.SetTileEntity(x, y, z, te);
_dirty = true;
}
/// <summary> /// <summary>
/// Creates a default <see cref="TileEntity"/> record suitable for the block at the given local coordinates. /// Creates a default <see cref="TileEntity"/> record suitable for the block at the given local coordinates.
/// </summary> /// </summary>
@ -858,6 +870,15 @@ namespace Substrate
_dirty = true; _dirty = true;
} }
internal void CreateTileEntity (int index)
{
int x, y, z;
_blocks.GetMultiIndex(index, out x, out y, out z);
_tileEntityManager.CreateTileEntity(x, y, z);
_dirty = true;
}
/// <summary> /// <summary>
/// Clears any <see cref="TileEntity"/> record set for a block at the givne local coordinates, if one exists. /// Clears any <see cref="TileEntity"/> record set for a block at the givne local coordinates, if one exists.
/// </summary> /// </summary>
@ -870,6 +891,15 @@ namespace Substrate
_dirty = true; _dirty = true;
} }
internal void ClearTileEntity (int index)
{
int x, y, z;
_blocks.GetMultiIndex(index, out x, out y, out z);
_tileEntityManager.ClearTileEntity(x, y, z);
_dirty = true;
}
#endregion #endregion
public void ResetFluid () public void ResetFluid ()

View file

@ -0,0 +1,153 @@
using System;
using Substrate.Core;
//TODO: Benchmark struct vs. class. If no difference, prefer class.
namespace Substrate
{
/// <summary>
/// A reference to a single Alpha-compatible block in an <see cref="AlphaBlockCollection"/>.
/// </summary>
/// <remarks><para>The <see cref="AlphaBlockRef"/> type provides a reasonably lightweight reference to an individual block in a
/// <see cref="AlphaBlockCollection"/>. The <see cref="AlphaBLockRef"/> does not store any of the data itself. If the referenced
/// block in the <see cref="AlphaBlockCollection"/> is updated externally, those changes will be automatically reflected in the
/// <see cref="AlphaBlockRef"/>, and any changes made via the <see cref="AlphaBlockRef"/> will be applied directly to the corresponding
/// block within the <see cref="AlphaBlockCollection"/>. Such changes will also set the dirty status of the <see cref="AlphaBlockCollection"/>,
/// which can make this type particularly useful.</para>
/// <para>Despite being lightweight, using an <see cref="AlphaBlockRef"/> to get and set block data is still more expensive then directly
/// getting and setting data in the <see cref="AlphaBlockCollection"/> object, and can be significantly slow in a tight loop
/// (<see cref="AlphaBlockCollection"/> does not provide an interface for enumerating <see cref="AlphaBlockRef"/> objects specifically
/// to discourage this kind of use).</para>
/// <para><see cref="AlphaBlockRef"/> objects are most appropriate in cases where looking up an object requires expensive checks, such as
/// accessing blocks through a derived <see cref="BlockManager"/> type with enhanced block filtering. By getting an <see cref="AlphaBlockRef"/>,
/// any number of block attributes can be read or written to while only paying the lookup cost once to get the reference. Using the
/// <see cref="BlockManager"/> (or similar) directly would incur the expensive lookup on each operation. See NBToolkit for an example of this
/// use case.</para>
/// <para>Unlike the <see cref="AlphaBlock"/> object, this type exposed access to context-dependent data such as lighting.</para></remarks>
public struct AlphaBlockRef : IAlphaBlockRef
{
private readonly AlphaBlockCollection _collection;
private readonly int _index;
internal AlphaBlockRef (AlphaBlockCollection collection, int index)
{
_collection = collection;
_index = index;
}
public bool IsValid
{
get { return _collection != null; }
}
#region IBlock Members
/// <summary>
/// Gets information on the type of the block.
/// </summary>
public BlockInfo Info
{
get { return _collection.GetInfo(_index); }
}
/// <summary>
/// Gets or sets the id (type) of the block.
/// </summary>
public int ID
{
get
{
return _collection.GetID(_index);
}
set
{
_collection.SetID(_index, value);
}
}
#endregion
#region IDataBlock Members
/// <summary>
/// Gets or sets the supplementary data value of the block.
/// </summary>
public int Data
{
get
{
return _collection.GetData(_index);
}
set
{
_collection.SetData(_index, value);
}
}
#endregion
#region ILitBlock Members
/// <summary>
/// Gets or sets the block-source light component of the block.
/// </summary>
public int BlockLight
{
get
{
return _collection.GetBlockLight(_index);
}
set
{
_collection.SetBlockLight(_index, value);
}
}
/// <summary>
/// Gets or sets the sky-source light component of the block.
/// </summary>
public int SkyLight
{
get
{
return _collection.GetSkyLight(_index);
}
set
{
_collection.SetSkyLight(_index, value);
}
}
#endregion
#region IPropertyBlock Members
/// <summary>
/// Gets the Tile Entity record of the block if it has one.
/// </summary>
/// <returns>The <see cref="TileEntity"/> attached to this block, or null if the block type does not require a Tile Entity.</returns>
public TileEntity GetTileEntity ()
{
return _collection.GetTileEntity(_index);
}
/// <summary>
/// Sets a new Tile Entity record for the block.
/// </summary>
/// <param name="te">A Tile Entity record compatible with the block's type.</param>
public void SetTileEntity (TileEntity te)
{
_collection.SetTileEntity(_index, te);
}
/// <summary>
/// Removes any Tile Entity currently attached to the block.
/// </summary>
public void ClearTileEntity ()
{
_collection.ClearTileEntity(_index);
}
#endregion
}
}

View file

@ -1,131 +0,0 @@
using System;
using Substrate.Core;
using Substrate.NBT;
namespace Substrate
{
public class Block : IDataBlock, IPropertyBlock, ICopyable<Block>
{
private int _id;
private int _data;
private TileEntity _tileEntity;
public Block (int id)
{
_id = id;
UpdateTileEntity(0, id);
}
public Block (int id, int data)
{
_id = id;
_data = data;
UpdateTileEntity(0, id);
}
public Block (IAlphaBlockCollection chunk, int lx, int ly, int lz)
{
_id = chunk.GetID(lx, ly, lz);
_data = chunk.GetData(lx, ly, lz);
_tileEntity = chunk.GetTileEntity(lx, ly, lz).Copy();
}
#region IBlock Members
public BlockInfo Info
{
get { return BlockInfo.BlockTable[_id]; }
}
public int ID
{
get { return _id; }
set
{
UpdateTileEntity(_id, value);
_id = value;
}
}
#endregion
#region IDataBlock Members
public int Data
{
get { return _data; }
set
{
/*if (BlockManager.EnforceDataLimits && BlockInfo.BlockTable[_id] != null) {
if (!BlockInfo.BlockTable[_id].TestData(value)) {
return;
}
}*/
_data = value;
}
}
#endregion
#region IPropertyBlock Members
public TileEntity GetTileEntity ()
{
return _tileEntity;
}
public void SetTileEntity (TileEntity te)
{
BlockInfoEx info = BlockInfo.BlockTable[_id] as BlockInfoEx;
if (info == null) {
return;
}
if (te.GetType() != TileEntityFactory.Lookup(info.TileEntityName)) {
return;
}
_tileEntity = te;
}
public void ClearTileEntity ()
{
_tileEntity = null;
}
#endregion
#region ICopyable<Block> Members
public Block Copy ()
{
Block block = new Block(_id, _data);
block._tileEntity = _tileEntity.Copy();
return block;
}
#endregion
private void UpdateTileEntity (int old, int value)
{
BlockInfoEx info1 = BlockInfo.BlockTable[old] as BlockInfoEx;
BlockInfoEx info2 = BlockInfo.BlockTable[value] as BlockInfoEx;
if (info1 != info2) {
if (info1 != null) {
_tileEntity = null;
}
if (info2 != null) {
_tileEntity = TileEntityFactory.Create(info2.TileEntityName);
}
}
}
}
}

View file

@ -58,7 +58,7 @@ namespace Substrate
_chunkZLog = Log2(_chunkZDim); _chunkZLog = Log2(_chunkZDim);
} }
public Block GetBlock (int x, int y, int z) public AlphaBlock GetBlock (int x, int y, int z)
{ {
_cache = GetChunk(x, y, z); _cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) { if (_cache == null || !Check(x, y, z)) {
@ -68,17 +68,17 @@ namespace Substrate
return _cache.Blocks.GetBlock(x & _chunkXMask, y & _chunkYMask, z & _chunkZMask); return _cache.Blocks.GetBlock(x & _chunkXMask, y & _chunkYMask, z & _chunkZMask);
} }
public BlockRef GetBlockRef (int x, int y, int z) public AlphaBlockRef GetBlockRef (int x, int y, int z)
{ {
_cache = GetChunk(x, y, z); _cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) { if (_cache == null || !Check(x, y, z)) {
return null; return new AlphaBlockRef();
} }
return _cache.Blocks.GetBlockRef(x & _chunkXMask, y & _chunkYMask, z & _chunkZMask); return _cache.Blocks.GetBlockRef(x & _chunkXMask, y & _chunkYMask, z & _chunkZMask);
} }
public void SetBlock (int x, int y, int z, Block block) public void SetBlock (int x, int y, int z, AlphaBlock block)
{ {
_cache = GetChunk(x, y, z); _cache = GetChunk(x, y, z);
if (_cache == null || !Check(x, y, z)) { if (_cache == null || !Check(x, y, z)) {

View file

@ -1,213 +0,0 @@
using System;
using Substrate.Core;
namespace Substrate
{
public class BlockRef : IDataBlock, IPropertyBlock, ILitBlock
{
protected IAlphaBlockCollection _container;
protected int _x;
protected int _y;
protected int _z;
/*public int X
{
get { return _container.BlockGlobalX(_x); }
}
public int Y
{
get { return _container.BlockGlobalY(_y); }
}
public int Z
{
get { return _container.BlockGlobalZ(_z); }
}
public int LocalX
{
get { return _container.BlockLocalX(_x); }
}
public int LocalY
{
get { return _container.BlockLocalZ(_z); }
}
public int LocalZ
{
get { return _z; }
}*/
public BlockRef (IAlphaBlockCollection container, int x, int y, int z)
{
_container = container;
_x = x;
_y = y;
_z = z;
}
#region IBlock Members
public BlockInfo Info
{
get { return BlockInfo.BlockTable[_container.GetID(_x, _y, _z)]; }
}
public int ID
{
get { return _container.GetID(_x, _y, _z); }
set { _container.SetID(_x, _y, _z, value); }
}
public int Data
{
get { return _container.GetData(_x, _y, _z); }
set { _container.SetData(_x, _y, _z, value); }
}
#endregion
#region ILitBlock Members
public int BlockLight
{
get { return _container.GetBlockLight(_x, _y, _z); }
set { _container.SetBlockLight(_x, _y, _z, value); }
}
public int SkyLight
{
get { return _container.GetSkyLight(_x, _y, _z); }
set { _container.SetSkyLight(_x, _y, _z, value); }
}
#endregion
#region IPropertyBlock Members
public TileEntity GetTileEntity ()
{
return _container.GetTileEntity(_x, _y, _z);
}
public void SetTileEntity (TileEntity te)
{
_container.SetTileEntity(_x, _y, _z, te);
}
public void ClearTileEntity ()
{
_container.ClearTileEntity(_x, _y, _z);
}
#endregion
}
public struct AlphaBlockRef : IAlphaBlockRef
{
private readonly AlphaBlockCollection _collection;
private readonly int _index;
internal AlphaBlockRef (AlphaBlockCollection collection, int index)
{
_collection = collection;
_index = index;
}
#region IBlock Members
public BlockInfo Info
{
get { return _collection.GetInfo(_index); }
}
public int ID
{
get
{
return _collection.GetID(_index);
}
set
{
throw new NotImplementedException();
}
}
#endregion
#region IDataBlock Members
public int Data
{
get
{
return _collection.GetData(_index);
}
set
{
_collection.SetData(_index, value);
}
}
public void SetData (int value)
{
_collection.SetData(_index, value);
}
#endregion
#region ILitBlock Members
public int BlockLight
{
get
{
return _collection.GetBlockLight(_index);
}
set
{
_collection.SetBlockLight(_index, value);
}
}
public int SkyLight
{
get
{
return _collection.GetSkyLight(_index);
}
set
{
_collection.SetSkyLight(_index, value);
}
}
#endregion
#region IPropertyBlock Members
public TileEntity GetTileEntity ()
{
throw new NotImplementedException();
}
public void SetTileEntity (TileEntity te)
{
throw new NotImplementedException();
}
public void ClearTileEntity ()
{
throw new NotImplementedException();
}
#endregion
}
}

View file

@ -40,6 +40,7 @@ namespace Substrate.Core
public interface IAlphaBlockRef : IDataBlock, ILitBlock, IPropertyBlock public interface IAlphaBlockRef : IDataBlock, ILitBlock, IPropertyBlock
{ {
bool IsValid { get; }
} }
public interface IBlockCollection public interface IBlockCollection
@ -141,10 +142,10 @@ namespace Substrate.Core
public interface IAlphaBlockCollection : IDataBlockCollection, ILitBlockCollection, IPropertyBlockCollection public interface IAlphaBlockCollection : IDataBlockCollection, ILitBlockCollection, IPropertyBlockCollection
{ {
new Block GetBlock (int x, int y, int z); new AlphaBlock GetBlock (int x, int y, int z);
new BlockRef GetBlockRef (int x, int y, int z); new AlphaBlockRef GetBlockRef (int x, int y, int z);
void SetBlock (int x, int y, int z, Block block); void SetBlock (int x, int y, int z, AlphaBlock block);
} }
public interface IBoundedAlphaBlockCollection : IAlphaBlockCollection, IBoundedDataBlockCollection, IBoundedLitBlockCollection, IBoundedPropertyBlockCollection public interface IBoundedAlphaBlockCollection : IAlphaBlockCollection, IBoundedDataBlockCollection, IBoundedLitBlockCollection, IBoundedPropertyBlockCollection

View file

@ -94,6 +94,21 @@ namespace Substrate.Core
get { return _zdim; } get { return _zdim; }
} }
public int GetIndex (int x, int y, int z)
{
return _ydim * (x * _zdim + z) + y;
}
public void GetMultiIndex (int index, out int x, out int y, out int z)
{
int yzdim = _ydim * _zdim;
x = index / yzdim;
int zy = index - (x * yzdim);
z = zy / _ydim;
y = zy - (z * _ydim);
}
#region ICopyable<XZYByteArray> Members #region ICopyable<XZYByteArray> Members
public override ByteArray Copy () public override ByteArray Copy ()

View file

@ -2,7 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
namespace Substrate namespace Substrate.Core
{ {
public struct ChunkKey : IEquatable<ChunkKey> public struct ChunkKey : IEquatable<ChunkKey>
{ {

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityArrow (Entity e) public EntityArrow (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityArrow(this); return new EntityArrow(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityBoat : Entity public class EntityBoat : EntityTyped
{ {
public static readonly SchemaNodeCompound BoatSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound BoatSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "Boat"), new SchemaNodeString("id", "Boat"),
}); });
@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityBoat (Entity e) public EntityBoat (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityBoat(this); return new EntityBoat(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityChicken (Entity e) public EntityChicken (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityChicken(this); return new EntityChicken(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityCow (Entity e) public EntityCow (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityCow(this); return new EntityCow(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityCreeper (Entity e) public EntityCreeper (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityCreeper(this); return new EntityCreeper(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityEgg (Entity e) public EntityEgg (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityEgg(this); return new EntityEgg(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityFallingSand : Entity public class EntityFallingSand : EntityTyped
{ {
public static readonly SchemaNodeCompound FallingSandSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound FallingSandSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "FallingSand"), new SchemaNodeString("id", "FallingSand"),
new SchemaNodeScaler("Tile", TagType.TAG_BYTE), new SchemaNodeScaler("Tile", TagType.TAG_BYTE),
@ -27,7 +27,7 @@ namespace Substrate.Entities
{ {
} }
public EntityFallingSand (Entity e) public EntityFallingSand (EntityTyped e)
: base(e) : base(e)
{ {
EntityFallingSand e2 = e as EntityFallingSand; EntityFallingSand e2 = e as EntityFallingSand;
@ -39,7 +39,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -69,7 +69,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityFallingSand(this); return new EntityFallingSand(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityGhast (Entity e) public EntityGhast (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityGhast(this); return new EntityGhast(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityGiant (Entity e) public EntityGiant (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityGiant(this); return new EntityGiant(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityItem : Entity public class EntityItem : EntityTyped
{ {
public static readonly SchemaNodeCompound ItemSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound ItemSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "Item"), new SchemaNodeString("id", "Item"),
new SchemaNodeScaler("Health", TagType.TAG_SHORT), new SchemaNodeScaler("Health", TagType.TAG_SHORT),
@ -44,7 +44,7 @@ namespace Substrate.Entities
{ {
} }
public EntityItem (Entity e) public EntityItem (EntityTyped e)
: base(e) : base(e)
{ {
EntityItem e2 = e as EntityItem; EntityItem e2 = e as EntityItem;
@ -58,7 +58,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -93,7 +93,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityItem(this); return new EntityItem(this);
} }

View file

@ -6,7 +6,7 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityMinecart : Entity public class EntityMinecart : EntityTyped
{ {
public enum CartType public enum CartType
{ {
@ -15,7 +15,7 @@ namespace Substrate.Entities
FURNACE = 2, FURNACE = 2,
} }
public static readonly SchemaNodeCompound MinecartSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound MinecartSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "Minecart"), new SchemaNodeString("id", "Minecart"),
new SchemaNodeScaler("Type", TagType.TAG_BYTE), new SchemaNodeScaler("Type", TagType.TAG_BYTE),
@ -33,7 +33,7 @@ namespace Substrate.Entities
{ {
} }
public EntityMinecart (Entity e) public EntityMinecart (EntityTyped e)
: base(e) : base(e)
{ {
EntityMinecart e2 = e as EntityMinecart; EntityMinecart e2 = e as EntityMinecart;
@ -45,7 +45,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -84,7 +84,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityMinecart(this); return new EntityMinecart(this);
} }

View file

@ -23,7 +23,7 @@ namespace Substrate.Entities
_items = new ItemCollection(_CAPACITY); _items = new ItemCollection(_CAPACITY);
} }
public EntityMinecartChest (Entity e) public EntityMinecartChest (EntityTyped e)
: base(e) : base(e)
{ {
EntityMinecartChest e2 = e as EntityMinecartChest; EntityMinecartChest e2 = e as EntityMinecartChest;
@ -44,7 +44,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -75,7 +75,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityMinecartChest(this); return new EntityMinecartChest(this);
} }

View file

@ -42,7 +42,7 @@ namespace Substrate.Entities
{ {
} }
public EntityMinecartFurnace (Entity e) public EntityMinecartFurnace (EntityTyped e)
: base(e) : base(e)
{ {
EntityMinecartFurnace e2 = e as EntityMinecartFurnace; EntityMinecartFurnace e2 = e as EntityMinecartFurnace;
@ -56,7 +56,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -90,7 +90,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityMinecartFurnace(this); return new EntityMinecartFurnace(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityMob : Entity public class EntityMob : EntityTyped
{ {
public static readonly SchemaNodeCompound MobSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound MobSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "Mob"), new SchemaNodeString("id", "Mob"),
new SchemaNodeScaler("AttackTime", TagType.TAG_SHORT), new SchemaNodeScaler("AttackTime", TagType.TAG_SHORT),
@ -56,7 +56,7 @@ namespace Substrate.Entities
{ {
} }
public EntityMob (Entity e) public EntityMob (EntityTyped e)
: base(e) : base(e)
{ {
EntityMob e2 = e as EntityMob; EntityMob e2 = e as EntityMob;
@ -71,7 +71,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -107,7 +107,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityMob(this); return new EntityMob(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityMonster (Entity e) public EntityMonster (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityMonster(this); return new EntityMonster(this);
} }

View file

@ -6,7 +6,7 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityPainting : Entity public class EntityPainting : EntityTyped
{ {
public enum DirectionType public enum DirectionType
{ {
@ -16,7 +16,7 @@ namespace Substrate.Entities
SOUTH = 3, SOUTH = 3,
} }
public static readonly SchemaNodeCompound PaintingSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound PaintingSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "Painting"), new SchemaNodeString("id", "Painting"),
new SchemaNodeScaler("Dir", TagType.TAG_BYTE), new SchemaNodeScaler("Dir", TagType.TAG_BYTE),
@ -67,7 +67,7 @@ namespace Substrate.Entities
{ {
} }
public EntityPainting (Entity e) public EntityPainting (EntityTyped e)
: base(e) : base(e)
{ {
EntityPainting e2 = e as EntityPainting; EntityPainting e2 = e as EntityPainting;
@ -83,7 +83,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -121,7 +121,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityPainting(this); return new EntityPainting(this);
} }

View file

@ -27,7 +27,7 @@ namespace Substrate.Entities
{ {
} }
public EntityPig (Entity e) public EntityPig (EntityTyped e)
: base(e) : base(e)
{ {
EntityPig e2 = e as EntityPig; EntityPig e2 = e as EntityPig;
@ -39,7 +39,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -69,7 +69,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityPig(this); return new EntityPig(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityPigZombie (Entity e) public EntityPigZombie (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityPigZombie(this); return new EntityPigZombie(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityPrimedTnt : Entity public class EntityPrimedTnt : EntityTyped
{ {
public static readonly SchemaNodeCompound PrimedTntSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound PrimedTntSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeString("id", "PrimedTnt"), new SchemaNodeString("id", "PrimedTnt"),
new SchemaNodeScaler("Fuse", TagType.TAG_BYTE), new SchemaNodeScaler("Fuse", TagType.TAG_BYTE),
@ -27,7 +27,7 @@ namespace Substrate.Entities
{ {
} }
public EntityPrimedTnt (Entity e) public EntityPrimedTnt (EntityTyped e)
: base(e) : base(e)
{ {
EntityPrimedTnt e2 = e as EntityPrimedTnt; EntityPrimedTnt e2 = e as EntityPrimedTnt;
@ -39,7 +39,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -69,7 +69,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityPrimedTnt(this); return new EntityPrimedTnt(this);
} }

View file

@ -35,7 +35,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySheep (Entity e) public EntitySheep (EntityTyped e)
: base(e) : base(e)
{ {
EntitySheep e2 = e as EntitySheep; EntitySheep e2 = e as EntitySheep;
@ -48,7 +48,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -80,7 +80,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySheep(this); return new EntitySheep(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySkeleton (Entity e) public EntitySkeleton (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySkeleton(this); return new EntitySkeleton(this);
} }

View file

@ -27,7 +27,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySlime (Entity e) public EntitySlime (EntityTyped e)
: base(e) : base(e)
{ {
EntitySlime e2 = e as EntitySlime; EntitySlime e2 = e as EntitySlime;
@ -39,7 +39,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -69,7 +69,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySlime(this); return new EntitySlime(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySnowball (Entity e) public EntitySnowball (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySnowball(this); return new EntitySnowball(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySpider (Entity e) public EntitySpider (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySpider(this); return new EntitySpider(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntitySquid (Entity e) public EntitySquid (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntitySquid(this); return new EntitySquid(this);
} }

View file

@ -6,9 +6,9 @@ namespace Substrate.Entities
{ {
using Substrate.NBT; using Substrate.NBT;
public class EntityThrowable : Entity public class EntityThrowable : EntityTyped
{ {
public static readonly SchemaNodeCompound ThrowableSchema = BaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound ThrowableSchema = EntityTyped.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeScaler("xTile", TagType.TAG_SHORT), new SchemaNodeScaler("xTile", TagType.TAG_SHORT),
new SchemaNodeScaler("yTile", TagType.TAG_SHORT), new SchemaNodeScaler("yTile", TagType.TAG_SHORT),
@ -66,7 +66,7 @@ namespace Substrate.Entities
{ {
} }
public EntityThrowable (Entity e) public EntityThrowable (EntityTyped e)
: base(e) : base(e)
{ {
EntityThrowable e2 = e as EntityThrowable; EntityThrowable e2 = e as EntityThrowable;
@ -83,7 +83,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -123,7 +123,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityThrowable(this); return new EntityThrowable(this);
} }

View file

@ -43,7 +43,7 @@ namespace Substrate.Entities
{ {
} }
public EntityWolf (Entity e) public EntityWolf (EntityTyped e)
: base(e) : base(e)
{ {
EntityWolf e2 = e as EntityWolf; EntityWolf e2 = e as EntityWolf;
@ -57,7 +57,7 @@ namespace Substrate.Entities
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public override Entity LoadTree (TagNode tree) public override EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -91,7 +91,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityPig(this); return new EntityPig(this);
} }

View file

@ -18,7 +18,7 @@ namespace Substrate.Entities
{ {
} }
public EntityZombie (Entity e) public EntityZombie (EntityTyped e)
: base(e) : base(e)
{ {
} }
@ -36,7 +36,7 @@ namespace Substrate.Entities
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public override Entity Copy () public override EntityTyped Copy ()
{ {
return new EntityZombie(this); return new EntityZombie(this);
} }

View file

@ -6,18 +6,10 @@ using Substrate.NBT;
namespace Substrate namespace Substrate
{ {
public interface IEntityContainer /// <summary>
{ /// The base Entity type for Minecraft Entities, providing access to data common to all Minecraft Entities.
List<Entity> FindEntities (string id); /// </summary>
List<Entity> FindEntities (Predicate<Entity> match); public class Entity : INBTObject<Entity>, ICopyable<Entity>
bool AddEntity (Entity ent);
int RemoveEntities (string id);
int RemoveEntities (Predicate<Entity> match);
}
public class UntypedEntity : INBTObject<UntypedEntity>, ICopyable<UntypedEntity>
{ {
public class Vector3 public class Vector3
{ {
@ -32,7 +24,7 @@ namespace Substrate
public double Yaw { get; set; } public double Yaw { get; set; }
} }
public static readonly SchemaNodeCompound UTBaseSchema = new SchemaNodeCompound("") private static readonly SchemaNodeCompound _schema = new SchemaNodeCompound("")
{ {
new SchemaNodeList("Pos", TagType.TAG_DOUBLE, 3), new SchemaNodeList("Pos", TagType.TAG_DOUBLE, 3),
new SchemaNodeList("Motion", TagType.TAG_DOUBLE, 3), new SchemaNodeList("Motion", TagType.TAG_DOUBLE, 3),
@ -52,56 +44,84 @@ namespace Substrate
private short _air; private short _air;
private byte _onGround; private byte _onGround;
/// <summary>
/// Gets or sets the global position of the entity in fractional block coordinates.
/// </summary>
public Vector3 Position public Vector3 Position
{ {
get { return _pos; } get { return _pos; }
set { _pos = value; } set { _pos = value; }
} }
/// <summary>
/// Gets or sets the velocity of the entity.
/// </summary>
public Vector3 Motion public Vector3 Motion
{ {
get { return _motion; } get { return _motion; }
set { _motion = value; } set { _motion = value; }
} }
/// <summary>
/// Gets or sets the orientation of the entity.
/// </summary>
public Orientation Rotation public Orientation Rotation
{ {
get { return _rotation; } get { return _rotation; }
set { _rotation = value; } set { _rotation = value; }
} }
/// <summary>
/// Gets or sets the distance that the entity has fallen, if it is falling.
/// </summary>
public double FallDistance public double FallDistance
{ {
get { return _fallDistance; } get { return _fallDistance; }
set { _fallDistance = (float)value; } set { _fallDistance = (float)value; }
} }
/// <summary>
/// Gets or sets the fire counter of the entity.
/// </summary>
public int Fire public int Fire
{ {
get { return _fire; } get { return _fire; }
set { _fire = (short)value; } set { _fire = (short)value; }
} }
/// <summary>
/// Gets or sets the reamining air availale to the entity.
/// </summary>
public int Air public int Air
{ {
get { return _air; } get { return _air; }
set { _air = (short)value; } set { _air = (short)value; }
} }
/// <summary>
/// Gets or sets a value indicating whether the entity is currently touch the ground.
/// </summary>
public bool IsOnGround public bool IsOnGround
{ {
get { return _onGround == 1; } get { return _onGround == 1; }
set { _onGround = (byte)(value ? 1 : 0); } set { _onGround = (byte)(value ? 1 : 0); }
} }
public UntypedEntity () /// <summary>
/// Constructs a new generic <see cref="Entity"/> with default values.
/// </summary>
public Entity ()
{ {
_pos = new Vector3(); _pos = new Vector3();
_motion = new Vector3(); _motion = new Vector3();
_rotation = new Orientation(); _rotation = new Orientation();
} }
public UntypedEntity (UntypedEntity e) /// <summary>
/// Constructs a new generic <see cref="Entity"/> by copying fields from another <see cref="Entity"/> object.
/// </summary>
/// <param name="e">An <see cref="Entity"/> to copy fields from.</param>
public Entity (Entity e)
{ {
_pos = new Vector3(); _pos = new Vector3();
_pos.X = e._pos.X; _pos.X = e._pos.X;
@ -126,7 +146,20 @@ namespace Substrate
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public UntypedEntity LoadTree (TagNode tree) /// <summary>
/// Gets a <see cref="SchemaNode"/> representing the basic schema of an Entity.
/// </summary>
public static SchemaNodeCompound Schema
{
get { return _schema; }
}
/// <summary>
/// Attempt to load an Entity subtree into the <see cref="Entity"/> without validation.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>The <see cref="Entity"/> returns itself on success, or null if the tree was unparsable.</returns>
public Entity LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null) { if (ctree == null) {
@ -157,7 +190,12 @@ namespace Substrate
return this; return this;
} }
public UntypedEntity LoadTreeSafe (TagNode tree) /// <summary>
/// Attempt to load an Entity subtree into the <see cref="Entity"/> with validation.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>The <see cref="Entity"/> returns itself on success, or null if the tree failed validation.</returns>
public Entity LoadTreeSafe (TagNode tree)
{ {
if (!ValidateTree(tree)) { if (!ValidateTree(tree)) {
return null; return null;
@ -166,6 +204,10 @@ namespace Substrate
return LoadTree(tree); return LoadTree(tree);
} }
/// <summary>
/// Builds an Entity subtree from the current data.
/// </summary>
/// <returns>The root node of an Entity subtree representing the current data.</returns>
public TagNode BuildTree () public TagNode BuildTree ()
{ {
TagNodeCompound tree = new TagNodeCompound(); TagNodeCompound tree = new TagNodeCompound();
@ -195,9 +237,14 @@ namespace Substrate
return tree; return tree;
} }
/// <summary>
/// Validate an Entity subtree against a basic schema.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>Status indicating whether the tree was valid against the internal schema.</returns>
public bool ValidateTree (TagNode tree) public bool ValidateTree (TagNode tree)
{ {
return new NBTVerifier(tree, UTBaseSchema).Verify(); return new NBTVerifier(tree, _schema).Verify();
} }
#endregion #endregion
@ -205,35 +252,56 @@ namespace Substrate
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public UntypedEntity Copy () /// <summary>
/// Creates a deep-copy of the <see cref="Entity"/>.
/// </summary>
/// <returns>A deep-copy of the <see cref="Entity"/>.</returns>
public Entity Copy ()
{ {
return new UntypedEntity(this); return new Entity(this);
} }
#endregion #endregion
} }
public class Entity : UntypedEntity, INBTObject<Entity>, ICopyable<Entity> /// <summary>
/// A base entity type for all entities except <see cref="Player"/> entities.
/// </summary>
/// <remarks>Generally, this class should be subtyped into new concrete Entity types, as this generic type is unable to
/// capture any of the custom data fields. It is however still possible to create instances of <see cref="Entity"/> objects,
/// which may allow for graceful handling of unknown Entity types.</remarks>
public class EntityTyped : Entity, INBTObject<EntityTyped>, ICopyable<EntityTyped>
{ {
public static readonly SchemaNodeCompound BaseSchema = UTBaseSchema.MergeInto(new SchemaNodeCompound("") private static readonly SchemaNodeCompound _schema = Entity.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeScaler("id", TagType.TAG_STRING), new SchemaNodeScaler("id", TagType.TAG_STRING),
}); });
private string _id; private string _id;
/// <summary>
/// Gets the id (type) of the entity.
/// </summary>
public string ID public string ID
{ {
get { return _id; } get { return _id; }
} }
public Entity (string id) /// <summary>
/// Creates a new generic <see cref="EntityTyped"/> with the given id.
/// </summary>
/// <param name="id">The id (name) of the Entity.</param>
public EntityTyped (string id)
: base() : base()
{ {
_id = id; _id = id;
} }
public Entity (Entity e) /// <summary>
/// Constructs a new <see cref="EntityTyped"/> by copying an existing one.
/// </summary>
/// <param name="e">The <see cref="EntityTyped"/> to copy.</param>
public EntityTyped (EntityTyped e)
: base(e) : base(e)
{ {
_id = e._id; _id = e._id;
@ -242,7 +310,20 @@ namespace Substrate
#region INBTObject<Entity> Members #region INBTObject<Entity> Members
public virtual new Entity LoadTree (TagNode tree) /// <summary>
/// Gets a <see cref="SchemaNode"/> representing the basic schema of an Entity.
/// </summary>
public static SchemaNodeCompound Schema
{
get { return _schema; }
}
/// <summary>
/// Attempt to load an Entity subtree into the <see cref="EntityTyped"/> without validation.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>The <see cref="EntityTyped"/> returns itself on success, or null if the tree was unparsable.</returns>
public virtual new EntityTyped LoadTree (TagNode tree)
{ {
TagNodeCompound ctree = tree as TagNodeCompound; TagNodeCompound ctree = tree as TagNodeCompound;
if (ctree == null || base.LoadTree(tree) == null) { if (ctree == null || base.LoadTree(tree) == null) {
@ -254,7 +335,12 @@ namespace Substrate
return this; return this;
} }
public virtual new Entity LoadTreeSafe (TagNode tree) /// <summary>
/// Attempt to load an Entity subtree into the <see cref="EntityTyped"/> with validation.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>The <see cref="EntityTyped"/> returns itself on success, or null if the tree failed validation.</returns>
public virtual new EntityTyped LoadTreeSafe (TagNode tree)
{ {
if (!ValidateTree(tree)) { if (!ValidateTree(tree)) {
return null; return null;
@ -263,6 +349,10 @@ namespace Substrate
return LoadTree(tree); return LoadTree(tree);
} }
/// <summary>
/// Builds an Entity subtree from the current data.
/// </summary>
/// <returns>The root node of an Entity subtree representing the current data.</returns>
public virtual new TagNode BuildTree () public virtual new TagNode BuildTree ()
{ {
TagNodeCompound tree = base.BuildTree() as TagNodeCompound; TagNodeCompound tree = base.BuildTree() as TagNodeCompound;
@ -271,9 +361,14 @@ namespace Substrate
return tree; return tree;
} }
/// <summary>
/// Validate an Entity subtree against a basic schema.
/// </summary>
/// <param name="tree">The root node of an Entity subtree.</param>
/// <returns>Status indicating whether the tree was valid against the internal schema.</returns>
public virtual new bool ValidateTree (TagNode tree) public virtual new bool ValidateTree (TagNode tree)
{ {
return new NBTVerifier(tree, BaseSchema).Verify(); return new NBTVerifier(tree, _schema).Verify();
} }
#endregion #endregion
@ -281,9 +376,13 @@ namespace Substrate
#region ICopyable<Entity> Members #region ICopyable<Entity> Members
public virtual new Entity Copy () /// <summary>
/// Creates a deep-copy of the <see cref="EntityTyped"/>.
/// </summary>
/// <returns>A deep-copy of the <see cref="EntityTyped"/>.</returns>
public virtual new EntityTyped Copy ()
{ {
return new Entity(this); return new EntityTyped(this);
} }
#endregion #endregion

View file

@ -6,7 +6,7 @@ namespace Substrate
{ {
using NBT; using NBT;
public class EntityCollection : IEnumerable<Entity> public class EntityCollection : IEnumerable<EntityTyped>
{ {
private TagNodeList _entities; private TagNodeList _entities;
@ -23,9 +23,9 @@ namespace Substrate
_entities = entities; _entities = entities;
} }
public List<Entity> FindAll (string id) public List<EntityTyped> FindAll (string id)
{ {
List<Entity> set = new List<Entity>(); List<EntityTyped> set = new List<EntityTyped>();
foreach (TagNodeCompound ent in _entities) { foreach (TagNodeCompound ent in _entities) {
TagNode eid; TagNode eid;
@ -37,7 +37,7 @@ namespace Substrate
continue; continue;
} }
Entity obj = EntityFactory.Create(ent); EntityTyped obj = EntityFactory.Create(ent);
if (obj != null) { if (obj != null) {
set.Add(obj); set.Add(obj);
} }
@ -46,12 +46,12 @@ namespace Substrate
return set; return set;
} }
public List<Entity> FindAll (Predicate<Entity> match) public List<EntityTyped> FindAll (Predicate<EntityTyped> match)
{ {
List<Entity> set = new List<Entity>(); List<EntityTyped> set = new List<EntityTyped>();
foreach (TagNodeCompound ent in _entities) { foreach (TagNodeCompound ent in _entities) {
Entity obj = EntityFactory.Create(ent); EntityTyped obj = EntityFactory.Create(ent);
if (obj == null) { if (obj == null) {
continue; continue;
} }
@ -64,7 +64,7 @@ namespace Substrate
return set; return set;
} }
public bool Add (Entity ent) public bool Add (EntityTyped ent)
{ {
/*double xlow = _cx * XDim; /*double xlow = _cx * XDim;
double xhigh = xlow + XDim; double xhigh = xlow + XDim;
@ -106,7 +106,7 @@ namespace Substrate
return rem; return rem;
} }
public int RemoveAll (Predicate<Entity> match) public int RemoveAll (Predicate<EntityTyped> match)
{ {
int rem = _entities.RemoveAll(val => int rem = _entities.RemoveAll(val =>
{ {
@ -115,7 +115,7 @@ namespace Substrate
return false; return false;
} }
Entity obj = EntityFactory.Create(cval); EntityTyped obj = EntityFactory.Create(cval);
if (obj == null) { if (obj == null) {
return false; return false;
} }
@ -132,7 +132,7 @@ namespace Substrate
#region IEnumerable<Entity> Members #region IEnumerable<Entity> Members
public IEnumerator<Entity> GetEnumerator () public IEnumerator<EntityTyped> GetEnumerator ()
{ {
return new EntityEnumerator(_entities); return new EntityEnumerator(_entities);
} }
@ -148,11 +148,11 @@ namespace Substrate
#endregion #endregion
public class EntityEnumerator : IEnumerator<Entity> public class EntityEnumerator : IEnumerator<EntityTyped>
{ {
private IEnumerator<TagNode> _enum; private IEnumerator<TagNode> _enum;
private Entity _cur; private EntityTyped _cur;
public EntityEnumerator (TagNodeList entities) public EntityEnumerator (TagNodeList entities)
{ {
@ -161,7 +161,7 @@ namespace Substrate
#region IEnumerator<Entity> Members #region IEnumerator<Entity> Members
public Entity Current public EntityTyped Current
{ {
get get
{ {

View file

@ -1,42 +1,41 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Substrate.Entities;
using Substrate.NBT;
namespace Substrate namespace Substrate
{ {
using NBT;
using Entities;
/// <summary> /// <summary>
/// Creates new instances of concrete <see cref="Entity"/> types from a dynamic registry. /// Creates new instances of concrete <see cref="EntityTyped"/> types from a dynamic registry.
/// </summary> /// </summary>
/// <remarks>This factory allows specific <see cref="Entity"/> objects to be generated as an NBT tree is parsed. New types can be /// <remarks>This factory allows specific <see cref="EntityTyped"/> objects to be generated as an NBT tree is parsed. New types can be
/// registered with the factory at any time, so that custom <see cref="Entity"/> types can be supported. By default, the standard /// registered with the factory at any time, so that custom <see cref="EntityTyped"/> types can be supported. By default, the standard
/// Entities of Minecraft are registered with the factory at startup and bound to their respective 'id' fields.</remarks> /// Entities of Minecraft are registered with the factory at startup and bound to their respective 'id' fields.</remarks>
public class EntityFactory public class EntityFactory
{ {
private static Dictionary<string, Type> _registry; private static Dictionary<string, Type> _registry;
/// <summary> /// <summary>
/// Create a new instance of a concrete <see cref="Entity"/> type by name. /// Create a new instance of a concrete <see cref="EntityTyped"/> type by name.
/// </summary> /// </summary>
/// <param name="type">The name that a concrete <see cref="Entity"/> type was registered with.</param> /// <param name="type">The name that a concrete <see cref="EntityTyped"/> type was registered with.</param>
/// <returns>A new instance of a concrete <see cref="Entity"/> type, or null if no type was registered with the given name.</returns> /// <returns>A new instance of a concrete <see cref="EntityTyped"/> type, or null if no type was registered with the given name.</returns>
public static Entity Create (string type) public static EntityTyped Create (string type)
{ {
Type t; Type t;
if (!_registry.TryGetValue(type, out t)) { if (!_registry.TryGetValue(type, out t)) {
return null; return null;
} }
return Activator.CreateInstance(t) as Entity; return Activator.CreateInstance(t) as EntityTyped;
} }
/// <summary> /// <summary>
/// Create a new instance of a concrete <see cref="Entity"/> type by NBT node. /// Create a new instance of a concrete <see cref="EntityTyped"/> type by NBT node.
/// </summary> /// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Entity, containing an 'id' field of the Entity's registered name.</param> /// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Entity, containing an 'id' field of the Entity's registered name.</param>
/// <returns>A new instance of a concrete <see cref="Entity"/> type, or null if no type was registered with the given name.</returns> /// <returns>A new instance of a concrete <see cref="EntityTyped"/> type, or null if no type was registered with the given name.</returns>
public static Entity Create (TagNodeCompound tree) public static EntityTyped Create (TagNodeCompound tree)
{ {
TagNode type; TagNode type;
if (!tree.TryGetValue("id", out type)) { if (!tree.TryGetValue("id", out type)) {
@ -48,16 +47,16 @@ namespace Substrate
return null; return null;
} }
Entity te = Activator.CreateInstance(t) as Entity; EntityTyped te = Activator.CreateInstance(t) as EntityTyped;
return te.LoadTreeSafe(tree); return te.LoadTreeSafe(tree);
} }
/// <summary> /// <summary>
/// Lookup a concrete <see cref="Entity"/> type by name. /// Lookup a concrete <see cref="EntityTyped"/> type by name.
/// </summary> /// </summary>
/// <param name="type">The name that a concrete <see cref="Entity"/> type was registered with.</param> /// <param name="type">The name that a concrete <see cref="EntityTyped"/> type was registered with.</param>
/// <returns>The <see cref="Type"/> of a concrete <see cref="Entity"/> type, or null if no type was registered with the given name.</returns> /// <returns>The <see cref="Type"/> of a concrete <see cref="EntityTyped"/> type, or null if no type was registered with the given name.</returns>
public static Type Lookup (string type) public static Type Lookup (string type)
{ {
Type t; Type t;
@ -69,10 +68,10 @@ namespace Substrate
} }
/// <summary> /// <summary>
/// Registers a new concrete <see cref="Entity"/> type with the <see cref="EntityFactory"/>, binding it to a given name. /// Registers a new concrete <see cref="EntityTyped"/> type with the <see cref="EntityFactory"/>, binding it to a given name.
/// </summary> /// </summary>
/// <param name="id">The name to bind to a concrete <see cref="Entity"/> type.</param> /// <param name="id">The name to bind to a concrete <see cref="EntityTyped"/> type.</param>
/// <param name="subtype">The <see cref="Type"/> of a concrete <see cref="Entity"/> type.</param> /// <param name="subtype">The <see cref="Type"/> of a concrete <see cref="EntityTyped"/> type.</param>
public static void Register (string id, Type subtype) public static void Register (string id, Type subtype)
{ {
_registry[id] = subtype; _registry[id] = subtype;

View file

@ -6,9 +6,9 @@ using Substrate.NBT;
namespace Substrate namespace Substrate
{ {
public class Player : UntypedEntity, INBTObject<Player>, ICopyable<Player>, IItemContainer public class Player : Entity, INBTObject<Player>, ICopyable<Player>, IItemContainer
{ {
public static readonly SchemaNodeCompound PlayerSchema = UTBaseSchema.MergeInto(new SchemaNodeCompound("") public static readonly SchemaNodeCompound PlayerSchema = Entity.Schema.MergeInto(new SchemaNodeCompound("")
{ {
new SchemaNodeScaler("AttackTime", TagType.TAG_SHORT), new SchemaNodeScaler("AttackTime", TagType.TAG_SHORT),
new SchemaNodeScaler("DeathTime", TagType.TAG_SHORT), new SchemaNodeScaler("DeathTime", TagType.TAG_SHORT),

View file

@ -62,15 +62,17 @@
<Reference Include="System" /> <Reference Include="System" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Source\BlockFluid.cs" /> <Compile Include="Source\AlphaBlock.cs" />
<Compile Include="Source\AlphaBlockRef.cs" />
<Compile Include="Source\Core\BlockFluid.cs" />
<Compile Include="Source\Data.cs" /> <Compile Include="Source\Data.cs" />
<Compile Include="Source\ItemInfo.cs" /> <Compile Include="Source\ItemInfo.cs" />
<Compile Include="Source\ChunkCache.cs" /> <Compile Include="Source\Core\ChunkCache.cs" />
<Compile Include="Source\Entities\EntitySquid.cs" /> <Compile Include="Source\Entities\EntitySquid.cs" />
<Compile Include="Source\EntityCollection.cs" /> <Compile Include="Source\EntityCollection.cs" />
<Compile Include="Source\AlphaBlockCollection.cs" /> <Compile Include="Source\AlphaBlockCollection.cs" />
<Compile Include="Source\BlockLight.cs" /> <Compile Include="Source\Core\BlockLight.cs" />
<Compile Include="Source\BlockTileEntities.cs" /> <Compile Include="Source\Core\BlockTileEntities.cs" />
<Compile Include="Source\Level.cs" /> <Compile Include="Source\Level.cs" />
<Compile Include="Source\NBT\INBTObject.cs" /> <Compile Include="Source\NBT\INBTObject.cs" />
<Compile Include="Source\NBT\SchemaNode.cs" /> <Compile Include="Source\NBT\SchemaNode.cs" />
@ -95,19 +97,17 @@
<Compile Include="Source\NBT\TagNodeNull.cs" /> <Compile Include="Source\NBT\TagNodeNull.cs" />
<Compile Include="Source\NBT\VerifierLogger.cs" /> <Compile Include="Source\NBT\VerifierLogger.cs" />
<Compile Include="Source\PlayerManager.cs" /> <Compile Include="Source\PlayerManager.cs" />
<Compile Include="Source\PlayerFile.cs" /> <Compile Include="Source\Core\PlayerFile.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Source\Block.cs" />
<Compile Include="Source\BlockInfo.cs" /> <Compile Include="Source\BlockInfo.cs" />
<Compile Include="Source\BlockInterface.cs" /> <Compile Include="Source\Core\BlockInterface.cs" />
<Compile Include="Source\BlockKey.cs" /> <Compile Include="Source\Core\BlockKey.cs" />
<Compile Include="Source\BlockManager.cs" /> <Compile Include="Source\BlockManager.cs" />
<Compile Include="Source\BlockRef.cs" />
<Compile Include="Source\Chunk.cs" /> <Compile Include="Source\Chunk.cs" />
<Compile Include="Source\ChunkFile.cs" /> <Compile Include="Source\Core\ChunkFile.cs" />
<Compile Include="Source\ChunkFileManager.cs" /> <Compile Include="Source\Core\ChunkFileManager.cs" />
<Compile Include="Source\ChunkInterface.cs" /> <Compile Include="Source\Core\ChunkInterface.cs" />
<Compile Include="Source\ChunkKey.cs" /> <Compile Include="Source\Core\ChunkKey.cs" />
<Compile Include="Source\ChunkManager.cs" /> <Compile Include="Source\ChunkManager.cs" />
<Compile Include="Source\ChunkRef.cs" /> <Compile Include="Source\ChunkRef.cs" />
<Compile Include="Source\Entities\EntityArrow.cs" /> <Compile Include="Source\Entities\EntityArrow.cs" />
@ -140,14 +140,14 @@
<Compile Include="Source\Entity.cs" /> <Compile Include="Source\Entity.cs" />
<Compile Include="Source\EntityFactory.cs" /> <Compile Include="Source\EntityFactory.cs" />
<Compile Include="Source\Item.cs" /> <Compile Include="Source\Item.cs" />
<Compile Include="Source\NBTFile.cs" /> <Compile Include="Source\Core\NBTFile.cs" />
<Compile Include="Source\NBT\JSONSerializer.cs" /> <Compile Include="Source\NBT\JSONSerializer.cs" />
<Compile Include="Source\NBT\NBT.cs" /> <Compile Include="Source\NBT\NBT.cs" />
<Compile Include="Source\NBT\NBTVerifier.cs" /> <Compile Include="Source\NBT\NBTVerifier.cs" />
<Compile Include="Source\Player.cs" /> <Compile Include="Source\Player.cs" />
<Compile Include="Source\Region.cs" /> <Compile Include="Source\Region.cs" />
<Compile Include="Source\RegionFile.cs" /> <Compile Include="Source\Core\RegionFile.cs" />
<Compile Include="Source\RegionKey.cs" /> <Compile Include="Source\Core\RegionKey.cs" />
<Compile Include="Source\RegionManager.cs" /> <Compile Include="Source\RegionManager.cs" />
<Compile Include="Source\TileEntities\TileEntityChest.cs" /> <Compile Include="Source\TileEntities\TileEntityChest.cs" />
<Compile Include="Source\TileEntities\TileEntityFurnace.cs" /> <Compile Include="Source\TileEntities\TileEntityFurnace.cs" />
@ -158,13 +158,12 @@
<Compile Include="Source\TileEntities\TileEntityTrap.cs" /> <Compile Include="Source\TileEntities\TileEntityTrap.cs" />
<Compile Include="Source\TileEntity.cs" /> <Compile Include="Source\TileEntity.cs" />
<Compile Include="Source\TileEntityFactory.cs" /> <Compile Include="Source\TileEntityFactory.cs" />
<Compile Include="Source\Utility\Base.cs" /> <Compile Include="Source\Core\Base.cs" />
<Compile Include="Source\Utility\ByteArray.cs" /> <Compile Include="Source\Core\ByteArray.cs" />
<Compile Include="Source\Utility\Compat.cs" /> <Compile Include="Source\Core\IndexedLinkedList.cs" />
<Compile Include="Source\Utility\IndexedLinkedList.cs" /> <Compile Include="Source\Core\Interface.cs" />
<Compile Include="Source\Utility\Interface.cs" /> <Compile Include="Source\Core\LRUCache.cs" />
<Compile Include="Source\Utility\LRUCache.cs" /> <Compile Include="Source\Core\NibbleArray.cs" />
<Compile Include="Source\Utility\NibbleArray.cs" />
<Compile Include="Source\World.cs" /> <Compile Include="Source\World.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>