twoolie / NBT

Python Parser/Writer for the NBT file format, and it's container the RegionFile.
MIT License
361 stars 74 forks source link

Writing a compound tag to a file #163

Open Matthias1590 opened 2 years ago

Matthias1590 commented 2 years ago

Hey there, I've been stuck at what should be a simple thing to do for a while now, I can't manage to write a compound tag to a file. Following the examples on PyPI didn't help much either as they don't write existing tags to files but rather create a new tag (using the nbt.NBTFile class) and write that. Am I missing something or is this just not supported? And if it's not supported, how should I go about doing this?

twoolie commented 1 year ago

Hi @Matthias1590, Sorry to take so long getting back to you.

Essentially, NBTFile subclasses TAG_Compound, so unfortunately you can't just write any arbitrary compound tag at the root of a file.

A workaround would be to create an NBTFile and copy all the root elements into that NBTFile.

tag_compound = nbt.TAG_Compound(...)

nbt_file = nbt.NBTFile()
nbt_file.name = tag_compound.name
nbt_file.tags = tag_compound.tags
nbt_file.write_file("filename.nbt")