chunk

package
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 12 Imported by: 6

Documentation

Index

Constants

View Source
const (
	// SubChunkVersion is the current version of the written sub chunks, specifying the format they are
	// written on disk and over network.
	SubChunkVersion = 9
	// CurrentBlockVersion is the current version of blocks (states) of the game. This version is composed
	// of 4 bytes indicating a version, interpreted as a big endian int. The current version represents
	// 1.21.60.33 {1, 21, 60, 33}.
	CurrentBlockVersion int32 = 18168865
)

Variables

View Source
var (
	// DiskEncoding is the Encoding for writing a Chunk to disk. It writes block palettes using NBT and does not use
	// varints.
	DiskEncoding diskEncoding
	// NetworkEncoding is the Encoding used for sending a Chunk over network. It does not use NBT and writes varints.
	NetworkEncoding networkEncoding
	// BiomePaletteEncoding is the paletteEncoding used for encoding a palette of biomes.
	BiomePaletteEncoding biomePaletteEncoding
	// BlockPaletteEncoding is the paletteEncoding used for encoding a palette of block states encoded as NBT.
	BlockPaletteEncoding blockPaletteEncoding
)

Functions

func DecodeBiomes

func DecodeBiomes(buf *bytes.Buffer, c *Chunk, e Encoding) error

DecodeBiomes reads the paletted storages holding biomes from buf and stores it into the Chunk passed.

func EncodeBiomes

func EncodeBiomes(c *Chunk, e Encoding) []byte

EncodeBiomes encodes the biomes of a chunk into bytes. An Encoding may be passed to encode either for network or disk purposed, the most notable difference being that the network encoding generally uses varints and no NBT.

func EncodeSubChunk

func EncodeSubChunk(c *SubChunk, r define.Range, ind int, e Encoding) []byte

EncodeSubChunk encodes a sub-chunk from a chunk into bytes. An Encoding may be passed to encode either for network or disk purposed, the most notable difference being that the network encoding generally uses varints and no NBT.

Types

type Chunk

type Chunk struct {
	// contains filtered or unexported fields
}

Chunk is a segment in the world with a size of 16x16x256 blocks. A chunk contains multiple sub chunks and stores other information such as biomes. It is not safe to call methods on Chunk simultaneously from multiple goroutines.

func DiskDecode

func DiskDecode(data SerialisedData, r define.Range) (*Chunk, error)

DiskDecode decodes the data from a SerialisedData object into a chunk and returns it. If the data was invalid, an error is returned.

func NetworkDecode

func NetworkDecode(air uint32, data []byte, count int, r define.Range) (*Chunk, error)

NetworkDecode decodes the network serialised data passed into a Chunk if successful. If not, the chunk returned is nil and the error non-nil. The sub chunk count passed must be that found in the LevelChunk packet. noinspection GoUnusedExportedFunction

func NewChunk

func NewChunk(air uint32, r define.Range) *Chunk

NewChunk initialises a new chunk and returns it, so that it may be used.

func (*Chunk) Biome

func (chunk *Chunk) Biome(x uint8, y int16, z uint8) uint32

Biome returns the biome ID at a specific column in the chunk.

func (*Chunk) Biomes

func (chunk *Chunk) Biomes() [][]uint32

Blocks returns all biome IDs for each blocks in this chunk. The length of returned slice is the sub chunk counts of this chunk. For example, for a completely overworld chunk, the length is 24. Note that for each slice in this big slice, the length of it is 4096 (16*16*16).

func (*Chunk) Block

func (chunk *Chunk) Block(x uint8, y int16, z uint8, layer uint8) uint32

Block returns the runtime ID of the block at a given x, y and z in a chunk at the given layer. If no sub chunk exists at the given y, the block is assumed to be air.

func (*Chunk) Blocks

func (chunk *Chunk) Blocks(layer uint8) [][]uint32

Blocks returns all blocks (block runtime ids) whose in the target layer of this chunk. The length of returned slice is the sub chunk counts of this chunk. For example, for a completely overworld chunk, the length is 24. Note that for each slice in this big slice, the length of it is 4096 (16*16*16).

func (*Chunk) Compact

func (chunk *Chunk) Compact()

Compact compacts the chunk as much as possible, getting rid of any sub chunks that are empty, and compacts all storages in the sub chunks to occupy as little space as possible. Compact should be called right before the chunk is saved in order to optimise the storage space.

func (*Chunk) Equals

func (chunk *Chunk) Equals(c *Chunk) bool

Equals returns if the chunk passed is equal to the current one

func (*Chunk) HighestBlock added in v1.5.0

