using System; using System.Collections.Generic; namespace Substrate { using Nbt; using TileEntities; /// /// Creates new instances of concrete types from a dynamic registry. /// /// This factory allows specific objects to be generated as an NBT tree is parsed. New types can be /// registered with the factory at any time, so that custom 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. public class TileEntityFactory { private static Dictionary _registry = new Dictionary(); /// /// Create a new instance of a concrete type by name. /// /// The name that a concrete type was registered with. /// A new instance of a concrete type, or null if no type was registered with the given name. public static TileEntity Create (string type) { Type t; if (!_registry.TryGetValue(type, out t)) { return null; } return Activator.CreateInstance(t) as TileEntity; } /// /// Create a new instance of a concrete type by NBT node. /// /// A representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name. /// A new instance of a concrete type, or null if no type was registered with the given name. public static TileEntity Create(TagNodeCompound tree) { string type = tree["id"].ToTagString(); Type t; if (!_registry.TryGetValue(type, out t)) { return null; } TileEntity te = Activator.CreateInstance(t) as TileEntity; return te.LoadTreeSafe(tree); } /// /// Create a new instance of a concrete type by NBT node. /// /// A representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name. /// A new instance of a concrete type, or null if no type was registered with the given name. public static TileEntity CreateGeneric(TagNodeCompound tree) { string type = tree["id"].ToTagString(); Type t; if (!_registry.TryGetValue(type, out t)) { t = typeof (TileEntity); } TileEntity te = Activator.CreateInstance(t, true) as TileEntity; return te.LoadTreeSafe(tree); } /// /// Lookup a concrete type by name. /// /// The name that a concrete type was registered with. /// The of a concrete type, or null if no type was registered with the given name. public static Type Lookup (string type) { Type t; if (!_registry.TryGetValue(type, out t)) { return null; } return t; } /// /// Registers a new concrete type with the , binding it to a given name. /// /// The name to bind to a concrete type. /// The of a concrete type. public static void Register (string id, Type subtype) { _registry[id] = subtype; } /// /// Gets an enumerator over all registered TileEntities. /// public static IEnumerable> RegisteredTileEntities { get { foreach (KeyValuePair kvp in _registry) { yield return kvp; } } } static TileEntityFactory () { _registry[TileEntityEndPortal.TypeId] = typeof(TileEntityEndPortal); _registry[TileEntityBeacon.TypeId] = typeof(TileEntityBeacon); _registry[TileEntityBrewingStand.TypeId] = typeof(TileEntityBrewingStand); _registry[TileEntityChest.TypeId] = typeof(TileEntityChest); _registry[TileEntityControl.TypeId] = typeof(TileEntityControl); _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); } } /// /// An exception that is thrown when unknown TileEntity types are queried. /// public class UnknownTileEntityException : Exception { public UnknownTileEntityException (string message) : base(message) { } } }