Building a Command Line Tool that can View Minecraft Data

Building a Command Line Tool that can View Minecraft Data


This week's post is about building a command line tool to view Minecraft NBT files. What you will learn:

Named Binary Tag (NBT), files contain details about Minecraft using a basic binary file format. To find out more specifics of the NBT format, go through 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 an NBT file, level.dat, that contains details about the Minecraft level that I play on:

To view the data in a readable format I use view-nbt.

(level.dat files can be compressed, so I use --gzip.

How the script functions

NBT files can contain tags of various types. I used an Enum.Enum, Tag , to represent them.

To aid in the parsing, I made various data structures that can be used as auxiliary data.

Scalar_fmt is an fmt dictionary that maps some Tag objects to format strings usable by the struct module. This is designed to be used with "simple" tags, like Tag.Int or Tag.Byte. array_fmt is an array that maps some Tag objects into format strings that can be used by the struct module (but missing a length prefix). This is for simple array tags, such as Tag.Int_Array. Editum The dictionary byte tag assigns bytes to appropriate tags. I learned this from reading the NBT file format specification.

The parser uses the struct module to unpack binary data into different numbers (like int, double, short).

The parser is recursive.

It directly parses Tag.String as well as the tags in array_fmt or scalar_fmt. It parses Tag.List, Tag.Compound recursively.

The parsing creates a Node object, which is a collections.namedtuple that holds:

The tag type. - The tag name (optional). - The tag value.

To increase the readability of the data I have a function, the json_friendly function, which transforms Node objects into data structures which, to my mind help make the JSON easier to read.

I utilize Python's Gzip module to support compressed NBT files.

To convert the JSON-friendly information into strings I use Python's json function.

In the final...

In this week's article, you learned how to create a command-line tool to view Minecraft NBT files. You learned:

How to decompress binary data using the struct module. How to write a recursive parser that works with the NBT file format. How do you convert the parsed data into an JSON friendly file format.

My challenge to you is:

Create a tool, view-region, that turns Region files into JSON files that can be read by.

If you enjoyed this week’s post I invite you to share it with your acquaintances. The next post for the week will be available. We'll see you next week!

Report Page