NBTExplorer/Substrate/TileEntityFactory.cs
2011-04-06 04:40:27 +00:00

64 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace NBToolkit.Map
{
using NBT;
public class TileEntityFactory
{
private static Dictionary<string, Type> _registry;
public static TileEntity Create (string type)
{
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
return Activator.CreateInstance(t) as TileEntity;
}
public static TileEntity Create (NBT_Compound tree)
{
string type = tree["id"].ToNBTString();
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
TileEntity te = Activator.CreateInstance(t, new object[] { tree }) as TileEntity;
return te.LoadTreeSafe(tree);
}
public static Type Lookup (string type)
{
Type t;
if (!_registry.TryGetValue(type, out t)) {
return null;
}
return t;
}
public static void Register (string id, Type subtype)
{
_registry[id] = subtype;
}
static TileEntityFactory ()
{
_registry = new Dictionary<string, Type>();
_registry["Chest"] = typeof(TileEntityChest);
_registry["Furnace"] = typeof(TileEntityFurnace);
_registry["MobSpawner"] = typeof(TileEntityMobSpawner);
_registry["Music"] = typeof(TileEntityMusic);
_registry["Sign"] = typeof(TileEntitySign);
_registry["Trap"] = typeof(TileEntityTrap);
}
}
}