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 = 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 TypedEntity Create (string type) { Type t; if (!_registry.TryGetValue(type, out t)) { return null; } return Activator.CreateInstance(t) as TypedEntity; } /// /// 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 TypedEntity Create (TagNodeCompound tree) { TagNode type; if (!tree.TryGetValue("id", out type)) { return null; } Type t; if (!_registry.TryGetValue(type.ToTagString(), out t)) { return null; } TypedEntity te = Activator.CreateInstance(t) as TypedEntity; return te.LoadTreeSafe(tree); } /// /// Creates a new instance of a nonspecific object by NBT node. /// /// A representing a single Entity, containing an 'id' field. /// A new instance of a object, or null if the entity is not typed. public static TypedEntity CreateGeneric (TagNodeCompound tree) { TagNode type; if (!tree.TryGetValue("id", out type)) { return null; } TypedEntity te = new TypedEntity(type.ToTagString().Data); 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 Entities. /// public static IEnumerable> RegisteredEntities { get { foreach (KeyValuePair kvp in _registry) { yield return kvp; } } } static EntityFactory () { _registry[EntityArrow.TypeId] = typeof(EntityArrow); _registry[EntityBlaze.TypeId] = typeof(EntityBlaze); _registry[EntityBoat.TypeId] = typeof(EntityBoat); _registry[EntityCaveSpider.TypeId] = typeof(EntityCaveSpider); _registry[EntityChicken.TypeId] = typeof(EntityChicken); _registry[EntityCow.TypeId] = typeof(EntityCow); _registry[EntityCreeper.TypeId] = typeof(EntityCreeper); _registry[EntityEgg.TypeId] = typeof(EntityEgg); _registry[EntityEnderDragon.TypeId] = typeof(EntityEnderDragon); _registry[EntityEnderman.TypeId] = typeof(EntityEnderman); _registry[EntityEnderEye.TypeId] = typeof(EntityEnderEye); _registry[EntityFallingSand.TypeId] = typeof(EntityFallingSand); _registry[EntityFireball.TypeId] = typeof(EntityFireball); _registry[EntityGhast.TypeId] = typeof(EntityGhast); _registry[EntityGiant.TypeId] = typeof(EntityGiant); _registry[EntityItem.TypeId] = typeof(EntityItem); _registry[EntityMagmaCube.TypeId] = typeof(EntityMagmaCube); _registry[EntityMinecart.TypeId] = typeof(EntityMinecart); _registry[EntityMob.TypeId] = typeof(EntityMob); _registry[EntityMonster.TypeId] = typeof(EntityMonster); _registry[EntityMooshroom.TypeId] = typeof(EntityMooshroom); _registry[EntityPainting.TypeId] = typeof(EntityPainting); _registry[EntityPig.TypeId] = typeof(EntityPig); _registry[EntityPigZombie.TypeId] = typeof(EntityPigZombie); _registry[EntityPrimedTnt.TypeId] = typeof(EntityPrimedTnt); _registry[EntitySheep.TypeId] = typeof(EntitySheep); _registry[EntitySilverfish.TypeId] = typeof(EntitySilverfish); _registry[EntitySkeleton.TypeId] = typeof(EntitySkeleton); _registry[EntitySlime.TypeId] = typeof(EntitySlime); _registry[EntitySmallFireball.TypeId] = typeof(EntitySmallFireball); _registry[EntitySnowball.TypeId] = typeof(EntitySnowball); _registry[EntitySnowman.TypeId] = typeof(EntitySnowman); _registry[EntitySpider.TypeId] = typeof(EntitySpider); _registry[EntitySquid.TypeId] = typeof(EntitySquid); _registry[EntityEnderPearl.TypeId] = typeof(EntityEnderPearl); _registry[EntityVillager.TypeId] = typeof(EntityVillager); _registry[EntityWolf.TypeId] = typeof(EntityWolf); _registry[EntityXPOrb.TypeId] = typeof(EntityXPOrb); _registry[EntityZombie.TypeId] = typeof(EntityZombie); } } }