From 37739af5623757aa74ea4aee7501f516c844ce9f Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 4 Jan 2012 19:23:37 -0500 Subject: [PATCH] Fixed enumeration bug in EntityCollection --- SubstrateCS/Source/EntityCollection.cs | 11 ++++++++++- SubstrateCS/Source/EntityFactory.cs | 17 +++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/SubstrateCS/Source/EntityCollection.cs b/SubstrateCS/Source/EntityCollection.cs index 3ff18f4..f385106 100644 --- a/SubstrateCS/Source/EntityCollection.cs +++ b/SubstrateCS/Source/EntityCollection.cs @@ -188,12 +188,14 @@ namespace Substrate { private IEnumerator _enum; + private bool _next; private TypedEntity _cur; internal Enumerator (TagNodeList entities) { _enum = entities.GetEnumerator(); _cur = null; + _next = false; } #region IEnumerator Members @@ -205,7 +207,7 @@ namespace Substrate { get { - if (_cur == null) { + if (_next == false) { throw new InvalidOperationException(); } return _cur; @@ -240,10 +242,16 @@ namespace Substrate public bool MoveNext () { if (!_enum.MoveNext()) { + _next = false; return false; } _cur = EntityFactory.Create(_enum.Current.ToTagCompound()); + if (_cur == null) + _cur = EntityFactory.CreateGeneric(_enum.Current.ToTagCompound()); + + _next = true; + return true; } @@ -253,6 +261,7 @@ namespace Substrate void System.Collections.IEnumerator.Reset () { _cur = null; + _next = false; _enum.Reset(); } diff --git a/SubstrateCS/Source/EntityFactory.cs b/SubstrateCS/Source/EntityFactory.cs index df975db..9d25618 100644 --- a/SubstrateCS/Source/EntityFactory.cs +++ b/SubstrateCS/Source/EntityFactory.cs @@ -52,6 +52,23 @@ namespace Substrate return te.LoadTreeSafe(tree); } + /// + /// Creates a new instance of a nonspecific object by NBT node. + /// + /// A representing a single Entity, containing an 'id' field. + /// A new instance of a object, or null if the entity is not typed. + 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); + } + /// /// Lookup a concrete type by name. ///