import os
import json

def format_identifier(identifier, category):
    # Formateamos el identificador según la categoría (block o item)
    if category == "block":
        parts = identifier.split(":")
        if len(parts) == 2:
            formatted_identifier = parts[1].replace("_", " ").title()
            return f"tile.{identifier}.name={formatted_identifier} | §9BetterEnd"
    else:
        parts = identifier.split(":")
        if len(parts) == 2:
            formatted_identifier = parts[1].replace("_", " ").title()
            return f"item.{identifier}={formatted_identifier} | §9BetterEnd"
    return ""

def extract_and_format_identifiers(directory):
    identifiers = []
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith(".json"):
                file_path = os.path.join(root, file)
                with open(file_path, 'r', encoding='utf-8') as json_file:
                    try:
                        data = json.load(json_file)
                        identifier = data.get("minecraft:block", {}).get("description", {}).get("identifier") or \
                                    data.get("minecraft:item", {}).get("description", {}).get("identifier")
                        if identifier:
                            category = "block" if "minecraft:block" in data else "item"
                            formatted_identifier = format_identifier(identifier, category)
                            if formatted_identifier:
                                identifiers.append(formatted_identifier)
                    except json.JSONDecodeError:
                        print(f"Error parsing JSON in file {file_path}")
    return identifiers

def write_identifiers_to_file(identifiers, output_file):
    with open(output_file, 'w', encoding='utf-8') as file:
        for identifier in identifiers:
            file.write(identifier + '\n')

if __name__ == "__main__":
    directory_path = "."  # Reemplaza con la ruta de tu directorio
    output_file_path = "identifiers.txt"  # Ruta del archivo de salida
    extracted_and_formatted_identifiers = extract_and_format_identifiers(directory_path)
    write_identifiers_to_file(extracted_and_formatted_identifiers, output_file_path)
