Building an Command Line Tool to View Minecraft Data


This week's post is about creating an application that can be run from command line to view Minecraft NBT files. You will learn:



Named Binary Tag (NBT), files contain information about Minecraft using a basic binary format. These documents provide more details about the format NBT.



"NBT format" via Minecraft Wiki "NBT" via Wiki.vg "Named Binary Tag specification" via Minecraft.net (WebArchive)



Script source code



view-nbt



Utilize the script to see an NBT-file



I have an NBT file, level.dat. It contains information about the Minecraft level I play on.



To look at the data in a format that is readable I use view-nbt.



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



How the script functions



NBT files may contain tags of a variety of kinds. I used an Enum.Enum Tag to represent them.



To help in parsing, I made several data structures to aid in parsing.



Scalar_fmt is a dictionary which maps some Tag objects to format strings usable by the struct module. This is for tags that are simple such as Tag.Int and Tag.Byte. array_fmt is an array which converts Tag objects into format strings that can be used by the struct module (but lacking a length prefix). This is for simple array tags such as Tag.Int_Array. The dictionary byte_tag maps bytes to the appropriate tags. I learned this from reading the NBT file format specification.



The struct module is used by the parser to decompress binary data into various numbers (like short, double, and int).



The parser is recursive.



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



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



The tag's type. Optional: The tag's name. - The tag value.
Minecraft



To increase the accessibility of the data, I have a function, JSON_friendly, which converts Node objects into data structures which, to my mind, make the JSON easier to read.



I use Python's gzip module to support compressed NBT files.



I use Python's json module to dump the JSON friendly data into strings.



In the end...



This week's blog post will show you how to create a command line tool to view Minecraft NBT files. You learned:



How to unpack binary data using the struct module. How to build a recursive Parser for NBT file format. How do you convert the parsed data into an JSON friendly format for files.



My challenge to you is:



Create an application called view-region which converts Region files into JSON that is readable.



If you liked this week's blog, please share it with your friends and keep an eye out for the next post. We'll see you next week!


Created: 12/09/2022 08:45:13
Page views: 83
CREATE NEW PAGE