Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode Data (ie block state) bytes in Minecraft schematic (nbt) file?

I am parsing a schematic file with the following structure

The .schematic file format was created by the community to store sections of a Minecraft world for use with third-party programs. Schematics are in NBT format

The Named Binary Tag (NBT) file format is an extremely simple structured binary format used by the Minecraft game for a variety of things

Block Data Values define parts of the terrain in Minecraft.

I retrieving the block data of every Minecraft Block, and need to figure out how to decode these bytes. This is an example for the Stairs Minecraft Block

For example the stairs block data includes:

enter image description here

I can use nbt-js to parse the entire schematic file, which enables me to access the block data like this:

var b = schem.value.Data.value[index];

I decode the Stairs Block Data bits data with the following code

var facing = b & 0x03;
var half = (b >> 2) & 0x01;
var shape = (b >> 3) & 0x03;

These configuration values are essential to determine how the stair block should be rendered. For example, I use the facing value to rotate the block:

block.rotateX(facing);

However, the bits are interpreted differently for every block type, and this isn't defined anywhere that I can find.

like image 490
lancew Avatar asked Apr 23 '19 17:04

lancew


1 Answers

There does not exist a mapping that works for all blocks

And you'll just have to deal with it

This is the entire reason why 1.13 and The Flattening is removing metadata entirely resulting in all blockstates encoded as strings when serialized (NBT is a serialized data format and the one used for just about everything before reaching the Anvil format). At runtime these states are parsed and turned into true Object instances, obviating the need for magic values.

So you won't have to work out that facing = b & 0x03; you'll instead get {"facing":"east"}

Unfortunately, if you're working below 1.13, you will have to deal with metadata magic values and there is no solution unless you have runtime access to the game and can call getStateFromMeta() (1.10 through 1.12; unsure where 1.8 and 1.9 sit, as I never modded for those versions).

like image 160
Draco18s no longer trusts SE Avatar answered Nov 16 '22 04:11

Draco18s no longer trusts SE