NBTExplorer/Substrate/SubstrateCS/Source/TileEntityFactory.cs

66 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Text;
namespace Substrate
{
using NBT;
using TileEntities;
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;
}
2011-04-10 21:04:33 +00:00
return Activator.CreateInstance(t) as TileEntity;
}
public static TileEntity Create (TagCompound 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);
}
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);
}
}
}