Class ChunkSectionCache

java.lang.Object
ac.boar.anticheat.compensated.world.cache.ChunkSectionCache

public final class ChunkSectionCache extends Object
A shared, thread-safe cache of decoded chunk sections. - Many players receive the same chunk bytes. The cache decodes these bytes one time. All of these players then share one BoarChunkSection object. This lowers memory use and decode work. - Sharing is safe because no code writes to a shared section. When a player changes a block, CompensatedWorld#updateBlock first makes a private copy of the section for that player. This is copy-on-write. The other players keep the unchanged shared section. See BoarChunkSection#isShared(). - Cleanup is automatic. The cache holds each section through a weak reference. When no player world uses a section, the garbage collector removes the section and its cache entry.
  • Constructor Details

    • ChunkSectionCache

      public ChunkSectionCache()
  • Method Details

    • allAirSection

      public BoarChunkSection allAirSection(int airId)
    • getOrDecode

      public BoarChunkSection getOrDecode(io.netty.buffer.ByteBuf payload, int airId, Supplier<BoarChunkSection> decoder)
      Returns the shared section for the given chunk bytes. If the cache has no section for these bytes, this method runs decoder and caches the result. If decoder returns null, the decode failed. This method then returns null and caches nothing. An all-air section decodes to a normal section. The cache stores and shares it like all other sections. - Players can use different block palettes, for example different Minecraft versions or custom blocks. Sharing is still safe because a cached section stores the raw ids from the packet, not translated blocks. Each player translates a raw id to a block when the player reads it, in CompensatedWorld#getBlockAt. Two players share a section only when their raw bytes are identical. A player who shares a section gets the same raw ids that its own decode would produce. One rule keeps this safe: the cache must hold only raw ids, never translated block data.
    • clear

      public void clear()
      Removes all entries. Call this only on shutdown. The garbage collector does the normal cleanup.