Merge pull request #7 from Sukasa/master

Allow return of unregistered Tile Entity types as TileEntity class
This commit is contained in:
Justin Aquadro 2012-11-06 08:38:39 -08:00
commit 3eaf2bf6af
2 changed files with 25 additions and 3 deletions

View file

@ -43,7 +43,7 @@ namespace Substrate.Core
return null; return null;
} }
return TileEntityFactory.Create(te); return TileEntityFactory.CreateGeneric(te);
} }
public void SetTileEntity (int x, int y, int z, TileEntity te) public void SetTileEntity (int x, int y, int z, TileEntity te)

View file

@ -36,12 +36,13 @@ namespace Substrate
/// </summary> /// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name.</param> /// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name.</param>
/// <returns>A new instance of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns> /// <returns>A new instance of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns>
public static TileEntity Create (TagNodeCompound tree) public static TileEntity Create(TagNodeCompound tree)
{ {
string type = tree["id"].ToTagString(); string type = tree["id"].ToTagString();
Type t; Type t;
if (!_registry.TryGetValue(type, out t)) { if (!_registry.TryGetValue(type, out t))
{
return null; return null;
} }
@ -50,6 +51,27 @@ namespace Substrate
return te.LoadTreeSafe(tree); return te.LoadTreeSafe(tree);
} }
/// <summary>
/// Create a new instance of a concrete <see cref="TileEntity"/> type by NBT node.
/// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Tile Entity, containing an 'id' field of the Tile Entity's registered name.</param>
/// <returns>A new instance of a concrete <see cref="TileEntity"/> type, or null if no type was registered with the given name.</returns>
public static TileEntity CreateGeneric(TagNodeCompound tree)
{
string type = tree["id"].ToTagString();
Type t;
if (!_registry.TryGetValue(type, out t))
{
t = typeof (TileEntity);
}
TileEntity te = Activator.CreateInstance(t, true) as TileEntity;
return te.LoadTreeSafe(tree);
}
/// <summary> /// <summary>
/// Lookup a concrete <see cref="TileEntity"/> type by name. /// Lookup a concrete <see cref="TileEntity"/> type by name.
/// </summary> /// </summary>