ChunkManager - rvandoosselaer/Blocks GitHub Wiki
The ChunkManager takes care of the hassle of managing the chunks. Loading of chunks, generating of chunks, mesh construction, ...
Creating the ChunkManager:
boolean triggerAdjacentChunkUpdates)
ChunkManager chunkManager = ChunkManager.builder()
.generatorPoolSize(2)
.meshPoolSize(2)
.generator(new ChunkNoiseGenerator(System.currentTimeMillis()))
.triggerAdjacentChunkUpdates(true)
.build();
ChunkManager settings:
Parameter | Description | Default value |
---|---|---|
cacheSize | The size of the internal chunk cache. | Based on the size of the chunk grid. For the default chunk grid size of (9, 5, 9) the size of cache will be 405. |
meshPoolSize | The size of the thread pool for mesh generation tasks. | 1 |
repositoryPoolSize | The size of the thread pool for chunk loading and chunk storing. | 1 |
generatorPoolSize | The size of the thread pool for chunk generation. | 1 |
repository | The repository implementation to use for loading and storing chunks. | |
generator | The implementation to use for generating the block data of chunks. | |
cacheMaintenanceInterval | The interval in milliseconds between cache maintenance operations. | 1000 |
The ChunkManager needs to be initialized before it can be used and should always be cleaned up to properly shutdown the worker thread pools. When using the ChunkManager, the update()
method should be called at regular intervals. It is a good practice to handle the lifecycle of the ChunkManager in the ChunkManagerState
so the jMonkeyEngine state manager takes care of the lifecycle for you.
ChunkManagerState chunkManagerState = new ChunkManagerState(chunkManager);
stateManager.attach(chunkManagerState);
Chunk management
A chunk can be retrieved from the cache of the ChunkManagerState by using the getChunk()
method. When the chunk isn't available in the cache, it can be requested with the requestChunk()
method.
The ChunkManagerState will first try to load the requested chunk using the ChunkRepository
. When this is not successful, or there isn't a ChunkRepository set, the BlocksManager will try to generate the chunk using the ChunkGenerator
. When this also fails, an empty chunk will be created. The requested chunk is placed in the cache and can be retrieved using the getChunk() method.
💡 classes can register a
ChunkManagerListener
to the ChunkManager, to get notified when a chunk is available for retrieval or when the mesh of a chunk is updated.
See javadoc