4

I am currently making a Minecraft chunk manipulation program, and I would like to know how Minecraft chunk sections are stored.

From the Minecraft Wiki Article on how Minecraft Stores its chunks.

Sections: List of Compound tags, each tag is a sub-chunk of sorts.

An individual Section.

Y: The Y index (not coordinate) of this section. Range 0 to 15 (bottom to top), with no duplicates but some sections may be missing if empty.

Blocks: 4096 bytes of block IDs defining the terrain. 8 bits per block, plus ? the bits from the below Add tag.

Add: May not exist. 2048 bytes of additional block ID data. The value to add to (combine with) the above block ID to form the true block ID in the range 0 to 4095. 4 bits per block. Combining is done by shifting this value to the left 8 bits and then adding it to the block ID from above.

Data: 2048 bytes of block data additionally defining parts of the terrain. 4 bits per block.

BlockLight: 2048 bytes recording the amount of block-emitted light in each block. Makes load times faster compared to recomputing at load time. 4 bits per block.

SkyLight: 2048 bytes recording the amount of sunlight or moonlight hitting each block. 4 bits per block.

But I do not understand how Blocks is read. Every Section in a Chunk is 16 x 16 x 16 blocks. But instead Minecraft stores the blocks in a section in a 1-d array.

Community
  • 1
  • 1
frogocomics
  • 41
  • 1
  • 5

1 Answers1

5

A 3D array is also stored linear in RAM. You need to convert coordinates to an index. For the "Blocks" Tag its this formula:

Index = Ycoord * 256 + Zcoord * 16 + Xcoord

This is called YZX-order. The tags "HeightMap" or "Biomes" use ZX-order (Index = Zcoord * 16 + X)

Mystery
  • 171
  • 9
  • Why do people call it 'YZX-order', which puts the major-order (X) last? As that stands, 'ZX-order' would be a subset of 'YZX-order' ... Wouldn't it make more sense to call these 'XZY' and 'XZ' ordering? This way we could say "XZ-ordering is a subset of XZY-ordering". This would put the LSB first in the name, just like 'row-major' and 'col-major' already do. – thenumbernine May 06 '23 at 12:12