forked from minecraft-dotnet/Substrate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntityCollection.cs
More file actions
271 lines (224 loc) · 8.77 KB
/
EntityCollection.cs
File metadata and controls
271 lines (224 loc) · 8.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using System;
using System.Collections.Generic;
using System.Text;
using Substrate.Nbt;
namespace Substrate
{
/// <summary>
/// Functions to query and manage a collection of entities.
/// </summary>
public class EntityCollection : IEnumerable<TypedEntity>
{
private TagNodeList _entities;
private bool _dirty;
/// <summary>
/// Gets or sets a value indicating whether this collection contains unsaved changes.
/// </summary>
public bool IsDirty
{
get { return _dirty; }
set { _dirty = value; }
}
/// <summary>
/// Creates a new <see cref="EntityCollection"/> around a <see cref="TagNodeList"/> containing Entity nodes.
/// </summary>
/// <param name="entities">A <see cref="TagNodeList"/> containing Entity nodes.</param>
public EntityCollection (TagNodeList entities)
{
_entities = entities;
}
/// <summary>
/// Gets a list of all entities in the collection that match a given id (type).
/// </summary>
/// <param name="id">The id (type) of entities that should be returned.</param>
/// <returns>A list of <see cref="TypedEntity"/> objects matching the given id (type).</returns>
public List<TypedEntity> FindAll (string id)
{
List<TypedEntity> set = new List<TypedEntity>();
foreach (TagNodeCompound ent in _entities) {
TagNode eid;
if (!ent.TryGetValue("id", out eid)) {
continue;
}
if (eid.ToTagString().Data != id) {
continue;
}
TypedEntity obj = EntityFactory.Create(ent);
if (obj != null) {
set.Add(obj);
}
}
return set;
}
/// <summary>
/// Gets a list of all entities in the collection that match a given condition.
/// </summary>
/// <param name="match">A <see cref="Predicate{T}"/> defining the matching condition.</param>
/// <returns>A list of <see cref="TypedEntity"/> objects matching the given condition.</returns>
public List<TypedEntity> FindAll (Predicate<TypedEntity> match)
{
List<TypedEntity> set = new List<TypedEntity>();
foreach (TagNodeCompound ent in _entities) {
TypedEntity obj = EntityFactory.Create(ent);
if (obj == null) {
continue;
}
if (match(obj)) {
set.Add(obj);
}
}
return set;
}
/// <summary>
/// Adds a <see cref="TypedEntity"/> to the collection.
/// </summary>
/// <param name="ent">The <see cref="TypedEntity"/> object to add.</param>
/// <remarks>It is up to the developer to ensure that the <see cref="TypedEntity"/> being added to the collection has a position that
/// is within acceptable range of the collection. <see cref="EntityCollection"/> transparently back other objects such as
/// <see cref="IChunk"/> objects, which have a well-defined position in global space. The <see cref="EntityCollection"/> itself has
/// no concept of position and will not enforce constraints on the positions of <see cref="TypedEntity"/> objects being added.</remarks>
public void Add (TypedEntity ent)
{
_entities.Add(ent.BuildTree());
_dirty = true;
}
/// <summary>
/// Removes all entities matching the given id (type) from the collection.
/// </summary>
/// <param name="id">The id (type) of entities that should be removed.</param>
/// <returns>A count of the number of entities that were removed.</returns>
public int RemoveAll (string id)
{
int rem = _entities.RemoveAll(val =>
{
TagNodeCompound cval = val as TagNodeCompound;
if (cval == null) {
return false;
}
TagNode sval;
if (!cval.TryGetValue("id", out sval)) {
return false;
}
return (sval.ToTagString().Data == id);
});
if (rem > 0) {
_dirty = true;
}
return rem;
}
/// <summary>
/// Removes all entities matching the given condition from the collection.
/// </summary>
/// <param name="match">A <see cref="Predicate{T}"/> defining the matching condition.</param>
/// <returns>A count of the number of entities that were removed.</returns>
public int RemoveAll (Predicate<TypedEntity> match)
{
int rem = _entities.RemoveAll(val =>
{
TagNodeCompound cval = val as TagNodeCompound;
if (cval == null) {
return false;
}
TypedEntity obj = EntityFactory.Create(cval);
if (obj == null) {
return false;
}
return match(obj);
});
if (rem > 0) {
_dirty = true;
}
return rem;
}
#region IEnumerable<Entity> Members
/// <summary>
/// Returns an enumerator that iterates through all entities.
/// </summary>
/// <returns>An <see cref="Enumerator"/> for this object.</returns>
public IEnumerator<TypedEntity> GetEnumerator ()
{
return new Enumerator(_entities);
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through all entities.
/// </summary>
/// <returns>An <see cref="Enumerator"/> for this object.</returns>
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator ()
{
return new Enumerator(_entities);
}
#endregion
/// <summary>
/// Enumerates the entities within an <see cref="EntityCollection"/>.
/// </summary>
private struct Enumerator : IEnumerator<TypedEntity>
{
private IEnumerator<TagNode> _enum;
private bool _next;
private TypedEntity _cur;
internal Enumerator (TagNodeList entities)
{
_enum = entities.GetEnumerator();
_cur = null;
_next = false;
}
#region IEnumerator<Entity> Members
/// <summary>
/// Gets the <see cref="TypedEntity"/> at the current position of the enumerator.
/// </summary>
public TypedEntity Current
{
get
{
if (_next == false) {
throw new InvalidOperationException();
}
return _cur;
}
}
#endregion
#region IDisposable Members
/// <summary>
/// Releases all resources used by the <see cref="Enumerator"/>.
/// </summary>
public void Dispose () { }
#endregion
#region IEnumerator Members
/// <summary>
/// Gets the <see cref="TypedEntity"/> at the current position of the enumerator.
/// </summary>
object System.Collections.IEnumerator.Current
{
get { return Current; }
}
/// <summary>
/// Advances the enumerator to the next <see cref="TypedEntity"/> in the <see cref="EntityCollection"/>.
/// </summary>
/// <returns>True if the enumerator was successfully advanced to the next position; false if the enumerator advanced past the end of the collection.</returns>
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;
}
/// <summary>
/// Sets the enumerator to its initial position, which is before the first <see cref="TypedEntity"/> in the collection.
/// </summary>
void System.Collections.IEnumerator.Reset ()
{
_cur = null;
_next = false;
_enum.Reset();
}
#endregion
}
}
}