Skip to content

Commit d35410d

Browse files
committed
Simplify PaperChunkSnapshotProvider
1 parent 0141e5b commit d35410d

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

paper/src/main/java/xyz/jpenilla/squaremap/paper/util/chunksnapshot/PaperChunkSnapshotProvider.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,39 @@ record PaperChunkSnapshotProvider(
2727
public CompletableFuture<@Nullable ChunkSnapshot> asyncSnapshot(final int x, final int z) {
2828
return CompletableFuture.supplyAsync(() -> {
2929
final @Nullable ChunkAccess existing = this.level.getChunkIfLoadedImmediately(x, z);
30-
if (existing != null && existing.getPersistedStatus().isOrAfter(ChunkStatus.FULL)) {
31-
return CompletableFuture.completedFuture(existing);
32-
} else if (existing != null) {
33-
return CompletableFuture.<@Nullable ChunkAccess>completedFuture(null);
30+
if (existing != null) {
31+
final @Nullable ChunkSnapshot snapshot = this.maybeSnapshot(existing);
32+
if (snapshot != null) {
33+
return CompletableFuture.completedFuture(snapshot);
34+
}
3435
}
35-
final CompletableFuture<@Nullable ChunkAccess> load = new CompletableFuture<>();
36+
final CompletableFuture<@Nullable ChunkSnapshot> load = new CompletableFuture<>();
3637
ChunkSystem.scheduleChunkLoad(
3738
this.level,
3839
x,
3940
z,
4041
ChunkStatus.EMPTY,
4142
true,
4243
PrioritisedExecutor.Priority.NORMAL,
43-
load::complete
44+
chunk -> load.complete(this.maybeSnapshot(chunk))
4445
);
4546
return load;
46-
}, this.executor(x, z)).thenCompose(chunkFuture -> chunkFuture.thenApplyAsync(chunk -> {
47-
if (chunk == null) {
47+
}, this.executor(x, z)).thenCompose(future -> future);
48+
}
49+
50+
private @Nullable ChunkSnapshot maybeSnapshot(@Nullable ChunkAccess chunk) {
51+
if (chunk == null) {
52+
return null;
53+
}
54+
if (chunk instanceof ImposterProtoChunk imposter) {
55+
chunk = imposter.getWrapped();
56+
}
57+
if (!chunk.getPersistedStatus().isOrAfter(ChunkStatus.FULL)) {
58+
if (chunk.getBelowZeroRetrogen() == null || !chunk.getBelowZeroRetrogen().targetStatus().isOrAfter(ChunkStatus.SPAWN)) {
4859
return null;
4960
}
50-
if (chunk instanceof ImposterProtoChunk imposter) {
51-
chunk = imposter.getWrapped();
52-
}
53-
if (!chunk.getPersistedStatus().isOrAfter(ChunkStatus.FULL)) {
54-
if (chunk.getBelowZeroRetrogen() == null || !chunk.getBelowZeroRetrogen().targetStatus().isOrAfter(ChunkStatus.SPAWN)) {
55-
return null;
56-
}
57-
}
58-
return ChunkSnapshot.snapshot(this.level, chunk, false);
59-
}, this.executor(x, z)));
61+
}
62+
return ChunkSnapshot.snapshot(this.level, chunk, false);
6063
}
6164

6265
private Executor executor(final int x, final int z) {

0 commit comments

Comments
 (0)