11

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.

Community
  • 1
  • 1
lancew
  • 780
  • 4
  • 22
  • 1
    can you update your question with a clear reproducible or easy to understand example? – Fabrizio Bertoglio Apr 26 '19 at 12:35
  • Done, see edit2. – lancew Apr 26 '19 at 12:45
  • 1
    I refactored your question, but it is still not clear: 1) `schem.value.Data.value[index];` what is the value of the `b` variable and what is the value of `schem` etc... maybe you want to create a [`fiddle`](https://jsfiddle.net/) and stub the values so that we can quickly recreate your scenario. 2) what is the output of `facing = data & 0x03;` and the rest of the code. Try to create some sort of fiddle or explain clearly the code. – Fabrizio Bertoglio Apr 26 '19 at 13:32
  • 2
    @lancew Have you already checked out https://www.npmjs.com/package/mc-schematic which builds upon https://www.npmjs.com/package/minecraft-data ? Looks pretty promising to me, – Fitzi Apr 26 '19 at 14:04
  • @FabrizioBertoglio thank you, I can see that its much easier to read now. I edited further to retain the original purpose. – lancew Apr 26 '19 at 14:28
  • @Fitzi Thank you for your help. I did look through those projects, and just spent some more time going through them. From what I can tell, they don't actually decode the data bits, just provide access to them. Its a lot to look through though, so I could have missed it. – lancew Apr 26 '19 at 14:30
  • *Questions asking us to [recommend or find a tool, software library are off-topic for Stack Overflow as they tend to attract opinionated answers and spam](https://stackoverflow.com/help/on-topic). Instead, describe the problem and what has been done so far to solve it.* I suggest you to update your answer so that the problem is very clear. I am happy to reverse all my edits, but your question went from 4 to 10 upvotes so probably it is better off. The current question is on how to decode one block, you never included in your question the logic to decode multiple blocks. – Fabrizio Bertoglio Apr 26 '19 at 15:46
  • Stackoverflow is not meant for others to solve your problem, but to receive assistance and finding a collective solution. – Fabrizio Bertoglio Apr 26 '19 at 15:47
  • I definitely asked for a solution to decode ALL blocks in the original question. I am fine with the current edits though. My hope was that someone who has done this before would share their solution. – lancew Apr 26 '19 at 15:54

1 Answers1

6

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).

Community
  • 1
  • 1
  • Wait, so for any schematic saved from 1.13 I won’t have to deal with the block state bit decoding? Are the standard schematic plugins currently able to save schematics from 1.13? – lancew Apr 27 '19 at 21:08
  • 1
    I don't know if any schematic plugins are available for 1.13 or if the schematic file format has been updated to reflect, but *if one is available* then it would have to support the new BlockState format (as metadata is 100% gone). – Draco18s no longer trusts SE Apr 28 '19 at 01:16
  • Thank you. Unless anyone proves you wrong in the time limit, this answer will get the bounty. – lancew Apr 28 '19 at 20:14