Skip to content

Commit 3e227d3

Browse files
committed
Refactor ChunkRegion to lazily create chunks
1 parent dbff52a commit 3e227d3

3 files changed

Lines changed: 43 additions & 19 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public Chunk(Instant timestamp, @Nullable CompoundTag rootTag) {
6363
setRootTag(rootTag);
6464
}
6565

66+
Chunk(ChunkRegion region, int localIndex) {
67+
this.region = region;
68+
this.localIndex = localIndex;
69+
}
70+
6671
/// Return the region of this chunk, or `null` if this chunk is not in any region.
6772
@Override
6873
@Contract(pure = true)
@@ -156,6 +161,18 @@ public void remove(CompoundTag element) throws IllegalArgumentException {
156161
rootTag = null;
157162
}
158163

164+
@Override
165+
public int hashCode() {
166+
return Objects.hash(timestamp, rootTag);
167+
}
168+
169+
@Override
170+
public boolean equals(Object obj) {
171+
return this == obj || obj instanceof Chunk that
172+
&& this.timestamp.equals(that.timestamp)
173+
&& Objects.equals(this.rootTag, that.rootTag);
174+
}
175+
159176
@Override
160177
public String toString() {
161178
return "Chunk[x=" + getLocalX() + ", z=" + getLocalZ() + ", root=" + rootTag + ']';

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

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,41 +134,50 @@ public ChunkRegion() {
134134
}
135135

136136
@Contract(pure = true)
137-
public @Nullable Chunk getChunk(int localIndex) {
137+
public Chunk getChunk(int localIndex) {
138138
Objects.checkIndex(localIndex, ChunkUtils.CHUNKS_PRE_REGION);
139-
140-
return chunks[localIndex];
139+
Chunk chunk = chunks[localIndex];
140+
if (chunk == null) {
141+
chunks[localIndex] = chunk = new Chunk(this, localIndex);
142+
}
143+
return chunk;
141144
}
142145

143146
@Contract(pure = true)
144-
public @Nullable Chunk getChunk(int x, int z) {
147+
public Chunk getChunk(int x, int z) {
145148
Objects.checkIndex(x, ChunkUtils.CHUNKS_PER_REGION_SIDE);
146149
Objects.checkIndex(z, ChunkUtils.CHUNKS_PER_REGION_SIDE);
147150

148-
return chunks[ChunkUtils.toLocalIndex(x, z)];
151+
return getChunk(ChunkUtils.toLocalIndex(x, z));
149152
}
150153

151154
@Contract(mutates = "this,param2")
152-
public void setChunk(int localIndex, @Nullable Chunk chunk) {
155+
public void setChunk(int localIndex, Chunk chunk) {
153156
Objects.checkIndex(localIndex, ChunkUtils.CHUNKS_PRE_REGION);
157+
Objects.requireNonNull(chunk);
154158

155159
Chunk old = chunks[localIndex];
156160
if (old != null) {
157161
old.setRegion(null, -1);
158162
}
159163

160-
if (chunk != null) {
161-
if (chunk.getParent() != null) {
162-
// The chunk is already in another region, so we need to remove it from its old region first.
163-
ChunkRegion oldRegion = chunk.getParent();
164-
oldRegion.remove(chunk);
165-
}
166-
167-
chunk.setRegion(this, localIndex);
164+
if (chunk.getParent() != null) {
165+
// The chunk is already in another region, so we need to remove it from its old region first.
166+
ChunkRegion oldRegion = chunk.getParent();
167+
oldRegion.remove(chunk);
168168
}
169+
170+
chunk.setRegion(this, localIndex);
169171
chunks[localIndex] = chunk;
170172
}
171173

174+
@Contract(mutates = "this,param3")
175+
public void setChunk(int x, int z, Chunk chunk) {
176+
Objects.checkIndex(x, ChunkUtils.CHUNKS_PER_REGION_SIDE);
177+
Objects.checkIndex(z, ChunkUtils.CHUNKS_PER_REGION_SIDE);
178+
setChunk(ChunkUtils.toLocalIndex(x, z), chunk);
179+
}
180+
172181
@Override
173182
@Contract(mutates = "this,param1")
174183
public void remove(Chunk chunk) throws IllegalArgumentException {
@@ -182,9 +191,8 @@ public void remove(Chunk chunk) throws IllegalArgumentException {
182191
if (old != chunk) {
183192
throw new AssertionError("Expected " + chunk + ", but got " + old);
184193
}
185-
186-
chunks[localIndex] = null;
187194
chunk.setRegion(null, -1);
195+
chunks[localIndex] = null;
188196
}
189197

190198
@Override

‎src/test/java/org/glavo/nbt/chunk/ChunkRegionTest.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,11 @@ public void testReadRegion(String path) throws IOException {
175175
for (int localIndex = 0; localIndex < CHUNKS_PRE_REGION; localIndex++) {
176176
var chunk = actual.getChunk(localIndex);
177177

178-
assertEquals(expected.timestamps[localIndex], chunk != null ? chunk.getTimestamp() : Instant.EPOCH);
178+
assertEquals(expected.timestamps[localIndex], chunk.getTimestamp());
179179

180180
if (expected.chunks[localIndex] == null) {
181-
assertTrue(chunk == null || chunk.getRootTag() == null);
181+
assertNull(chunk.getRootTag());
182182
} else {
183-
assertNotNull(chunk);
184183
assertEquals(localIndex, chunk.getLocalIndex());
185184
assertEquals(expected.chunks[localIndex], chunk.getRootTag());
186185
}

0 commit comments

Comments
 (0)