SerenityJS
    Preparing search index...

    Represents a chunk within a Dimension instance. Chunks hold sub chunks, which hold block states (BlockPermutations). Chunks can be dirty, meaning they have been modified and need to be saved.

    Example Usage

    	import { BlockIdentifier, BlockPermutation } from "@serenityjs/block"
    import { DimensionType } from "@serenityjs/protocol"
    import { Chunk } from "@serenityjs/world"

    // Create a new chunk with the dimension type "Overworld" and the x and z coordinates of 0
    const chunk = new Chunk(DimensionType.Overworld, 0, 0) // DimensionType will determine the maximum height of the chunk

    // First we need to obtain the BlockPermutations we will use to set the blocks
    const bedrock = BlockPermutation.resolve(BlockIdentifier.Bedrock)
    const dirt = BlockPermutation.resolve(BlockIdentifier.Dirt, { dirt_type: "normal" })
    const grass = BlockPermutation.resolve(BlockIdentifier.GrassBlock)

    // We can now set a block at the x, y, and z coordinates of the chunk
    // We will create a Superflat chunk with a maximum height of 4
    for (let x = 0; x < 16; x++) {
    for (let z = 0; z < 16; z++) {
    for (let y = 0; y < 4; y++) {
    // Check if the y coordinate is 0, if so, set the block to bedrock
    if (y === 0) chunk.setPermutation(x, y, z, bedrock)

    // Check if the y coordinate is 1 and 2, if so, set the block to dirt
    else if (y === 1 || y === 2) chunk.setPermutation(x, y, z, dirt)

    // Check if the y coordinate is 3, if so, set the block to grass
    else if (y === 3) chunk.setPermutation(x, y, z, grass)
    }
    }
    }
    Index

    Constructors

    • Creates a new chunk.

      Parameters

      • x: number

        The X coordinate of the chunk.

      • z: number

        The Z coordinate of the chunk.

      • type: DimensionType

        The dimension type of the chunk.

      • Optionalsubchunks: SubChunk[]

        The sub chunks of the chunk.

      Returns Chunk

    Properties

    cache: null | Buffer = null

    A cached buffer of the serialized chunk.

    dirty: boolean = false

    If the chunk has been modified, and has not been saved.

    hash: bigint

    The hash key of the chunk.

    ready: boolean = true

    If the chunk is ready to be sent to the client.

    subchunks: SubChunk[]

    The sub chunks of the chunk.

    type: DimensionType

    The dimension type of the chunk.

    x: number

    The X coordinate of the chunk.

    z: number

    The Z coordinate of the chunk.

    MAX_SUB_CHUNKS: 24

    The maximum amount of sub chunks.

    Methods

    • Get the bottommost level in which a permutation is not air, at the given X and Z coordinates.

      Parameters

      • position: IPosition

        The position to query.

      Returns number

      The bottommost level in which a permutation is not air.

    • Get the topmost level in which a permutation is not air, at the given X and Z coordinates.

      Parameters

      • position: IPosition

        The position to query.

      Returns number

      The topmost level in which a permutation is not air.

    • Set the permutation at the given X, Y and Z coordinates.

      Parameters

      • position: IPosition

        The position.

      • permutation: BlockPermutation

        The permutation.

      • layer: number = 0

        The state layer.

      • dirty: boolean = true

      Returns void

    • Deserialize a buffer into a chunk.

      Parameters

      • type: DimensionType

        The dimension type of the chunk.

      • x: number

        The X coordinate of the chunk.

      • z: number

        The Z coordinate of the chunk.

      • buffer: Buffer

        The buffer to deserialize.

      • nbt: boolean = false

        If block palette should be deserialized as NBT.

      Returns Chunk

      The deserialized chunk.

    • Serialize the chunk into a buffer.

      Parameters

      • chunk: Chunk

        The chunk to serialize.

      • nbt: boolean = false

        If block palette should be serialized as NBT.

      Returns Buffer

      The serialized buffer.