Skip to main content

NbtRegion

Creating an NbtRegion

After obtaining a Uint8Array of the region file, you can give it to NbtRegion.read to create an NbtRegion. This will read the region file header, but it won't read any chunks yet.

const response = await fetch('./r.0.0.mca')
const arrayBuffer = await response.arrayBuffer()
const region = NbtRegion.read(new Uint8Array(arrayBuffer, {}))

Methods

getChunkPositions()

Returns a list of the position of each chunk, relative to the region (so the x and z coordinates will always be between 0 and 31 inclusive).

const chunks = region.getChunkPositions()
console.log(chunks) // [[0, 0], [0, 1], [0, 2], [6, 3], [6, 4]]

getChunk(index)

Returns the NbtChunk by index, or undefined if the chunk doesn't exist.

findChunk(x, z)

Returns the NbtChunk by its x and z coordinates (region relative), or undefined if the chunk doesn't exist.

getFirstChunk()

Returns the first NbtChunk in the region, or undefined if this region is completely empty.

filter(predicate)

Returns a filtered list of NbtChunk using a predicate. Chunks that are undefined are not checked by the predicate and are filtered out.

const chunks = region.filter(chunk => chunk.x === 0)

map(mapper)

Returns a mapped list of NbtChunk using a mapper function. Chunks that are undefined are not mapped and are filtered out.

const compressions = new Set(region.map(chunk => chunk.compression))
if (compressions.size > 1) {
console.log('Region has mixed compression!')
}

write()

Turns this region into a Uint8Array.

const array = region.write()

toJson()

Serializes the region to a format which can be represented by JSON. This can be necessary when using workers.

const json = region.toJson()
// send the data to a different worker
const region2 = NbtRegion.fromJson(json, chunkResolver)
info

For performance reasons, the serialized data does not contain the raw chunk data. The region obtained from NbtRegion.fromJson returns an NbtRegion.Ref. chunkResolver is necessary to asynchronously request the data of a chunk.

NbtRegion.Ref

A region ref acts very similar to a normal NbtRegion, most notable changes:

  • Instead of working with NbtChunk, it uses NbtChunk.Ref.
  • It does not have thewrite() or toJson() methods.