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