def main(world_path: str, check: bool = False): total_found = 0 for world in WORLDS.keys(): print(f"Checking the {world}") world_folder = AnvilWorldFolder(path.join(world_path, WORLDS[world])) regions = world_folder.regionfiles if len(regions) == 0: print(f"Couldn't find region files for the {world}, skipping") continue with Bar("Checking Regions", fill="█", max=len(regions)) as bar: for region_coords in regions.keys(): region = RegionFile(regions[region_coords]) chunks = region.get_metadata() for chunk in chunks: chunk_x = region_coords[0] * 32 + chunk.x chunk_z = region_coords[1] * 32 + chunk.z nbt = world_folder.get_nbt(chunk_x, chunk_z) found_errors = False entities = nbt["Level"]["TileEntities"] for entity in entities: if not in_chunk(chunk_x, chunk_z, entity['x'], entity['z']): total_found += 1 found_errors = True # Move the entity to the (hopefully) right coordinates entity["x"].value = chunk_x * 16 + ( to_int(entity['x']) % 16) entity["z"].value = chunk_z * 16 + ( to_int(entity['z']) % 16) if found_errors and not check: region.write_chunk(chunk.x, chunk.z, nbt) bar.next() print( f"{ 'Found' if check else 'Fixed'} {total_found} entities with wrong coordinates" )
def process_region(reg, callback): reg_nbt = RegionFile(reg) for m in reg_nbt.get_metadata(): chunk = reg_nbt.get_chunk(m.x, m.z) level = chunk['Level'] tile_entities = level['TileEntities'] chunk_needs_update = False for ent in tile_entities: if ent['id'].value == 'minecraft:command_block': if callback(ent): chunk_needs_update = True if chunk_needs_update: reg_nbt.write_chunk(m.x, m.z, chunk) reg_nbt.close()