2020-06-30 18:07:47 +02:00
|
|
|
package net.minestom.server.instance;
|
|
|
|
|
2020-10-24 09:34:19 +02:00
|
|
|
import net.minestom.server.MinecraftServer;
|
|
|
|
import net.minestom.server.utils.callback.OptionalCallback;
|
2020-09-19 18:35:00 +02:00
|
|
|
import net.minestom.server.utils.chunk.ChunkCallback;
|
2020-10-24 09:34:19 +02:00
|
|
|
import net.minestom.server.utils.thread.MinestomThread;
|
2020-10-24 10:46:23 +02:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.Nullable;
|
2020-10-24 09:34:19 +02:00
|
|
|
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
2020-06-30 18:07:47 +02:00
|
|
|
|
2020-07-21 18:48:15 +02:00
|
|
|
/**
|
2020-10-12 02:56:30 +02:00
|
|
|
* Interface implemented to change the way chunks are loaded/saved.
|
2020-10-15 08:21:13 +02:00
|
|
|
* <p>
|
2020-10-12 02:56:30 +02:00
|
|
|
* See {@link MinestomBasicChunkLoader} for the default implementation used in {@link InstanceContainer}.
|
2020-07-21 18:48:15 +02:00
|
|
|
*/
|
2020-06-30 18:07:47 +02:00
|
|
|
public interface IChunkLoader {
|
2020-07-21 18:48:15 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-24 09:34:19 +02:00
|
|
|
* Loads a {@link Chunk}, all blocks should be set since the {@link ChunkGenerator} is not applied.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
2020-10-12 03:18:02 +02:00
|
|
|
* @param instance the {@link Instance} where the {@link Chunk} belong
|
2020-07-21 18:48:15 +02:00
|
|
|
* @param chunkX the chunk X
|
|
|
|
* @param chunkZ the chunk Z
|
2020-10-12 03:18:02 +02:00
|
|
|
* @param callback the callback executed when the {@link Chunk} is done loading,
|
2020-10-24 09:34:19 +02:00
|
|
|
* never called if the method returns false. Can be null.
|
2020-07-21 18:48:15 +02:00
|
|
|
* @return true if the chunk loaded successfully, false otherwise
|
|
|
|
*/
|
2020-10-24 10:46:23 +02:00
|
|
|
boolean loadChunk(@NotNull Instance instance, int chunkX, int chunkZ, @Nullable ChunkCallback callback);
|
2020-07-21 18:48:15 +02:00
|
|
|
|
|
|
|
/**
|
2020-10-24 09:34:19 +02:00
|
|
|
* Saves a {@link Chunk} with an optional callback for when it is done.
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
2020-10-12 03:18:02 +02:00
|
|
|
* @param chunk the {@link Chunk} to save
|
|
|
|
* @param callback the callback executed when the {@link Chunk} is done saving,
|
2020-10-24 09:34:19 +02:00
|
|
|
* should be called even if the saving failed (you can throw an exception).
|
|
|
|
* Can be null.
|
2020-07-21 18:48:15 +02:00
|
|
|
*/
|
2020-10-24 10:46:23 +02:00
|
|
|
void saveChunk(@NotNull Chunk chunk, @Nullable Runnable callback);
|
2020-07-07 13:41:55 +02:00
|
|
|
|
2020-10-24 09:34:19 +02:00
|
|
|
/**
|
|
|
|
* Saves multiple chunks with an optional callback for when it is done.
|
|
|
|
* <p>
|
|
|
|
* Implementations need to check {@link #supportsParallelSaving()} to support the feature if possible.
|
|
|
|
*
|
|
|
|
* @param chunks the chunks to save
|
|
|
|
* @param callback the callback executed when the {@link Chunk} is done saving,
|
|
|
|
* should be called even if the saving failed (you can throw an exception).
|
|
|
|
* Can be null.
|
|
|
|
*/
|
2020-10-24 10:46:23 +02:00
|
|
|
default void saveChunks(@NotNull Collection<Chunk> chunks, @Nullable Runnable callback) {
|
2020-10-24 09:34:19 +02:00
|
|
|
if (supportsParallelSaving()) {
|
|
|
|
ExecutorService parallelSavingThreadPool = new MinestomThread(MinecraftServer.THREAD_COUNT_PARALLEL_CHUNK_SAVING, MinecraftServer.THREAD_NAME_PARALLEL_CHUNK_SAVING, true);
|
|
|
|
chunks.forEach(c -> parallelSavingThreadPool.execute(() -> saveChunk(c, null)));
|
|
|
|
try {
|
|
|
|
parallelSavingThreadPool.shutdown();
|
|
|
|
parallelSavingThreadPool.awaitTermination(1L, java.util.concurrent.TimeUnit.DAYS);
|
|
|
|
OptionalCallback.execute(callback);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
AtomicInteger counter = new AtomicInteger();
|
|
|
|
for (Chunk chunk : chunks) {
|
|
|
|
saveChunk(chunk, () -> {
|
|
|
|
final boolean isLast = counter.incrementAndGet() == chunks.size();
|
|
|
|
if (isLast) {
|
|
|
|
OptionalCallback.execute(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-07 13:41:55 +02:00
|
|
|
/**
|
2020-10-12 02:56:30 +02:00
|
|
|
* Does this {@link IChunkLoader} allow for multi-threaded saving of {@link Chunk}?
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
2020-08-07 09:14:50 +02:00
|
|
|
* @return true if the chunk loader supports parallel saving
|
2020-07-07 13:41:55 +02:00
|
|
|
*/
|
|
|
|
default boolean supportsParallelSaving() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-10-12 02:56:30 +02:00
|
|
|
* Does this {@link IChunkLoader} allow for multi-threaded loading of {@link Chunk}?
|
2020-07-21 18:48:15 +02:00
|
|
|
*
|
2020-08-07 09:14:50 +02:00
|
|
|
* @return true if the chunk loader supports parallel loading
|
2020-07-07 13:41:55 +02:00
|
|
|
*/
|
|
|
|
default boolean supportsParallelLoading() {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-30 18:07:47 +02:00
|
|
|
}
|