Region file format

From Minecraft Wiki
Jump to navigation Jump to search
This article needs cleanup to comply with the style guide.
 [discuss]
Please help improve this page. The talk page may contain suggestions.
BlockSprite rose.png: Sprite image for rose in Minecraft
This page describes content that has been removed and was only present in earlier versions of Java Edition.
 
As of 1.2.1, this format is no longer used.

The Region file format is the binary file format for storing Java Edition chunks from Beta 1.3 to 1.1. Each file stores a group of 32×32 chunks called a region.[a] The format took the place of the Alpha level format, which had been in use since the Infdev development phase, where chunks were stored in individual files on the file system. The file does not begin with a magic number, unlike other file formats, and begins directly with the header. The format has been superseded by the Anvil file format; however, the Anvil file format made changes only to the chunk format and changed the region file extensions from ".mcr" to ".mca".

The system is based on McRegion,[2] a mod by Scaevolus, also known for his development of the Optimine project.[3] The McRegion format was adopted nearly unchanged, except for the addition of a table of chunk update timestamps. JahKob has claimed that this format is up to 7 times faster than the previous system.[4] The difference in a world's total file size between the Region file format and the Alpha level format is negligible.

Location

[edit | edit source]

Region files are found in your Java Edition world directory in the following directories:

  • directory/entities
  • directory/poi
  • directory/region

Where directory may refer to the Minecraft world save directory, or one of the dimensions:​[until 26.1]

  • Overworld: save_directory
  • Nether: save_directory/DIM-1
  • The End: save_directory/DIM1

In which, the region files will take the form r.x.z.mcr. Or when referring to the Anvil file format, the same but with the .mca file extension instead.

The X and Z position of a region file is determined through the floor of dividing the chunk coordinates by 32. For example, a chunk at (30, -3) would be in the region (0, -1), and the chunk at (1500, -600) would be at (46, -19).

Written in code, this is more commonly achieved through performing an Arithmetic shift 5 bits to the right. Note that an arithmetic shift is different from a Logical shift. Most programming languages will perform this when using a signed integer.

Where getting the block position will be as simple as using 512 instead of 32. (3216) And when shifting using 9 instead of 5. (log2(512))

Why bit shifting is used over standard division, even when using integers has to do that integer division will only result in floored division if the integer is positive. If the integer is negative, which it will be 50% of the time, the result is rounded up as opposed to rounded down.

Example

[edit | edit source]

Structure

[edit | edit source]

A region file is built-up out of sectors. These sectors are 4KiB large, which is equal to the common memory page size for operating system kernels. The entire file is sector-aligned, and the total size should thus be a multiple of 4KiB. If the file isn't aligned to this boundary there is a possibility that it is corrupt.

The file has a header of two tables containing the chunk data. Sectors following these tables may be allocated to a chunk. When a chunk is newly created, or grows in sectors, the code will look for this number of sequential sectors that are free. If it fails to find this, the chunk will be appended to the end of the file.

Sector 0, 1 2...
Description Header, contains payload metadata. Payload data and unused sectors.
[edit | edit source]

The first two sectors of the file contain the two headers, occupying one sector each. The first table containing the position data of the chunks in the region file itself, the second providing timestamps for the last update of said chunks.

These tables compose of a 1024 element list (322) containing big-endian, 32-bit integers. Each chunk has a corresponding X and Z position. This is computed through floored division, and taking the modulo of the index, here represented as "i"

  • x=imod32
  • y=i32

With the reverse being done here:

  • i=4(x+32z)

Ensuring X and Z are constrained to a value from 0..31 (inclusive), i.e. the result of a modulo of 32.

To read the position data out of the first table will require reading bytes 4i to 4i+4 (exclusive). The same can be done for the location data, but adding 1024 to both results.

The location data is stored with a specific format, with the first 3 bytes referring to the offset in sectors from the start of the file. The last, and least significant byte giving us the size in sectors of the chunk's data.

This implicitly means that the maximum amount of sectors for a chunk is 255, and thus 1MiB.

Byte 0 1 2 3
Description Offset Length

Payload

[edit | edit source]

Chunk data begins with a (big-endian) four-byte signed length field that indicates the exact length of the remaining chunk data in bytes. The following byte indicates the compression scheme used for chunk data, and the remaining (length-1) bytes are the compressed chunk data.

Minecraft always pads the last chunk's data to be a multiple-of-4096B in length (so that the entire file has a size that is a multiple of 4KiB). Minecraft does not accept files in which the last chunk is not padded. Note that this padding is not included in the length field.

byte 0 1 2 3 4 5...
description length (in bytes) compression type compressed data (length-1 bytes)

There are currently five defined compression schemes:

value method
1 GZip (RFC1952) (unused in practice)
2 Zlib (RFC1950)
3since a version before 1.15.1 Uncompressed
4 LZ4 (since 24w04a, enabled in server.properties)
127 Custom compression algorithm (since 24w05a, for third-party servers)

A namespaced string must follow representing the algorithm used. The string is preceded by its length, encoded as an unsigned 16-bit integer.

The uncompressed data is in NBT format and follows the information detailed on the chunk format article; if compressed with compression scheme 1, the compressed data would be the same as the on-disk content of an Alpha chunk file. Note that chunks are always saved using compression scheme 2 by the official client.

If the value of compression scheme increases by 128, the compressed data is saved in a file called c.x.z.mcc, where x and z are the chunk's coordinates, instead of the usual position.

Migration and level.dat

[edit | edit source]
How Minecraft looks when converting to the new format.

Beta 1.3 converts any "old" chunks into region files before loading the world, rather than incrementally, as they are loaded during play. As part of the conversion, level.dat is updated with TAG_Int("version") (note case) set to 19132. Beta 1.3 also introduces a new level name field, TAG_String("LevelName"). A new TAG_Byte("Sleeping") was introduced in player TAG_Compounds - level.dat in singleplayer, [player name].dat in multiplayer that indicates whether the player is in a bed. It has value of 1 (true) or 0 (false). In Beta 1.8, TAG_Int("GameType") was added. In 1.0.0, TAG_byte("hardcore") was added.

The format of level.dat is otherwise unchanged.

See also

[edit | edit source]
[edit | edit source]

References

[edit | edit source]

Notes

[edit | edit source]
  1. A total of 1024 chunks can be stored in the format, covering an area of 512×512 blocks.[1]

Software

[edit | edit source]

The community has developed programs to work with region files:

Name Description Screenshot
McRegion This mod optimizes how chunks are stored on the disk, meaning pauses to load or save a chunk as the player moves around a world become much shorter and less noticeable.
Minecraft Region Fixer This tool is a python script that tries to fix problems in region files. It can find some typical errors (corrupted chunks, wrong located chunks, too many entities problems), and can fix these errors in various ways (deleting the chunks, replacing them with a backup copy, or relocating the chunk). This is a command-line application.
MCA2NBT A simple Unix command-line utility to convert a Minecraft .mca region file (in anvil format) to a directory with the same basename containing an uncompressed NBT file for each of its chunks.
Region Scanner A Java Edition 1.7.10 (only) Java command line utility to analyze and mass edit region files
[edit | edit source]