Class ChunkCache<T>

java.lang.Object
dev.myriadclient.api.world.ChunkCache<T>
Type Parameters:
T - what is kept per chunk

public final class ChunkCache<T> extends Object
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 Classes
    Modifier and Type
    Class
    Description
    static interface 
    A chunk's value as boxes, each in a colour (ARGB), for ChunkCache.Builder.boxes.
    static interface 
    Takes a chunk's boxes (ChunkCache.Boxes), in world coordinates.
    static final class 
    static interface 
    Works out the value for a loaded chunk, or null to keep nothing for it.
    static interface 
    Draws a chunk's value into its mesh, in world coordinates.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    forEach(Consumer<? super T> action)
    Runs action on every chunk's value (chunks without one are skipped).
    void
    Each chunk's mesh that has something in it.
    @Nullable T
    get(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 T
    get(net.minecraft.core.BlockPos pos)
    The value for the chunk holding pos, or null if it has none (yet).
    void
    invalidate(int chunkX, int chunkZ)
    Recomputes one chunk on a coming tick; nothing happens for a chunk the cache doesn't cover.
    void
    invalidate(net.minecraft.core.BlockPos pos)
    Recomputes the chunk holding pos.
    void
    Recomputes every chunk: call it when a setting changes what the compute function finds.
    boolean
    Whether it's listening and computing: between start() and stop(), or while its module is on.
    @Nullable RenderLook
    Built with ChunkCache.Builder.boxes: its look, or null.
    static <T> ChunkCache.Builder<T>
    of(Module owner, ChunkCache.Compute<T> compute)
    A cache that runs while owner is enabled, named after it in log messages.
    int[]
    The colours a ChunkCache.Builder.boxes cache's silhouettes index.
    int
    Chunks still waiting to be computed or meshed (async: those being computed included).
    void
    Rebuilds every chunk's mesh from its kept value, without reading the world: call it when a look changes.
    int
    How many chunks hold a value.
    static <T> ChunkCache.Builder<T>
    A cache you start() and stop() yourself (from a service, a panel, a HUD element).
    void
    Starts listening and computing.
    void
    Stops and frees everything (values and meshes).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • of

      public static <T> ChunkCache.Builder<T> of(Module owner, ChunkCache.Compute<T> compute)
      A cache that runs while owner is enabled, named after it in log messages.
    • standalone

      public static <T> ChunkCache.Builder<T> standalone(ChunkCache.Compute<T> compute)
      A cache you start() and stop() yourself (from a service, a panel, a HUD element).
    • get

      public @Nullable T get(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).
    • get

      public @Nullable T get(net.minecraft.core.BlockPos pos)
      The value for the chunk holding pos, or null if it has none (yet).
    • forEach

      public void forEach(Consumer<? super T> action)
      Runs action on every chunk's value (chunks without one are skipped).
    • 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

      public boolean isRunning()
      Whether it's listening and computing: between start() and stop(), or while its module is on.
    • 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 holding pos.
    • invalidateAll

      public void invalidateAll()
      Recomputes every chunk: call it when a setting changes what the compute function finds.
    • look

      @Internal public @Nullable RenderLook look()
      Built with ChunkCache.Builder.boxes: its look, or null.
    • palette

      @Internal public int[] palette()
      The colours a ChunkCache.Builder.boxes cache's silhouettes index.
    • forEachMesh

      @Internal public void forEachMesh(Consumer<WorldMesh> action)
      Each chunk's mesh that has something in it.
    • 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 with of(Module, ChunkCache.Compute) do this when their module turns on.
    • stop

      public void stop()
      Stops and frees everything (values and meshes).