def Read(type: str, file_name): print("Working....") # For Multiple files, create a list of JSON data elements to parse through later datas = [] if type == "multiple": file_names = file_name.split(" ") file_name = "Grouped" for x in file_names: data = nbt.read_from_nbt_file(f"{x}.nbt") datas.append(nbt.NBTTagBase.json_obj(data, full_json=True)) else: # For Single files, do the same as Multiple, but only fill it with one element data = nbt.read_from_nbt_file(f"{file_name}.nbt") datas.append(nbt.NBTTagBase.json_obj(data, full_json=True)) Parse(file_name, datas)
def __init__(self, worldName): self.worldName = worldName userName = str(subprocess.check_output('whoami', shell=True)).split('\\')[-3] self.absPath = f'C:/Users/{userName}/AppData/Roaming/.minecraft/saves/{worldName}' # get absolute path of world to be modified self.worldLeveldat = nbt.read_from_nbt_file( f'{self.absPath}/level.dat') self.gameType = 0 self.spawnCoords = 0
def move_existing_remote_player_to_local_player(self): world = LeveldatWorldInfo(self.worldName) # get remoter player's data remotePlayerData = nbt.read_from_nbt_file(self.absPlayerPath) # overwrite local player's data with remote player's world.worldLeveldat['Data']['Player'] = remotePlayerData nbt.write_to_nbt_file(f'{self.absWorldPath}/level.dat', world.worldLeveldat)
class ReadFileTest(unittest.TestCase): tag = nbt.read_from_nbt_file('test/test_read.nbt') def test_byte(self): self.assertEqual(self.tag['tag_type'].value, 10, 'Byte test') def test_short(self): self.assertEqual(self.tag['nbt_version'].value, 19133) def test_int(self): self.assertEqual(self.tag['int_tag'].value, 1048576) def test_long(self): self.assertEqual(self.tag['long_tag'].value, 1510346461649) def test_float(self): self.assertTrue(approx(self.tag['float_tag'].value, 2.71828, 0.000005)) def test_double(self): self.assertTrue( approx(self.tag['pi'].value, 3.141592653589746, 0.0000000000000005)) def test_bytearray(self): self.assertListEqual(self.tag['bytes_helloworld'].value, list(b'Hello, world!')) def test_intarray(self): self.assertListEqual(self.tag['int_array'].value, [1048576, 2097152, 4194304, 8388608, 16777216]) def test_longarray(self): self.assertListEqual( self.tag['long_array'].value, [1111111111111111111, 2222222222222222222, 3333333333333333333]) def test_taglist(self): self.assertListEqual( self.tag['description'].value, list( map(nbt.NBTTagString, ['Python-NBT test', 'Created on 2019/12/09']))) def test_compound(self): self.assertEqual(self.tag['test_version']['major'].value, 0) self.assertEqual(self.tag['test_version']['minor'].value, 0) self.assertEqual(self.tag['test_version']['build'].value, 1) def test_nesting(self): nested = self.tag['multi_nesting'] for i in range(1, 5): nested = nested['layer' + str(i)] self.assertEqual(nested['yay'].value, 'yay!')
class JsonTest(unittest.TestCase): tag = nbt.read_from_nbt_file('test/test_read.nbt') json_path = 'test/test_json.json' def test1_to_json(self): with open(self.json_path, 'w', encoding='utf-8') as file: json.dump(self.tag.json_obj(), file, indent=2) def test2_from_json(self): with open(self.json_path) as file: _tag = nbt.from_json(json.load(file)) self.assertEqual(self.tag, _tag) def test3_to_simple_json(self): self.assertDictEqual( self.tag.json_obj(full_json=False), { "long_array": [ 1111111111111111111, 2222222222222222222, 3333333333333333333 ], "nbt_version": 19133, "description": ["Python-NBT test", "Created on 2019/12/09"], "pi": 3.141592653589746, "int_array": [1048576, 2097152, 4194304, 8388608, 16777216], "test_version": { "major": 0, "minor": 0, "build": 1 }, "bytes_helloworld": [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], "int_tag": 1048576, "tag_type": 10, "long_tag": 1510346461649, "float_tag": 2.718280076980591, "multi_nesting": { "layer1": { "layer2": { "layer3": { "layer4": { "yay": "yay!" } } } } } })
def setup_template_for_player(self): playerTemplate = nbt.read_from_nbt_file('GamerJuice.dat') world = LeveldatWorldInfo(self.worldName) world.get_default_gamershit() playerTemplate['playerGameType'] = world.gameType playerTemplate['Pos'] = world.spawnCoords playerTemplate['UUID'] = nbt.TAG_Int_Array(self.playerUUIDArray) world.worldLeveldat['Data']['Player'] = playerTemplate nbt.write_to_nbt_file(f'{self.absWorldPath}/level.dat', world.worldLeveldat) nbt.write_to_nbt_file(f'{self.absWorldPath}/level.dat_old', world.worldLeveldat)
def add_dir(path): for directory in os.listdir(path): if directory not in {'chapter.nbt', 'index.nbt'}: if os.path.isdir(os.path.join(path, directory)): add_dir(os.path.join(path, directory)) else: file = nbt.read_from_nbt_file(os.path.join(path, directory)) if 'tasks' in file.keys(): for task in file.get('tasks'): id_list.add(gen_id(task['uid'].value)) if 'rewards' in file.keys(): for reward in file.get('rewards'): id_list.add(gen_id(reward['uid'].value)) directory = os.path.splitext(directory)[0] id_list.add(directory)
def add_dir(path): for directory in os.listdir(path): if directory not in {'chapter.nbt', 'index.nbt'}: if os.path.isdir(os.path.join(path, directory)): add_dir(os.path.join(path, directory)) else: file = nbt.read_from_nbt_file(os.path.join(path, directory)) if 'tasks' in file.keys(): for task in file.get('tasks'): id_list.add(gen_id(task['uid'].value)) if 'rewards' in file.keys(): for reward in file.get('rewards'): id_list.add(gen_id(reward['uid'].value)) directory = os.path.splitext(directory)[0] if directory not in id_list: id_list.add(directory) else: print("Collision detected! id: %s, %d" % (directory, JavaInteger.to_signed(directory, base=16))) return
import python_nbt.nbt as nbt file = nbt.read_from_nbt_file("r.0.0.mca") print(file["TileEntities"])
def test_write(self): nbt.write_to_nbt_file('test/test_write.nbt', self.tag) read_tag = nbt.read_from_nbt_file('test/test_write.nbt') self.assertEqual(read_tag, self.tag)
continue if isinstance(nbt_list, nbt.NBTTagList) or isinstance( entry, nbt.NBTTagCompound): traverse_dicts(entry) elif isinstance(entry, nbt.NBTTagString): string_replacer(entry) for (subdir, dirs, files) in os.walk("toconvert", topdown=True): for file in files: directory = subdir + os.sep filepath = directory + file if filepath.endswith(".nbt"): nbtfile = nbt.read_from_nbt_file(filepath) traverse_dicts(nbtfile) directory = directory.replace("toconvert", "converted").replace( originalBiome, newBiome) Path(directory).mkdir(parents=True, exist_ok=True) newFile = directory + file.replace(originalBiome, newBiome) nbt.write_to_nbt_file(newFile, nbtfile) continue else: continue for x in blockPalette: print(x)
import python_nbt.nbt as nbt import json file = nbt.read_from_nbt_file("./test/file.nbt") print(json.dumps(file.json_obj, indent=2))
import python_nbt.nbt as nbt from python_nbt import JavaInteger chapters = [ "61404d4e", "a2d67590", "b2a6a21a", "9a4d850c", "f764b037", "92df22db", "2ccc878e" ] file = nbt.read_from_nbt_file("./config/ftbquests/normal/chapters/index.nbt") print(hex(JavaInteger.number_bits)) print(file['index']) [ file['index'].append(JavaInteger.to_signed(idx, base=16)) for idx in chapters if idx not in file['index'] ] print(file['index']) nbt.write_to_nbt_file("./.workspace/nbt/index.nbt", file)