Skip to content

Commit bf57681

Browse files
committed
Add getParent method to NBTElement interface
1 parent 3f72355 commit bf57681

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/main/java/org/glavo/nbt/NBTElement.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.glavo.nbt.chunk.Chunk;
1919
import org.glavo.nbt.chunk.ChunkRegion;
2020
import org.glavo.nbt.tag.Tag;
21+
import org.jetbrains.annotations.Nullable;
2122

2223
public sealed interface NBTElement permits ChunkRegion, Chunk, Tag, NBTParent {
24+
/// Returns the parent of this element, or `null` if this element is not a child of any parent.
25+
@Nullable NBTParent<?> getParent();
2326
}

src/main/java/org/glavo/nbt/chunk/Chunk.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ public Chunk(@Nullable CompoundTag rootTag) {
4343
return region;
4444
}
4545

46+
/// Return the region of this chunk, or `null` if this chunk is not in any region.
47+
@Override
48+
@Contract(pure = true)
49+
public @Nullable ChunkRegion getParent() {
50+
return getRegion();
51+
}
52+
4653
void setRegion(@Nullable ChunkRegion region, int localIndex) {
4754
this.region = region;
4855
this.localIndex = localIndex;

src/main/java/org/glavo/nbt/chunk/ChunkRegion.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,13 @@ public static ChunkRegion readRegion(Path file) throws IOException {
117117
public ChunkRegion() {
118118
}
119119

120+
/// Always returns `null`. A chunk region is the root of the NBT tree, and it has no parent.
121+
@Override
122+
@Contract(value = "-> null", pure = true)
123+
public @Nullable NBTParent<ChunkRegion> getParent() {
124+
return null;
125+
}
126+
120127
@Contract(pure = true)
121128
public @Nullable Chunk getChunk(int localIndex) {
122129
Objects.checkIndex(localIndex, ChunkUtils.CHUNKS_PRE_REGION);

src/main/java/org/glavo/nbt/tag/Tag.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ public int getIndex() {
121121
}
122122

123123
/// If the tag is a child of a [parent][NBTParent], returns the parent; otherwise, returns `null`.
124+
@Override
124125
public @Nullable NBTParent<? extends Tag> getParent() {
125126
return parent;
126127
}

0 commit comments

Comments
 (0)