import os

template_file = r"D:\Minrcraft Run1\versions\26.1.2-Fabric\resourcepacks\Featured 1.5\assets\minecraft\variants-cit\modules\items\apple2.json"
list_file = r"D:\Minrcraft Run1\versions\26.1.2-Fabric\resourcepacks\Featured 1.5\assets\minecraft\variants-cit\modules\items\file.txt"

# 目标目录 = 模板所在目录
target_dir = os.path.dirname(template_file)

print("=== 脚本开始运行 ===")
print(f"[信息] 目标目录: {target_dir}")

# 检查文件
if not os.path.exists(template_file):
    print(f"[错误] 找不到模板文件: {template_file}")
    input("按回车退出...")
    exit()

if not os.path.exists(list_file):
    print(f"[错误] 找不到列表文件: {list_file}")
    input("按回车退出...")
    exit()

# 读取模板
with open(template_file, "r", encoding="utf-8") as f:
    template_content = f.read()

# 读取列表
with open(list_file, "r", encoding="utf-8") as f:
    names = [line.strip() for line in f if line.strip()]

print(f"[信息] 读取到 {len(names)} 个文件名")

for name in names:
    try:
        base_name = name.replace(".json", "")
        target_path = os.path.join(target_dir, name)

        print(f"\n[处理] {name}")

        content = template_content
        content = content.replace("apple2", "__TEMP__")
        content = content.replace("apple", base_name)
        content = content.replace("__TEMP__", base_name + "2")

        # 写入文件
        with open(target_path, "w", encoding="utf-8") as file:
            file.write(content)

        print(f"[成功] 已生成: {name}")
        print(f"[成功] 替换: apple → {base_name}, apple2 → {base_name}2")

    except Exception as e:
        print(f"[错误] 处理 {name} 时出错: {e}")

print("\n=== 全部完成 ===")
input("按回车键退出...")