import os
import json

# Set your recipe directory path
recipe_dir = r"C:\Users\nekoz\AppData\Roaming\PrismLauncher\instances\Resurvival7.2\minecraft\global_packs\required_data\Resurvival Retooling Mod\data\valhelsia_structures\recipes"

def reformat_recipe(data):
    if data.get("type") == "valhelsia_structures:axe_crafting" and "input" in data and "output" in data:
        return {
            "type": "minecraft:crafting_shaped",
            "key": {
                "X": {
                    "item": data["input"]["item"]
                }
            },
            "pattern": [
                "X",
                "X"
            ],
            "result": {
                "item": data["output"]["item"],
                "count": data["output"].get("count", 1)
            }
        }
    return data

# Walk through directory and reformat each JSON file
for root, _, files in os.walk(recipe_dir):
    for filename in files:
        if filename.endswith(".json"):
            file_path = os.path.join(root, filename)

            try:
                with open(file_path, 'r', encoding='utf-8') as f:
                    data = json.load(f)

                reformatted = reformat_recipe(data)

                # Overwrite the original file
                with open(file_path, 'w', encoding='utf-8') as f:
                    json.dump(reformatted, f, indent=2)
                    
                print(f"Reformatted: {file_path}")

            except Exception as e:
                print(f"Error processing {file_path}: {e}")