Class ChunkCache<T>
java.lang.Object
dev.myriadclient.api.world.ChunkCache<T>
- Type Parameters:
T- what is kept per chunk
Something worked out once per chunk and kept until that chunk changes: the storage blocks in it, its safe holes, the
ores of a search. Instead of rescanning everything around you every few ticks, a chunk is computed when it loads or
comes into range, and again only when a block in it changes. With a
mesher each chunk also keeps a
WorldMesh, so what you found is drawn every frame without a render handler.
private final IntSetting range = sgGeneral.integer("Range").defaultValue(8).range(1, 32).build();
private final ChunkCache<List<BlockPos>> debris = ChunkCache.of(this, chunk -> {
List<BlockPos> found = new ArrayList<>();
BlockScan.forEach(chunk, s -> s.is(Blocks.ANCIENT_DEBRIS), (pos, state) -> found.add(pos.immutable()));
return found.isEmpty() ? null : found; // null: nothing to keep for this chunk
})
.range(range::get) // chunks around the player (default: render distance)
.mesh((found, mesh) -> { // optional: draw it, rebuilt only when the chunk changes
for (BlockPos pos : found) mesh.blockShape(pos, fill.argb(), line.argb(), ShapeMode.BOTH, true);
})
.build();
public MyModule() {
...
settings.onAnyChanged(s -> debris.remeshAll()); // colours changed: redraw, no rescan needed
}
Built with of(Module, Compute), the cache runs while its module is enabled and frees everything when it's
disabled. Work is spread over ticks within a time budget, nearest chunks first; the theme changing re-meshes
automatically. Unless built ChunkCache.Builder.async(), everything runs on the client thread, so compute functions can
read the world freely.
Keep the compute function to facts about the world, and turn settings into looks in the mesher: then a colour or
shape setting only needs remeshAll(), which never touches the world. Call invalidateAll() when a
setting changes what is found.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA chunk's value as boxes, each in a colour (ARGB), forChunkCache.Builder.boxes.static interfaceTakes a chunk's boxes (ChunkCache.Boxes), in world coordinates.static final classstatic interfaceWorks out the value for a loaded chunk, or null to keep nothing for it.static interfaceDraws a chunk's value into its mesh, in world coordinates. -
Method Summary
Modifier and TypeMethodDescriptionvoidRunsactionon every chunk's value (chunks without one are skipped).voidforEachMesh(Consumer<WorldMesh> action) Each chunk's mesh that has something in it.@Nullable Tget(int chunkX, int chunkZ) The value for a chunk (chunk coordinates: block x and z divided by 16), or null if it has none (yet).@Nullable Tget(net.minecraft.core.BlockPos pos) The value for the chunk holdingpos, or null if it has none (yet).voidinvalidate(int chunkX, int chunkZ) Recomputes one chunk on a coming tick; nothing happens for a chunk the cache doesn't cover.voidinvalidate(net.minecraft.core.BlockPos pos) Recomputes the chunk holdingpos.voidRecomputes every chunk: call it when a setting changes what the compute function finds.boolean@Nullable RenderLooklook()Built withChunkCache.Builder.boxes: its look, or null.static <T> ChunkCache.Builder<T> of(Module owner, ChunkCache.Compute<T> compute) A cache that runs whileowneris enabled, named after it in log messages.int[]palette()The colours aChunkCache.Builder.boxescache's silhouettes index.intpending()Chunks still waiting to be computed or meshed (async: those being computed included).voidRebuilds every chunk's mesh from its kept value, without reading the world: call it when a look changes.intsize()How many chunks hold a value.static <T> ChunkCache.Builder<T> standalone(ChunkCache.Compute<T> compute) voidstart()Starts listening and computing.voidstop()Stops and frees everything (values and meshes).
-
Method Details
-
of
A cache that runs whileowneris enabled, named after it in log messages. -
standalone
-
get
The value for a chunk (chunk coordinates: block x and z divided by 16), or null if it has none (yet). -
get
The value for the chunk holdingpos, or null if it has none (yet). -
forEach
-
size
public int size()How many chunks hold a value. -
pending
public int pending()Chunks still waiting to be computed or meshed (async: those being computed included). 0 once the cache has caught up. -
isRunning
-
invalidate
public void invalidate(int chunkX, int chunkZ) Recomputes one chunk on a coming tick; nothing happens for a chunk the cache doesn't cover. -
invalidate
public void invalidate(net.minecraft.core.BlockPos pos) Recomputes the chunk holdingpos. -
invalidateAll
public void invalidateAll()Recomputes every chunk: call it when a setting changes what the compute function finds. -
look
Built withChunkCache.Builder.boxes: its look, or null. -
palette
@Internal public int[] palette()The colours aChunkCache.Builder.boxescache's silhouettes index. -
forEachMesh
-
remeshAll
public void remeshAll()Rebuilds every chunk's mesh from its kept value, without reading the world: call it when a look changes. -
start
public void start()Starts listening and computing. Caches made withof(Module, ChunkCache.Compute)do this when their module turns on. -
stop
public void stop()Stops and frees everything (values and meshes).
-