Skip to main content

Named Binary Tag Reader/Writer

Project description

Forewords

This is mainly a Named Binary Tag parser & writer library.

From the initial specification by Markus Persson:

NBT (Named Binary Tag) is a tag based binary format designed to carry large
amounts of binary data with smaller amounts of additional data.
An NBT file consists of a single GZIPped Named Tag of type TAG_Compound.

Current specification is on the official [Minecraft Wiki](https://minecraft.gamepedia.com/NBT_format).

This library is very suited to inspect & edit the Minecraft data files. Provided examples demonstrate how to: - get player and world statistics, - list mobs, chest contents, biomes, - draw a simple world map, - etc.

world.png

Note: Examples are just here to help using and testing the library. Developing Minecraft tools is out of the scope of this project.

Status

The library supports all the currently known tag types (including the arrays of ‘Integer’ and ‘Long’), and the examples work with the McRegion, pre-“flattened” and “flattened” Anvil formats.

Last update was tested on Minecraft version 1.13.2.

Dependencies

The library, the tests and the examples are only using the Python core library, except curl for downloading some test reference data and PIL (Python Imaging Library) for the map example.

Supported Python releases: 2.7, 3.4 to 3.7

Usage

Reading files

The easiest way to read an nbt file is to instantiate an NBTFile object e.g.:

>>> from nbt import nbt
>>> nbtfile = nbt.NBTFile("bigtest.nbt",'rb')
>>> nbtfile.name
u'Level'
>>> nbtfile["nested compound test"].tag_info()
TAG_Compound("nested compound test"): 2 Entries
>>> for tag in nbtfile["nested compound test"]["ham"].tags:
...     print(tag.tag_info())
...
TAG_String("name"): Hampus
TAG_Float("value"): 0.75
>>> [tag.value for tag in nbtfile["listTest (long)"].value]
[11, 12, 13, 14, 15]

Files can also be read from a fileobj (file-like object that contains a compressed stream) or a buffer (file-like object that contains an uncompressed stream of NBT Tags) which can be accomplished thusly:

>>> from nbt.nbt import *
>>> nbtfile = NBTFile(fileobj=previously_opened_file)
# or....
>>> nbtfile = NBTFile(buffer=net_socket.makefile())

Writing files

Writing files is easy too! if you have a NBTFile object, simply call it’s write_file() method. If the NBTFile was instantiated with a filename, then write_file needs no extra arguments. It just works. If however you created a new file object from scratch (or even if you just want to save it somewhere else) call write_file(‘pathtonewfile.nbt’):

>>> from nbt import nbt
>>> nbtfile = nbt.NBTFile("bigtest.nbt",'rb')
>>> nbtfile["listTest (compound)"].tags[0]["name"].value = "Different name"
>>> nbtfile.write_file("newnbtfile.nbt")

It is also possible to write to a buffer or fileobj using the same keyword args:

>>> nbtfile.write_file(fileobj = my_file) #compressed
>>> nbtfile.write_file(buffer = sock.makefile()) #uncompressed

Creating files

Creating files is trickier but ultimately should give you no issue, as long as you have read the NBT spec (hint.. it’s very short). Also be sure to note that the NBTFile object is actually a TAG_Compound with some wrapper features, so you can use all the standard tag features:

>>> from nbt.nbt import *
>>> nbtfile = NBTFile()

First, don’t forget to name the top level tag:

>>> nbtfile.name = "My Top Level Tag"
>>> nbtfile.tags.append(TAG_Float(name="My Float Name", value=3.152987593947))
>>> mylist = TAG_List(name="TestList", type=TAG_Long) #type needs to be pre-declared!
>>> mylist.tags.append(TAG_Long(100))
>>> mylist.tags.extend([TAG_Long(120),TAG_Long(320),TAG_Long(19)])
>>> nbtfile.tags.append(mylist)
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
    TAG_Float("My Float Name"): 3.15298759395
    TAG_List("TestList"): 4 entries of type TAG_Long
    {
        TAG_Long: 100
        TAG_Long: 120
        TAG_Long: 320
        TAG_Long: 19
    }
}
>>> nbtfile["TestList"].tags.sort(key = lambda tag: tag.value)
>>> print(nbtfile.pretty_tree())
TAG_Compound("My Top Level Tag"): 2 Entries
{
    TAG_Float("My FloatName"): 3.15298759395
    TAG_List("TestList"): 4 entries of type TAG_Long
    {
        TAG_Long: 19
        TAG_Long: 100
        TAG_Long: 120
        TAG_Long: 320
     }
}
>>> nbtfile.write_file("mynbt.dat")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nbt-legacy-1.5.1.tar.gz (125.4 kB view details)

Uploaded Source

Built Distribution

nbt_legacy-1.5.1-py3-none-any.whl (25.7 kB view details)

Uploaded Python 3

File details

Details for the file nbt-legacy-1.5.1.tar.gz.

File metadata

  • Download URL: nbt-legacy-1.5.1.tar.gz
  • Upload date:
  • Size: 125.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.2

File hashes

Hashes for nbt-legacy-1.5.1.tar.gz
Algorithm Hash digest
SHA256 dba35d6c756480181560e3fdb7b16fde779fe6dcff3f0aa870c37b55d9e15686
MD5 52d8f2a3a2bb3bef9fcfc706b1588ff1
BLAKE2b-256 2d53f9436cffbe48736dd6a47310a4c54eb4fb74df31e2eb70b5d5bde4c0ee19

See more details on using hashes here.

File details

Details for the file nbt_legacy-1.5.1-py3-none-any.whl.

File metadata

  • Download URL: nbt_legacy-1.5.1-py3-none-any.whl
  • Upload date:
  • Size: 25.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.2

File hashes

Hashes for nbt_legacy-1.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9ab6dabb9d2ef575e99e1573bbbb679544a55c2e9ceae34bc954e6307c921f16
MD5 0e7071fc118c875e684ea9fe07632024
BLAKE2b-256 4f8e8c6771f73ac1faf13b5fe6feb9938538f1957be9188371f99a8ec2702f1e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page