This week's post will focus on creating an command line tool to view Minecraft NBT files. You will learn:
Named Binary Tag (NBT), files contain information about Minecraft using a simple binary format. To find out more specifics of the NBT format look up these documents:
"NBT format" via Minecraft Wiki. "NBT" via Minecraft.vg. "Named Binary Tag specification" via Minecraft.net (WebArchive).
Script source code
view-nbt
Use the script to look at an NBT-file
I have a NBT file, level.dat. It contains details about the Minecraft level I play on.
I use view-nbt for viewing the data in a readable format:
(level.dat files can be compressed, so I use --gzip.
How the script works
NBT files contain tags of many different kinds so I chose to use an enum.Enum Tag, to represent the tags.
To help in parsing, I made various data structures that can be used as auxiliary data.
Scalar_fmt can be used map certain Tag objects to form strings that are usable by struct module. This is meant to be used with "simple" tags such as Tag.Int or Tag.Byte. array_fmt is a dictionary which converts Tag objects to format strings that can be used by the struct module (but, missing the length prefix). This is for simple array tags like Tag.Int_Array. The dictionary byte_tag assigns bytes to the appropriate tags. I learned this from reading the NBT specification for file formats.
The struct module is utilized by the parser to decompress binary data into different numbers (like short double, short, and int).
The parser is recursive.
It directly parses Tag.String in addition to tags in array_fmt and scalar_fmt. It parses recursively Tag.List and Tag.Compound.
The parsing creates a Node object, which is a collections.namedtuple that holds:
The tag type. The tag's name (optional). - The tag value.
To make it easier to read the data, I developed an application called json_friendly which converts Node objects into data structures.
I use Python's gzip module to support compressed NBT files.
To convert the JSON-friendly information into strings, I use Python's Python's json function.
Let's conclude...
In this week's blog, you learned how to build an application that runs on command line to open Minecraft NBT files. You learned:
How to decode binary data using the struct module. How to create an recursive Parser to support NBT file format. How can you convert the parsed data into JSON-friendly file format
My challenge to you is:
Create a tool, view-region which converts Region files into readable JSON.
If you enjoyed this week’s post I invite you to share it with your friends. The next week's post will be available. We'll see you next week!
Persiancatrescue