最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

I am parsing a schematic file with the following structure

The .schematic file format was created by the munity 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:

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.

I am parsing a schematic file with the following structure

The .schematic file format was created by the munity 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:

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.

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Apr 23, 2019 at 17:46 lancewlancew 7704 silver badges23 bronze badges 9
  • 1 can you update your question with a clear reproducible or easy to understand example? – fabOnReact Commented Apr 26, 2019 at 12:35
  • Done, see edit2. – lancew Commented Apr 26, 2019 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 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. – fabOnReact Commented Apr 26, 2019 at 13:32
  • 2 @lancew Have you already checked out npmjs./package/mc-schematic which builds upon npmjs./package/minecraft-data ? Looks pretty promising to me, – Fitzi Commented Apr 26, 2019 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 Commented Apr 26, 2019 at 14:28
 |  Show 4 more ments

1 Answer 1

Reset to default 6 +100

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

发布评论

评论列表(0)

  1. 暂无评论