Skip to content

Commit 7676357

Browse files
committed
Added NbtElement.GetEnumerator
1 parent c01e084 commit 7676357

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

MCServerSharp.NBT/NbtElement.ContainerEnumerator.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public NbtElement Current
2020
{
2121
get
2222
{
23-
if (_currentIndex < 0)
23+
if (_currentIndex < 0 || _currentIndex >= _targetEndIndex)
2424
return default;
2525
return new NbtElement(_container._parent, _currentIndex);
2626
}

MCServerSharp.NBT/NbtElement.cs

+25-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Buffers;
3+
using System.Collections;
34
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.IO;
@@ -9,7 +10,7 @@
910
namespace MCServerSharp.NBT
1011
{
1112
[DebuggerDisplay("{ToString(), nq}")]
12-
public readonly partial struct NbtElement
13+
public readonly partial struct NbtElement : IEnumerable<NbtElement>
1314
{
1415
private readonly NbtDocument _parent;
1516
private readonly int _index;
@@ -26,18 +27,16 @@ public readonly partial struct NbtElement
2627

2728
public NbtElement this[ReadOnlySpan<char> name] => GetCompoundElement(name);
2829

29-
public NbtElement this[string name] => this[name.AsSpan()];
30-
3130
public NbtOptions Options => _parent.Options;
3231

3332
public bool IsValid => _parent != null && _parent.Bytes.Length != 0;
3433

35-
public NbtType Type => _parent != null
34+
public NbtType Type => _parent != null
3635
? _parent.GetTagType(_index)
3736
: NbtType.Undefined;
3837

39-
public NbtFlags Flags => _parent != null
40-
? _parent.GetFlags(_index)
38+
public NbtFlags Flags => _parent != null
39+
? _parent.GetFlags(_index)
4140
: NbtFlags.None;
4241

4342
public Utf8Memory Name => _parent != null
@@ -287,6 +286,26 @@ public ContainerEnumerator EnumerateContainer()
287286
return new ContainerEnumerator(this);
288287
}
289288

289+
public ContainerEnumerator GetEnumerator()
290+
{
291+
AssertValidInstance();
292+
293+
if (!Type.IsContainer())
294+
return default;
295+
296+
return new ContainerEnumerator(this);
297+
}
298+
299+
IEnumerator<NbtElement> IEnumerable<NbtElement>.GetEnumerator()
300+
{
301+
return GetEnumerator();
302+
}
303+
304+
IEnumerator IEnumerable.GetEnumerator()
305+
{
306+
return GetEnumerator();
307+
}
308+
290309
public ArrayEnumerator<T> EnumerateArray<T>()
291310
where T : unmanaged
292311
{

0 commit comments

Comments
 (0)