using System;
using System.Collections.Generic;
using Substrate.Entities;
using Substrate.Nbt;
namespace Substrate
{
///
/// 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
/// Entities of Minecraft are registered with the factory at startup and bound to their respective 'id' fields.
public class EntityFactory
{
private static Dictionary _registry;
///
/// 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 EntityTyped Create (string type)
{
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
return Activator.CreateInstance(t) as EntityTyped;
}
///
/// Create a new instance of a concrete type by NBT node.
///
/// A representing a single Entity, containing an 'id' field of the Entity's registered name.
/// A new instance of a concrete type, or null if no type was registered with the given name.
public static EntityTyped Create (TagNodeCompound tree)
{
TagNode type;
if (!tree.TryGetValue("id", out type)) {
return null;
}
Type t;
if (!_registry.TryGetValue(type.ToTagString(), out t)) {
return null;
}
EntityTyped te = Activator.CreateInstance(t) as EntityTyped;
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;
}
static EntityFactory ()
{
_registry = new Dictionary();
_registry["Arrow"] = typeof(EntityArrow);
_registry["Boat"] = typeof(EntityBoat);
_registry["Chicken"] = typeof(EntityChicken);
_registry["Cow"] = typeof(EntityCow);
_registry["Creeper"] = typeof(EntityCreeper);
_registry["Egg"] = typeof(EntityEgg);
_registry["FallingSand"] = typeof(EntityFallingSand);
_registry["Ghast"] = typeof(EntityGhast);
_registry["Giant"] = typeof(EntityGiant);
_registry["Item"] = typeof(EntityItem);
_registry["Minecart"] = typeof(EntityMinecart);
_registry["Mob"] = typeof(EntityMob);
_registry["Monster"] = typeof(EntityMonster);
_registry["Painting"] = typeof(EntityPainting);
_registry["Pig"] = typeof(EntityPig);
_registry["PigZombie"] = typeof(EntityPigZombie);
_registry["PrimedTnt"] = typeof(EntityPrimedTnt);
_registry["Sheep"] = typeof(EntitySheep);
_registry["Skeleton"] = typeof(EntitySkeleton);
_registry["Slime"] = typeof(EntitySlime);
_registry["Snowball"] = typeof(EntitySnowball);
_registry["Spider"] = typeof(EntitySpider);
_registry["Squid"] = typeof(EntitySquid);
_registry["Wolf"] = typeof(EntityWolf);
_registry["Zombie"] = typeof(EntityZombie);
}
}
}