DELETEME This page AND format is deprecated. It is advised to use the Anvil format in MCServer instead of the PAK format

WARNING: This page is incorrect for the latest version of MCServer

====== PAK Data Format ======
The PAK format is highly compact format that has a slightly better compression ratio and saving / loading times than the [[api:dataformatanvil | Anvil]] format. 

===== Versions =====
PAK files support two-level versioning. File versino data specifies the general layout of the file, and hasn't changed yet from the original version. Chunk-format version specifies the exact format in which separate chunks are stored in the file. Due to changes in Minecraft itself the chunk-format is already at its 3rd version, with MCServer being able to read in any version and write the latest version supported. Older versions are converted to newest version when the first chunk is read  or written from / to them.

===== High-level format =====
The file starts with a 1-byte file version data, followed by 1-byte chunk-format version data. Currently the file version is 1 and chunk-format version is 3.
The next two bytes specify the number of chunks saved in the file

===== Chunks =====
Chunks contain block and [[api:cBlockEntity | Block Entity]] (chests/furnaces/signs) data.

==== Block Data (v1) ====
Block data is stored as raw data, in the same format it is sent across the network and is documented in the [[http://mc.kev009.com/Protocol#Map_Chunk_.280x33.29 | Network protocol ]]
It consists out of four sequential sections, in order:
  *Block ID array (1 byte per block)
  *Block MetaData array (half byte / nibble per block)
  *Block Light array (half byte / nibble per block)
  *Sky Light array (half byte / nibble per block)

Chunks are always 16 * 128 * 16 blocks in size. That is:
  *X: 16
  *Y: 128
  *Z: 16

So first you have 16 * 128 * 16 bytes for the block IDs and then three times 16 * 128 * 16 half-bytes for the MetaData, Block Light, and Sky Light in order.

^ Size      ^ Data ^ Type ^ Example ^
| 32768 Bytes | Block Types | Byte Array | ... |
| 16384 Bytes | Block Meta Data | Nibble Array  | ... |
| 16384 Bytes | Block Light | Nibble Array  | ... |
| 16384 Bytes | Block Sky Light | Nibble Array  | ... |
| ? Bytes | [[api:cBlockEntity]] Array | [[api:cBlockEntity]] Array | Look below |

==== Block Entities ====
After the Block Data come the [[api:cBlockEntity | Block Entities]], they are chests, furnaces and signs.

^ Size      ^ Data ^ Type ^ Example ^
| 1 Byte | BlockID | Byte | 61 ([[api:cfurnaceentity | Furnace]]) |
| ? Bytes | Block Entity data | Data | [[api:cfurnaceentity#Data_Format | Furnace example]]|

As you can see, first comes an identifier byte, to define what kind of [[api:cBlockEntity | Block Entity]] it is, and then comes specifically sized data for each type of [[api:cBlockEntity | Block Entity]]

There are three kinds of Block Entities that are stored in chunks, how they are stored is described on their pages:
  * [[api:cchestentity]]
  * [[api:cfurnaceentity ]]
  * [[api:csignentity]]
====== Coordinates to indices ======
I use this function to convert block coordinates to indices which I use to access the arrays of blocks
<code cpp>
unsigned int cChunk::MakeIndex(int x, int y, int z )
{
	if( x < 16 && x > -1 && y < 128 && y > -1 && z < 16 && z > -1 )
		return y + (z * 128) + (x * 128 * 16);
	return 0;
}
</code>