Last active
August 5, 2016 03:01
-
-
Save firelizzard18/6b2a81630dd1dbfb528902e1b1bf852b to your computer and use it in GitHub Desktop.
Dead simple command line NBT editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Name: mc-nbt | |
# Version: 0.1 | |
# Description: Views or edits a Minecraft NBT file | |
# Author: Ethan Reesor <firelizzard@gmail.com> | |
# License: DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE <http://www.wtfpl.net/> | |
# Examples: | |
# Print entire file: | |
# mc-nbt FILE | |
# Print part of the tree: | |
# mc-nbt FILE --path Pos | |
# Set a tag's value (element of a list): | |
# mc-nbt FILE --path Pos.0 1024.0 | |
# Set a list tag's elements' values to elements of a list: | |
# mc-nbt FILE --path Pos [1,2,3] | |
from nbt import nbt | |
import argparse | |
def main(): | |
parser = argparse.ArgumentParser(description='Manage an NBT file') | |
parser.add_argument(dest='file', type=lambda x: nbt.NBTFile(x, 'rb')) | |
parser.add_argument('--path', type=lambda x: x.split('.'), default=[]) | |
parser.add_argument('--value', type=eval) | |
args = parser.parse_args() | |
last = None | |
item = args.file | |
if args.path: | |
for name in args.path: | |
last = item | |
if type(item) is nbt.TAG_List: | |
item = item[int(name)] | |
else: | |
item = item[name] | |
if args.value is None: | |
print(item.pretty_tree()) | |
elif not last: | |
raise ValueException('Cannot specify --value without --path') | |
elif type(item) is nbt.TAG_List and type(args.value) is list: | |
for elem, value in zip(item, args.value): | |
elem.value = value | |
args.file.write_file() | |
else: | |
item.value = args.value | |
args.file.write_file() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment