2011-04-06 04:43:54 +00:00
using System ;
using System.Collections.Generic ;
2011-04-06 22:01:22 +00:00
namespace Substrate
2011-04-06 04:43:54 +00:00
{
2011-06-30 04:41:29 +00:00
using Nbt ;
2011-04-06 19:41:57 +00:00
using TileEntities ;
2011-04-06 04:43:54 +00:00
2011-06-28 03:41:44 +00:00
/// <summary>
/// Creates new instances of concrete <see cref="TileEntity"/> types from a dynamic registry.
/// </summary>
/// <remarks>This factory allows specific <see cref="TileEntity"/> 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="TileEntity"/> types can be supported. By default, the standard
/// Tile Entities of Minecraft are registered with the factory at startup and bound to their respective 'id' fields.</remarks>
2011-04-06 04:43:54 +00:00
public class TileEntityFactory
{
private static Dictionary < string , Type > _registry ;
2011-06-28 03:41:44 +00:00
/// <summary>
/// Create a new instance of a concrete <see cref="TileEntity"/> type by name.
/// </summary>
/// <param name="type">The name that a concrete <see cref="TileEntity"/> type was registered with.</param>
/// <returns>A new instance of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns>
2011-04-06 04:43:54 +00:00
public static TileEntity Create ( string type )
{
Type t ;
if ( ! _registry . TryGetValue ( type , out t ) ) {
return null ;
}
2011-04-10 21:04:33 +00:00
return Activator . CreateInstance ( t ) as TileEntity ;
2011-04-06 04:43:54 +00:00
}
2011-06-28 03:41:44 +00:00
/// <summary>
/// Create a new instance of a concrete <see cref="TileEntity"/> type by NBT node.
/// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name.</param>
/// <returns>A new instance of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns>
2011-06-20 03:51:40 +00:00
public static TileEntity Create ( TagNodeCompound tree )
2011-04-06 04:43:54 +00:00
{
2011-04-09 02:50:42 +00:00
string type = tree [ "id" ] . ToTagString ( ) ;
2011-04-06 04:43:54 +00:00
Type t ;
if ( ! _registry . TryGetValue ( type , out t ) ) {
return null ;
}
2011-04-06 18:57:05 +00:00
TileEntity te = Activator . CreateInstance ( t ) as TileEntity ;
2011-04-06 04:43:54 +00:00
return te . LoadTreeSafe ( tree ) ;
}
2011-06-28 03:41:44 +00:00
/// <summary>
/// Lookup a concrete <see cref="TileEntity"/> type by name.
/// </summary>
/// <param name="type">The name that a concrete <see cref="TileEntity"/> type was registered with.</param>
/// <returns>The <see cref="Type"/> of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns>
2011-04-06 04:43:54 +00:00
public static Type Lookup ( string type )
{
Type t ;
if ( ! _registry . TryGetValue ( type , out t ) ) {
return null ;
}
return t ;
}
2011-06-28 03:41:44 +00:00
/// <summary>
/// Registers a new concrete <see cref="TileEntity"/> type with the <see cref="TileEntityFactory"/>, binding it to a given name.
/// </summary>
/// <param name="id">The name to bind to a concrete <see cref="TileEntity"/> type.</param>
/// <param name="subtype">The <see cref="Type"/> of a concrete <see cref="TileEntity"/> type.</param>
2011-04-06 04:43:54 +00:00
public static void Register ( string id , Type subtype )
{
_registry [ id ] = subtype ;
}
static TileEntityFactory ( )
{
_registry = new Dictionary < string , Type > ( ) ;
_registry [ "Chest" ] = typeof ( TileEntityChest ) ;
_registry [ "Furnace" ] = typeof ( TileEntityFurnace ) ;
_registry [ "MobSpawner" ] = typeof ( TileEntityMobSpawner ) ;
_registry [ "Music" ] = typeof ( TileEntityMusic ) ;
2011-06-05 04:11:44 +00:00
_registry [ "RecordPlayer" ] = typeof ( TileEntityRecordPlayer ) ;
2011-04-06 04:43:54 +00:00
_registry [ "Sign" ] = typeof ( TileEntitySign ) ;
_registry [ "Trap" ] = typeof ( TileEntityTrap ) ;
}
}
2011-06-27 04:49:29 +00:00
/// <summary>
/// An exception that is thrown when unknown TileEntity types are queried.
/// </summary>
public class UnknownTileEntityException : Exception
{
public UnknownTileEntityException ( string message )
: base ( message )
{ }
}
2011-04-06 04:43:54 +00:00
}