import os
import anvil
from anvil.errors import ChunkNotFound
from tqdm import tqdm

WORLD_PATH = r"" #here lied the path to my save folder
BLOCK = "loom"
PATH = os.path.join(WORLD_PATH, "region")

def scan_overworld():
    if not os.path.exists(PATH):
        print(f"Region directory not found at {PATH}")
        return

    mca_files = [f for f in os.listdir(PATH) if f.endswith(".mca")]
    
    for file in tqdm(mca_files, desc="Scanning Regions", unit="region"):
        region_path = os.path.join(PATH, file)
        region = anvil.Region.from_file(region_path)
        for cx in range(32):
            for cz in range(32):
                if os.path.getsize(region_path) < 8192: # region file is too small => it's a filler file that doesn't contain info. Skip
                    continue
                    
                off, length = region.chunk_location(cx, cz)
                if off == 0:
                    continue  # Chunk doesn't exist, skip    
                chunk = anvil.Chunk.from_region(region, cx, cz)
                for te in chunk.tile_entities:  
                    # Direct check (placed block)
                    if BLOCK in te["id"]:
                        print(f"\n[LOOM FOUND] Placed @ {te['x'].value}, {te['y'].value}, {te['z'].value}")
                    

if __name__ == "__main__":
    scan_overworld()
    print("\nScan complete.")