ChunkLoader can now indicate that they support parallel loading or saving

This commit is contained in:
jglrxavpok 2020-07-07 13:41:55 +02:00
parent 69654d05de
commit dfdf93bfc4
2 changed files with 28 additions and 5 deletions

View File

@ -5,4 +5,20 @@ import java.util.function.Consumer;
public interface IChunkLoader { public interface IChunkLoader {
boolean loadChunk(Instance instance, int chunkX, int chunkZ, Consumer<Chunk> callback); boolean loadChunk(Instance instance, int chunkX, int chunkZ, Consumer<Chunk> callback);
void saveChunk(Chunk chunk, Runnable callback); void saveChunk(Chunk chunk, Runnable callback);
/**
* Does this ChunkLoader allow for multithreaded saving of chunks?
* @return
*/
default boolean supportsParallelSaving() {
return false;
}
/**
* Does this ChunkLoader allow for multithreaded loading of chunks?
* @return
*/
default boolean supportsParallelLoading() {
return false;
}
} }

View File

@ -374,6 +374,12 @@ public class InstanceContainer extends Instance {
@Override @Override
public void saveChunksToStorageFolder(Runnable callback) { public void saveChunksToStorageFolder(Runnable callback) {
Check.notNull(getStorageFolder(), "You cannot save the instance if no StorageFolder has been defined"); Check.notNull(getStorageFolder(), "You cannot save the instance if no StorageFolder has been defined");
if(chunkLoader.supportsParallelSaving()) {
getChunks().parallelStream().forEach(c -> {
saveChunkToStorageFolder(c, null);
});
callback.run();
} else {
Iterator<Chunk> chunks = getChunks().iterator(); Iterator<Chunk> chunks = getChunks().iterator();
while (chunks.hasNext()) { while (chunks.hasNext()) {
Chunk chunk = chunks.next(); Chunk chunk = chunks.next();
@ -381,6 +387,7 @@ public class InstanceContainer extends Instance {
saveChunkToStorageFolder(chunk, isLast ? callback : null); saveChunkToStorageFolder(chunk, isLast ? callback : null);
} }
} }
}
@Override @Override
public BlockBatch createBlockBatch() { public BlockBatch createBlockBatch() {