The Creation of a Command Line Tool that can View Minecraft Data



This week's blog post is about creating an command line tool to view Minecraft NBT files. This is what you'll learn:



Named Binary Tag (NBT) files store information about Minecraft in a simple binary file format. These documents offer more details about the NBT format.



"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 view an NBT-file



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



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



(level.dat files are compressed, which is why I use the --gzip flag.)



How the script functions



NBT files are tagged with many different kinds so I chose to use an enum.Enum Tag, to represent the tags.



To aid in the parsing process, I came up with several data structures as auxiliary data:



Scalar_fmt is a dictionary which converts Tag objects to format strings that can be used by the struct module. This is meant to be used with "simple" tags such as 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 without a length prefix). This is for simple array tags such as Tag.Int_Array. The dictionary byte_tag maps bytes to the appropriate tags. This information was drawn from the NBT file format specification.



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



The parser is recursive.



It directly parses Tag.String in addition to tags in array_fmt and scalar_fmt. It parses Tag.List, Tag.Compound recursively.



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



The tag's type. Optional: The tag name. The value of the tag.



To make it easier to read data, I designed an algorithm called json_friendly that converts Node objects into data structures.



To assist with NBT files compressed by Python's Gzip module, I employ it.



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



Let's conclude...



This week's article will show you how to develop a command-line tool that can examine Minecraft NBT files. What you learned:



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



My challenge to you is:



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



If you liked this week's post Please share it with your friends.https://minecraft-server-list.info/ The next post for the week will be available. We'll see you next week!