Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prismarine-chunk

PrismarineJS57.9kMIT1.39.0TypeScript support: included

A class to hold chunk data for prismarine

minecraft, voxel, chunk, world

readme

prismarine-chunk

NPM version Build Status Discord Gitter Irc

Try it on gitpod

A class to hold chunk data for Minecraft: PC 1.8, 1.9, 1.10, 1.11, 1.12, 1.13, 1.14, 1.15 and 1.16 and Bedrock Edition 0.14 and 1.0, 1.16, 1.17, 1.18, 1.19 and 1.20

Usage

const registry = require('prismarine-registry')('1.8')
const ChunkColumn = require('prismarine-chunk')(registry)
const { Vec3 } = require("vec3")

const chunk = new ChunkColumn()

for (let x = 0; x < 16;x++) {
  for (let z = 0; z < 16; z++) {
    chunk.setBlockType(new Vec3(x, 50, z), 2)
    for (let y = 0; y < 256; y++) {
      chunk.setSkyLight(new Vec3(x, y, z), 15)
    }
  }
}

console.log(JSON.stringify(chunk.getBlock(new Vec3(3,50,3)),null,2))

Test data

pc

Test data can be generated with minecraftChunkDumper.

Install it globally with npm install minecraft-chunk-dumper -g then run :

minecraftChunkDumper saveChunks 1.20 "1_20" 10

bedrock

Run tests in bedrock-provider (which loads chunks through a client as part of its tests) and copy the generated data from fixtures/$version/pchunk into tests/bedrock_$version.

For the version, copy one chunk column of level_chunk without caching, level_chunk with caching, level_chunk CacheMissResponse, subchunk without caching, subchunk cached and subchunk CacheMissResponse into the test/version folder.

Note: bedrock-provider tests network decoding and loading chunks from a save database. The tests in prismarine-chunk test other parts of the chunk API, such as setting and getting block light, type, biome, entity and block entity data.

API

Chunk

Chunk(initData: { minY?: number, worldHeight?: number })

Build a new chunk. initData is only for 1.18+, and if not given or null the world will default to an old-style 0-256 world.

Chunk.initialize(iniFunc)

Initialize a chunk.

  • iniFunc is a function(x,y,z) returning a prismarine-block.

That function is faster than iterating and calling the setBlock* manually. It is useful to generate a whole chunk and load a whole chunk.

Chunk.version

returns the version the chunk loader was called with

Chunk.section

returns ChunkSection class for version

Chunk.getBlock(pos)

Get the Block at pos

.entity will have entity NBT data for this block, if it exists

Chunk.setBlock(pos,block)

Set the Block at pos

Set .entity property with NBT data for this block to load block entity data for the block

Chunk.getBlockType(pos)

Get the block type at pos

Chunk.getBlockStateId(pos)

Get the block state id at pos

Chunk.getBlockData(pos)

Get the block data (metadata) at pos

Chunk.getBlockLight(pos)

Get the block light at pos

Chunk.getSkyLight(pos)

Get the block sky light at pos

Chunk.getBiome(pos)

Get the block biome id at pos

Chunk.getBiomeColor(pos)

Get the block biome color at pos. Does nothing for PC.

Chunk.setBlockStateId(pos, stateId)

Set the block type stateId at pos

Chunk.setBlockType(pos, id)

Set the block type id at pos

Chunk.setBlockData(pos, data)

Set the block data (metadata) at pos

Chunk.setBlockLight(pos, light)

Set the block light at pos

Chunk.setSkyLight(pos, light)

Set the block sky light at pos

Chunk.setBiome(pos, biome)

Set the block biome id at pos

Chunk.setBiomeColor(pos, biomeColor)

Set the block biomeColor at pos. Does nothing for PC.

Chunk.getBlockEntity(pos)

Returns the block entity data at position if it exists

Chunk.setBlockEntity(pos, nbt)

Sets block entity data at position

Chunk.loadBlockEntities(nbt)

Loads an array of NBT data into the chunk column

Chunk.toJson()

Returns the chunk as json

Chunk.fromJson(j)

Load chunk from json

Chunk.sections

