Skip to content

Instantly share code, notes, and snippets.

@Iunius118
Last active June 30, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Iunius118/989807ba3b6c5b3dfcb3750547951803 to your computer and use it in GitHub Desktop.
Save Iunius118/989807ba3b6c5b3dfcb3750547951803 to your computer and use it in GitHub Desktop.
Output the list of block IDs and quantities from a structure .nbt file.
import sys
# Requires Python-NBT
# pip install Python-NBT
import python_nbt.nbt as nbt
def main():
nbt_file = open_file()
block_list = count_blocks(nbt_file)
output_block_list(block_list)
def open_file():
arg_list = sys.argv
if len(arg_list) < 2:
sys.exit("One argument representing .nbt file path is required.")
return nbt.read_from_nbt_file(arg_list[1])
def count_blocks(nbt_file):
palette = []
for block_type in nbt_file["palette"].value:
palette.append(block_type["Name"].value)
block_list = {}
for block in nbt_file["blocks"]:
block_list[palette[block["state"].value]] = block_list.get(palette[block["state"].value], 0) + 1
return block_list
def output_block_list(block_list):
with open(sys.argv[1] + ".txt", "w") as f:
for k, v in block_list.items():
f.write(k + "," + str(v) + "\n")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment