Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions core/src/main/java/net/pl3x/map/core/world/ChunkLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import de.bluecolored.bluenbt.TypeToken;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
Expand Down Expand Up @@ -81,15 +82,17 @@ public Chunk load(RandomAccessFile raf, long offset, int index) throws IOExcepti
// optimistic: try last used version
ChunkVersionLoader<?> usedLoader = lastUsedLoader;
Chunk chunk;
InputStream decompressedIn = new BufferedInputStream(compression.decompress(new FileInputStream(raf.getFD())));
chunk = usedLoader.load(world, region, decompressedIn, index);
try (InputStream decompressedIn = new BufferedInputStream(compression.decompress(nonClosing(new FileInputStream(raf.getFD()))))) {
chunk = usedLoader.load(world, region, decompressedIn, index);
}

// check version and reload chunk if the wrong loader has been used and a better one has been found
ChunkVersionLoader<?> actualLoader = findBestLoaderForVersion(chunk.getDataVersion());
if (actualLoader != null && usedLoader != actualLoader) {
raf.seek(offset + 5);
decompressedIn = new BufferedInputStream(compression.decompress(new FileInputStream(raf.getFD())));
chunk = actualLoader.load(world, region, decompressedIn, index);
try (InputStream decompressedIn = new BufferedInputStream(compression.decompress(nonClosing(new FileInputStream(raf.getFD()))))) {
chunk = actualLoader.load(world, region, decompressedIn, index);
}
lastUsedLoader = actualLoader;
}

Expand All @@ -103,6 +106,13 @@ public Chunk load(RandomAccessFile raf, long offset, int index) throws IOExcepti
return null;
}

private static InputStream nonClosing(InputStream in) {
return new FilterInputStream(in) {
@Override
public void close() {}
};
}

private static class ChunkVersionLoader<D extends Chunk.Data> {

private final Class<D> dataType;
Expand Down
Loading