Available for pc chunk implementation. Array of y => section Can be used to identify whether a section is empty or not (will be null if it's the case) For version >= 1.9, contains a .palette property which contains all the stateId of this section, can be used to check quickly whether a given block is in this section.

pc

Chunk.getMask()

Return the chunk bitmap 0b0000_0000_0000_0000(0x0000) means no chunks are set while 0b1111_1111_1111_1111(0xFFFF) means all chunks are set

Chunk.dump()

Returns the chunk raw data

Chunk.load(data, bitmap = 0xFFFF, skyLightSent = true, fullChunk = true)

Load raw data into the chunk

Chunk.dumpLight()

Returns the chunk raw light data (starting from 1.14)

Chunk.loadLight(data, skyLightMask, blockLightMask, emptySkyLightMask = 0, emptyBlockLightMask = 0)

Load lights into the chunk (starting from 1.14)

Chunk.loadParsedLight (skyLight, blockLight, skyLightMask, blockLightMask, emptySkyLightMask, emptyBlockLightMask)

Load lights into the chunk (starting from 1.17)

Chunk.dumpBiomes()

Returns the biomes as an array (starting from 1.15)

Chunk.loadBiomes(biomes)

Load biomes into the chunk (starting from 1.15)

bedrock

See index.d.ts

ChunkSection

pc

static fromJson(j: any): ChunkSection

static sectionSize(skyLightSent?: boolean): number

constructor(skyLightSent?: boolean)

data: Buffer

toJson(): { type: "Buffer"; data: number[]; }

initialize(iniFunc: any): void

getBiomeColor(pos: Vec3): { r: number; g: number; b: number; }

setBiomeColor(pos: Vec3, r: number, g: number, b: number): void

getBlockStateId(pos: Vec3): number

getBlockType(pos: Vec3): number

getBlockData(pos: Vec3): number

getBlockLight(pos: Vec3): number

getSkyLight(pos: Vec3): number

setBlockStateId(pos: Vec3, stateId: number): void

setBlockType(pos: Vec3, id: number): void

setBlockData(pos: Vec3, data: Buffer): void

setBlockLight(pos: Vec3, light: number): void

setSkyLight(pos: Vec3, light: number): void

dump(): Buffer

load(data: Buffer, skyLightSent?: boolean): void

bedrock

See index.d.ts

compact()

Shrinks the size of the SubChunk if possible after setBlock operations are done

getPalette()

Returns a list of unique block states that make up the chunk section

changelog

History

1.39.0

1.38.1

1.38.0

1.37.0

1.36.0

1.35.0

1.34.0

  • 1.20 support

1.33.1

  • Fix attempting to set skylight in chunks with no skylight

1.33.0

  • Bedrock 1.19.1 support, fix pc 1.18 world height from disk

1.32.0

  • 1.19 support

1.31.0

  • update mcdata

1.30.0

  • Bedrock 1.16 - 1.18.0 chunks (@extremeheat)
  • Block sections are not biomes (@nickelpro)

1.29.0

  • Implement prismarine-registry, basic block entities and 1.18 disk loading

1.28.1

  • improve the palette hack for 1.18

1.28.0

  • expose palette in 1.18

1.27.0

  • Add 1.18 chunk support (@nickelpro)

1.26.0

  • Fix fromLongArray index bug (@nickelpro)
  • Fix bitArray Or bug (@nickelpro)
  • Correctly update empty light sections (@nickelpro)
  • Be more correct about updating light masks (@nickelpro)
  • Add BitArray.or test (@nickelpro)

1.25.0

  • Add type info and bounds checks (@nickelpro)
  • Fix skyLightMask bookkeeping (@nickelpro)
  • Set GLOBAL_BITS_PER_BLOCK to 16 (@nickelpro)
  • Expose ChunkSections (@extremeheat)

1.24.0

  • 1.17 support (thanks @nickelpro @Archengius @u9g)

1.23.0

  • Add toArray/fromArray to BitArrays (@Karang)
  • Use Uint32Array instead of Array (@Saiv46)
  • add version property to chunk object (@u9g)
  • Fix pe -> bedrock (@nickelpro)

1.22.0

  • optimize for browser by inlining getSectionIndex and removing asserts (@rom1504)

1.21.0

  • fix initialize in all versions but 1.8 (@rom1504)
  • add typescript typings (@Darkflame72)

1.20.3

  • Several bug fix (thanks @IdanHo)

1.20.2

  • Discard the 0 length of the missing palette array in 1.9 (thanks @IdanHo)

1.20.1

  • Return air when reading y < 0 or y >= 256

1.20.0

  • 1.16 support

1.19.0

  • setBlockData for 1.13, 1.14, 1.15 (thanks @Deudly)

1.18.1

  • fix bitwise unsigned operators => fix dumping chunks for 1.9->1.12

1.18.0

  • reimplement 1.9->1.12 in a similar way to 1.13 (remove protodef dependency)
  • implement full chunk for 1.8
  • add empty load and dump biomes and light methods for simplicity in all versions

1.17.0

  • support for full chunk property (thanks @Karang)
  • fix bug in json serialization

1.16.0

  • support for 1.15 chunk (thanks @Karang)

1.15.0

  • support for 1.14 chunk (thanks @Karang)

1.14.0

  • faster 1.13 chunk implementation (thanks @Karang)

1.13.0

  • fast json serialization/parsing for out of process loading/writing

1.12.0

  • 1.13 support (thanks @hornta)

1.11.1

  • fix dumping for noSkylight chunks for 1.9-1.12 (thanks @IdanHo)

1.11.0

  • add chunk handling for chunks without skylight data in 1.8 (thanks @skullteria)

1.10.0

  • support 1.13
  • better tests

1.9.1

  • standardjs
  • circleci 2
  • better no chunk implementation exception

1.9.0

  • small 1.9 fix (thanks @Flynnn)
  • handle skylightsent in 1.8

1.8.2

  • fix initialize in 1.8 + test

1.8.1

  • fix initialize in 1.8

1.8.0

  • optimization of 1.9 chunk done by @allain

1.7.0

  • supports mcpc 1.12 (same as 1.9)

1.6.0

  • add skyLightSent to load

1.5.1

  • use last protodef, fix longToByte (no countTypeArgs), and remove gulp

1.5.0

  • supports mcpc 1.10 and 1.11 (same as 1.9)

1.4.0

  • supports mcpc 1.9 (thanks @Flynnn)

1.3.0

  • supports bitmap in load and dump in 1.8, default to bitmap == 0xFFFF

1.2.0

  • support MCPE 1.0 chunks

1.1.0

  • support MCPE 0.14 chunks

1.0.1

  • update to babel6

1.0.0

  • bump dependencies

0.3.2

  • simplify and fix initialize

0.3.1

  • fix iniPos in initialize

0.3.0

  • add Chunk.initialize, useful for fast generation

0.2.1

  • fix the badge

0.2.0

  • use vec3
  • add an example + doc
  • use prismarine-block

0.1.0

  • First version, basic functionality