Fixed enumeration bug in EntityCollection

This commit is contained in:
Justin Aquadro 2012-01-04 19:23:37 -05:00
parent 4d4a14b866
commit 37739af562
2 changed files with 27 additions and 1 deletions

View file

@ -188,12 +188,14 @@ namespace Substrate
{ {
private IEnumerator<TagNode> _enum; private IEnumerator<TagNode> _enum;
private bool _next;
private TypedEntity _cur; private TypedEntity _cur;
internal Enumerator (TagNodeList entities) internal Enumerator (TagNodeList entities)
{ {
_enum = entities.GetEnumerator(); _enum = entities.GetEnumerator();
_cur = null; _cur = null;
_next = false;
} }
#region IEnumerator<Entity> Members #region IEnumerator<Entity> Members
@ -205,7 +207,7 @@ namespace Substrate
{ {
get get
{ {
if (_cur == null) { if (_next == false) {
throw new InvalidOperationException(); throw new InvalidOperationException();
} }
return _cur; return _cur;
@ -240,10 +242,16 @@ namespace Substrate
public bool MoveNext () public bool MoveNext ()
{ {
if (!_enum.MoveNext()) { if (!_enum.MoveNext()) {
_next = false;
return false; return false;
} }
_cur = EntityFactory.Create(_enum.Current.ToTagCompound()); _cur = EntityFactory.Create(_enum.Current.ToTagCompound());
if (_cur == null)
_cur = EntityFactory.CreateGeneric(_enum.Current.ToTagCompound());
_next = true;
return true; return true;
} }
@ -253,6 +261,7 @@ namespace Substrate
void System.Collections.IEnumerator.Reset () void System.Collections.IEnumerator.Reset ()
{ {
_cur = null; _cur = null;
_next = false;
_enum.Reset(); _enum.Reset();
} }

View file

@ -52,6 +52,23 @@ namespace Substrate
return te.LoadTreeSafe(tree); return te.LoadTreeSafe(tree);
} }
/// <summary>
/// Creates a new instance of a nonspecific <see cref="TypedEntity"/> object by NBT node.
/// </summary>
/// <param name="tree">A <see cref="TagNodeCompound"/> representing a single Entity, containing an 'id' field.</param>
/// <returns>A new instance of a <see cref="TypedEntity"/> object, or null if the entity is not typed.</returns>
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);
}
/// <summary> /// <summary>
/// Lookup a concrete <see cref="TypedEntity"/> type by name. /// Lookup a concrete <see cref="TypedEntity"/> type by name.
/// </summary> /// </summary>