NBTExplorer/SubstrateCS/Source/EntityFactory.cs

123 lines
5.8 KiB
C#

using System;
using System.Collections.Generic;
using Substrate.Entities;
using Substrate.Nbt;
namespace Substrate
{
/// <summary>
/// Creates new instances of concrete <see cref="TypedEntity"/> types from a dynamic registry.
/// </summary>
/// <remarks>This factory allows specific <see cref="TypedEntity"/> 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="TypedEntity"/> 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.</remarks>
public class EntityFactory
{
private static Dictionary<string, Type> _registry = new Dictionary<string, Type>();
/// <summary>
/// Create a new instance of a concrete <see cref="TypedEntity"/> type by name.
/// </summary>
/// <param name="type">The name that a concrete <see cref="TypedEntity"/> type was registered with.</param>
/// <returns>A new instance of a concrete <see cref="TypedEntity"/> type, or null if no type was registered with the given name.</returns>
public static TypedEntity Create (string type)
{
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
return Activator.CreateInstance(t) as TypedEntity;
}
/// <summary>
/// Create a new instance of a concrete <see cref="TypedEntity"/> type by NBT node.
/// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Entity, containing an 'id' field of the Entity's registered name.</param>
/// <returns>A new instance of a concrete <see cref="TypedEntity"/> type, or null if no type was registered with the given name.</returns>
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);
}
/// <summary>
/// Lookup a concrete <see cref="TypedEntity"/> type by name.
/// </summary>
/// <param name="type">The name that a concrete <see cref="TypedEntity"/> type was registered with.</param>
/// <returns>The <see cref="Type"/> of a concrete <see cref="TypedEntity"/> type, or null if no type was registered with the given name.</returns>
public static Type Lookup (string type)
{
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
return t;
}
/// <summary>
/// Registers a new concrete <see cref="TypedEntity"/> type with the <see cref="EntityFactory"/>, binding it to a given name.
/// </summary>
/// <param name="id">The name to bind to a concrete <see cref="TypedEntity"/> type.</param>
/// <param name="subtype">The <see cref="Type"/> of a concrete <see cref="TypedEntity"/> type.</param>
public static void Register (string id, Type subtype)
{
_registry[id] = subtype;
}
static EntityFactory ()
{
_registry["Arrow"] = typeof(EntityArrow);
_registry["Blaze"] = typeof(EntityBlaze);
_registry["Boat"] = typeof(EntityBoat);
_registry["CaveSpider"] = typeof(EntityCaveSpider);
_registry["Chicken"] = typeof(EntityChicken);
_registry["Cow"] = typeof(EntityCow);
_registry["Creeper"] = typeof(EntityCreeper);
_registry["Egg"] = typeof(EntityEgg);
_registry["EnderDragon"] = typeof(EntityEnderDragon);
_registry["Enderman"] = typeof(EntityEnderman);
_registry["EyeOfEnderSignal"] = typeof(EntityEnderEye);
_registry["FallingSand"] = typeof(EntityFallingSand);
_registry["Fireball"] = typeof(EntityFireball);
_registry["Ghast"] = typeof(EntityGhast);
_registry["Giant"] = typeof(EntityGiant);
_registry["Item"] = typeof(EntityItem);
_registry["LavaSlime"] = typeof(EntityMagmaCube);
_registry["Minecart"] = typeof(EntityMinecart);
_registry["Mob"] = typeof(EntityMob);
_registry["Monster"] = typeof(EntityMonster);
_registry["MushroomCow"] = typeof(EntityMooshroom);
_registry["Painting"] = typeof(EntityPainting);
_registry["Pig"] = typeof(EntityPig);
_registry["PigZombie"] = typeof(EntityPigZombie);
_registry["PrimedTnt"] = typeof(EntityPrimedTnt);
_registry["Sheep"] = typeof(EntitySheep);
_registry["Silverfish"] = typeof(EntitySilverfish);
_registry["Skeleton"] = typeof(EntitySkeleton);
_registry["Slime"] = typeof(EntitySlime);
_registry["SmallFireball"] = typeof(EntitySmallFireball);
_registry["Snowball"] = typeof(EntitySnowball);
_registry["SnowMan"] = typeof(EntitySnowman);
_registry["Spider"] = typeof(EntitySpider);
_registry["Squid"] = typeof(EntitySquid);
_registry["ThrownEnderpearl"] = typeof(EntityEnderPearl);
_registry["Villager"] = typeof(EntityVillager);
_registry["Wolf"] = typeof(EntityWolf);
_registry["XPOrb"] = typeof(EntityXPOrb);
_registry["Zombie"] = typeof(EntityZombie);
}
}
}