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
{
2011-07-04 19:47:19 +00:00
private static Dictionary < string , Type > _registry = new Dictionary < string , Type > ( ) ;
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 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>
2012-11-05 03:33:06 +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 ;
2012-11-05 03:33:06 +00:00
if ( ! _registry . TryGetValue ( type , out t ) )
{
2011-04-06 04:43:54 +00:00
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 ) ;
}
2012-11-05 03:33:06 +00:00
/// <summary>
2012-11-06 05:35:57 +00:00
/// Create a new instance of a concrete <see cref="TileEntity"/> type by NBT node.
2012-11-05 03:33:06 +00:00
/// </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>
2012-11-06 05:35:57 +00:00
public static TileEntity CreateGeneric ( TagNodeCompound tree )
2012-11-05 03:33:06 +00:00
{
string type = tree [ "id" ] . ToTagString ( ) ;
Type t ;
if ( ! _registry . TryGetValue ( type , out t ) )
{
2012-11-06 05:35:57 +00:00
t = typeof ( TileEntity ) ;
2012-11-05 03:33:06 +00:00
}
2012-11-06 05:35:57 +00:00
TileEntity te = Activator . CreateInstance ( t , true ) as TileEntity ;
2012-11-05 03:33:06 +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 ;
}
2011-11-15 03:04:06 +00:00
/// <summary>
/// Gets an enumerator over all registered TileEntities.
/// </summary>
public static IEnumerable < KeyValuePair < string , Type > > RegisteredTileEntities
{
get
{
foreach ( KeyValuePair < string , Type > kvp in _registry ) {
yield return kvp ;
}
}
}
2011-04-06 04:43:54 +00:00
static TileEntityFactory ( )
{
2011-11-05 18:30:59 +00:00
_registry [ TileEntityEndPortal . TypeId ] = typeof ( TileEntityEndPortal ) ;
2012-09-09 20:01:30 +00:00
_registry [ TileEntityBeacon . TypeId ] = typeof ( TileEntityBeacon ) ;
2011-11-05 18:30:59 +00:00
_registry [ TileEntityBrewingStand . TypeId ] = typeof ( TileEntityBrewingStand ) ;
_registry [ TileEntityChest . TypeId ] = typeof ( TileEntityChest ) ;
2012-09-09 20:01:30 +00:00
_registry [ TileEntityControl . TypeId ] = typeof ( TileEntityControl ) ;
2011-11-05 18:30:59 +00:00
_registry [ TileEntityEnchantmentTable . TypeId ] = typeof ( TileEntityEnchantmentTable ) ;
_registry [ TileEntityFurnace . TypeId ] = typeof ( TileEntityFurnace ) ;
_registry [ TileEntityMobSpawner . TypeId ] = typeof ( TileEntityMobSpawner ) ;
_registry [ TileEntityMusic . TypeId ] = typeof ( TileEntityMusic ) ;
_registry [ TileEntityPiston . TypeId ] = typeof ( TileEntityPiston ) ;
_registry [ TileEntityRecordPlayer . TypeId ] = typeof ( TileEntityRecordPlayer ) ;
_registry [ TileEntitySign . TypeId ] = typeof ( TileEntitySign ) ;
_registry [ TileEntityTrap . TypeId ] = typeof ( TileEntityTrap ) ;
2011-04-06 04:43:54 +00:00
}
}
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
}