func (chunk *Chunk) HighestBlock(x, z uint8) int16

TODO: Expose this function to C API.

HighestBlock iterates from the highest non-empty sub chunk downwards to find the Y value of the highest non-air block at an x and z. If no blocks are present in the column, the minimum height is returned.

func (*Chunk) HighestFilledSubChunk

func (chunk *Chunk) HighestFilledSubChunk() uint16

HighestFilledSubChunk returns the index of the highest sub chunk in the chunk that has any blocks in it. 0 is returned if no subchunks have any blocks.

func (*Chunk) Range

func (chunk *Chunk) Range() define.Range

Range returns the define.Range of the Chunk as passed to NewChunk.

func (*Chunk) SetBiome

func (chunk *Chunk) SetBiome(x uint8, y int16, z uint8, biome uint32)

SetBiome sets the biome ID at a specific column in the chunk.

func (*Chunk) SetBiomes

func (chunk *Chunk) SetBiomes(biomes [][]uint32)

SetBiomes sets the biome IDs for each blocks in this chunk. The length of biomes could less or bigger than the sub chunk counts of this whole chunk.

If less, then only the given part will be modified, if bigger, then the bigger part will be not used.

The length of each slice in this big slice must be 4096 (16*16*16).

func (*Chunk) SetBlock

func (chunk *Chunk) SetBlock(x uint8, y int16, z uint8, layer uint8, block uint32)

SetBlock sets the runtime ID of a block at a given x, y and z in a chunk at the given layer. If no SubChunk exists at the given y, a new SubChunk is created and the block is set.

func (*Chunk) SetBlocks

func (chunk *Chunk) SetBlocks(layer uint8, blocks [][]uint32)

SetBlocks sets the whole chunk blocks in layer by given block runtime ids. The length of blocks could less or bigger than the sub chunk counts of this whole chunk.

If less, then only the given part will be modified, if bigger, then the bigger part will be not used.

The length of each slice in this big slice must be 4096 (16*16*16).

func (*Chunk) SetSub

func (chunk *Chunk) SetSub(subChunks []*SubChunk)

SetSub overwrite the sub chunks of this chunk.

The length of subChunks could less or bigger than the sub chunk counts of this whole chunk.

If less, then only the given part will be modified, if bigger, then the bigger part will be not used.

func (*Chunk) SetSubChunk

func (chunk *Chunk) SetSubChunk(subChunk *SubChunk, index int16)

SetSubChunk set the a sub chunk of this chunk. index is refer to the sub chunk Y index of this sub chunk.

func (*Chunk) Sub

func (chunk *Chunk) Sub() []*SubChunk

Sub returns a list of all sub chunks present in the chunk.

func (*Chunk) SubChunk

func (chunk *Chunk) SubChunk(y int16) *SubChunk

SubChunk finds the correct SubChunk in the Chunk by a Y value.

func (*Chunk) SubIndex

func (chunk *Chunk) SubIndex(y int16) int16

SubIndex returns the sub chunk Y index matching the y value passed.

func (*Chunk) SubY

func (chunk *Chunk) SubY(index int16) int16

SubY returns the sub chunk Y value matching the index passed.

type Encoding

type Encoding interface {
	// contains filtered or unexported methods
}

Encoding is an encoding type used for Chunk encoding. Implementations of this interface are DiskEncoding and NetworkEncoding, which can be used to encode a Chunk to an intermediate disk or network representation respectively.

type Palette

type Palette struct {
	// contains filtered or unexported fields
}

Palette is a palette of values that every PalettedStorage has. Storages hold 'pointers' to indices in this palette.

func NewPalette

func NewPalette(size paletteSize, values []uint32) *Palette

NewPalette returns a new Palette with size and a slice of added values.

func (*Palette) Add

func (palette *Palette) Add(v uint32) (index int16, resize bool)

Add adds a values to the Palette. It does not first check if the value was already set in the Palette. The index at which the value was added is returned. Another bool is returned indicating if the Palette was resized as a result of adding the value.

func (*Palette) Index

func (palette *Palette) Index(runtimeID uint32) int16

Index loops through the values of the Palette and looks for the index of the given value. If the value could not be found, -1 is returned.

func (*Palette) Len

func (palette *Palette) Len() int

Len returns the amount of unique values in the Palette.

func (*Palette) Replace

func (palette *Palette) Replace(f func(v uint32) uint32)

Replace calls the function passed for each value present in the Palette. The value returned by the function replaces the value present at the index of the value passed.

func (*Palette) Value

