chore: enforce and document notnull chunk loader, add noop impl

This commit is contained in:
mworzala 2024-07-08 11:58:15 -04:00
parent afae77a41c
commit 1903e8dff2
No known key found for this signature in database
GPG Key ID: B148F922E64797C7
3 changed files with 33 additions and 6 deletions

View File

@ -19,6 +19,10 @@ import java.util.concurrent.atomic.AtomicInteger;
*/ */
public interface IChunkLoader { public interface IChunkLoader {
static @NotNull IChunkLoader noop() {
return NoopChunkLoaderImpl.INSTANCE;
}
/** /**
* Loads instance data from the loader. * Loads instance data from the loader.
* *

View File

@ -269,9 +269,7 @@ public class InstanceContainer extends Instance {
// Clear cache // Clear cache
this.chunks.remove(getChunkIndex(chunkX, chunkZ)); this.chunks.remove(getChunkIndex(chunkX, chunkZ));
chunk.unload(); chunk.unload();
if (chunkLoader != null) { chunkLoader.unloadChunk(chunk);
chunkLoader.unloadChunk(chunk);
}
var dispatcher = MinecraftServer.process().dispatcher(); var dispatcher = MinecraftServer.process().dispatcher();
dispatcher.deletePartition(chunk); dispatcher.deletePartition(chunk);
} }
@ -588,17 +586,19 @@ public class InstanceContainer extends Instance {
* *
* @return the {@link IChunkLoader} of this instance * @return the {@link IChunkLoader} of this instance
*/ */
public IChunkLoader getChunkLoader() { public @NotNull IChunkLoader getChunkLoader() {
return chunkLoader; return chunkLoader;
} }
/** /**
* Changes the {@link IChunkLoader} of this instance (to change how chunks are retrieved when not already loaded). * Changes the {@link IChunkLoader} of this instance (to change how chunks are retrieved when not already loaded).
* *
* <p>{@link IChunkLoader#noop()} can be used to do nothing.</p>
*
* @param chunkLoader the new {@link IChunkLoader} * @param chunkLoader the new {@link IChunkLoader}
*/ */
public void setChunkLoader(IChunkLoader chunkLoader) { public void setChunkLoader(@NotNull IChunkLoader chunkLoader) {
this.chunkLoader = chunkLoader; this.chunkLoader = Objects.requireNonNull(chunkLoader, "Chunk loader cannot be null");
} }
@Override @Override

View File

@ -0,0 +1,23 @@
package net.minestom.server.instance;
import net.minestom.server.utils.async.AsyncUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.concurrent.CompletableFuture;
final class NoopChunkLoaderImpl implements IChunkLoader {
static final NoopChunkLoaderImpl INSTANCE = new NoopChunkLoaderImpl();
private NoopChunkLoaderImpl(){}
@Override
public @NotNull CompletableFuture<@Nullable Chunk> loadChunk(@NotNull Instance instance, int chunkX, int chunkZ) {
return CompletableFuture.completedFuture(null);
}
@Override
public @NotNull CompletableFuture<Void> saveChunk(@NotNull Chunk chunk) {
return AsyncUtils.VOID_FUTURE;
}
}