Skip to content

Commit

Permalink
Use empty world if the given file is not present in the loader
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Jul 2, 2023
1 parent 108646e commit a91d3c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/net/hollowcube/polar/PolarLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -44,8 +45,12 @@ public class PolarLoader implements IChunkLoader {
private final PolarWorld worldData;

public PolarLoader(@NotNull Path path) throws IOException {
this.worldData = PolarReader.read(Files.readAllBytes(path));
this.savePath = path;
if (Files.exists(path)) {
this.worldData = PolarReader.read(Files.readAllBytes(path));
} else {
this.worldData = new PolarWorld();
}
}

public PolarLoader(@NotNull InputStream inputStream) throws IOException {
Expand Down Expand Up @@ -198,7 +203,8 @@ public void unloadChunk(Chunk chunk) {
if (savePath != null) {
return CompletableFuture.runAsync(() -> {
try {
Files.write(savePath, PolarWriter.write(worldData));
Files.write(savePath, PolarWriter.write(worldData),
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
EXCEPTION_HANDLER.handleException(new RuntimeException("Failed to save world", e));
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/hollowcube/polar/PolarWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public class PolarWorld {
// Chunk data
private final Long2ObjectMap<PolarChunk> chunks = new Long2ObjectOpenHashMap<>();

public PolarWorld() {
this(LATEST_VERSION, DEFAULT_COMPRESSION, (byte) -4, (byte) 19, List.of());
}

public PolarWorld(
short version,
@NotNull CompressionType compression,
Expand Down

0 comments on commit a91d3c6

Please sign in to comment.