func (palette *Palette) Value(i uint16) uint32

Value returns the value in the Palette at a specific index.

type PalettedStorage

type PalettedStorage struct {
	// contains filtered or unexported fields
}

PalettedStorage is a storage of 4096 blocks encoded in a variable amount of uint32s, storages may have values with a bit size per block of 0, 1, 2, 3, 4, 5, 6, 8 or 16 bits. 3 of these formats have additional padding in every uint32 and an additional uint32 at the end, to cater for the blocks that don't fit. This padding is present when the storage has a block size of 3, 5 or 6 bytes. Methods on PalettedStorage must not be called simultaneously from multiple goroutines.

func (*PalettedStorage) At

func (storage *PalettedStorage) At(x, y, z byte) uint32

At returns the value of the PalettedStorage at a given x, y and z.

func (*PalettedStorage) Equal

func (storage *PalettedStorage) Equal(other *PalettedStorage) bool

Equal checks if two PalettedStorages are equal value wise. False is returned if either of the storages are nil.

func (*PalettedStorage) Palette

func (storage *PalettedStorage) Palette() *Palette

Palette returns the Palette of the PalettedStorage.

func (*PalettedStorage) Set

func (storage *PalettedStorage) Set(x, y, z byte, v uint32)

Set sets a value at a specific x, y and z. The Palette and PalettedStorage are expanded automatically to make space for the value, should that be needed.

type SerialisedData

type SerialisedData struct {
	// sub holds the data of the serialised sub chunks in a chunk. Sub chunks that are empty or that otherwise
	// don't exist are represented as an empty slice (or technically, nil).
	SubChunks [][]byte
	// Biomes is the biome data of the chunk, which is composed of a biome storage for each sub-chunk.
	Biomes []byte
}

SerialisedData holds the serialised data of a chunk. It consists of the chunk's block data itself, a height map, the biomes and entities and block entities.

func Encode

func Encode(c *Chunk, e Encoding) SerialisedData

Encode encodes Chunk to an intermediate representation SerialisedData. An Encoding may be passed to encode either for network or disk purposed, the most notable difference being that the network encoding generally uses varints and no NBT.

type SubChunk

type SubChunk struct {
	// contains filtered or unexported fields
}

SubChunk is a cube of blocks located in a chunk. It has a size of 16x16x16 blocks and forms part of a stack that forms a Chunk.

func DecodeSubChunk

func DecodeSubChunk(buf *bytes.Buffer, r define.Range, e Encoding) (subChunk *SubChunk, index int, err error)

DecodeSubChunk decodes a SubChunk from a bytes.Buffer. The Encoding passed defines how the block storages of the SubChunk are decoded.

func NewSubChunk

func NewSubChunk(air uint32) *SubChunk

NewSubChunk creates a new sub chunk. All sub chunks should be created through this function

func (*SubChunk) Block

func (sub *SubChunk) Block(x, y, z byte, layer uint8) uint32

Block returns the runtime ID of the block located at the given X, Y and Z. X, Y and Z must be in a range of 0-15.

func (*SubChunk) Blocks

func (sub *SubChunk) Blocks(layer uint8) []uint32

Blocks returns all blocks (block runtime ids) whose in layer. If layer is not exist, then return blocks with all air. Note that the length of returned slice is 4096 (16*16*16).

func (*SubChunk) Empty

func (sub *SubChunk) Empty() bool

Empty checks if the SubChunk is considered empty. This is the case if the SubChunk has 0 block storages or if it has a single one that is completely filled with air.

func (*SubChunk) Equals

func (sub *SubChunk) Equals(s *SubChunk) bool

Equals returns if the sub chunk passed is equal to the current one.

func (*SubChunk) Layer

func (sub *SubChunk) Layer(layer uint8) *PalettedStorage

Layer returns a certain block storage/layer from a sub chunk. If no storage at the layer exists, the layer is created, as well as all layers between the current highest layer and the new highest layer.

func (*SubChunk) Layers

func (sub *SubChunk) Layers() []*PalettedStorage

Layers returns all layers in the sub chunk. This method may also return an empty slice.

func (*SubChunk) SetBlock

func (sub *SubChunk) SetBlock(x, y, z byte, layer uint8, block uint32)

SetBlock sets the given block runtime ID at the given X, Y and Z. X, Y and Z must be in a range of 0-15.

func (*SubChunk) SetBlocks

func (sub *SubChunk) SetBlocks(layer uint8, blocks []uint32)

SetBlock sets the whole sub chunk in layer by given block runtime ids. len(blocks) must equal to 4096 (16*16*16).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL