plugins {
    id 'multiloader-loader'
    id 'net.neoforged.moddev'
}

def commonResources = project(':common').file('src/main/resources/').getAbsolutePath()
def forgeResources = file('src/main/resources/').getAbsolutePath()
def forgeBuildResources = file('build/resources/main/').getAbsolutePath()
def commonBuildResources = project(':common').file('build/resources/main/').getAbsolutePath()
def generatedOutput = project(':common').file('src/generated/resources/').getAbsolutePath()

neoForge {
    version = neoforge_version

    validateAccessTransformers = true

    def at = project(':common').file('src/main/resources/META-INF/accesstransformer.cfg')
    if (at.exists()) {
        accessTransformers = ["src/main/resources/META-INF/accesstransformer.cfg"]
    }
    runs {
        client {
            client()
            devLogin = true
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--existing', forgeResources,
                    '--existing', commonResources,
                    '--existing', forgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        data {
            clientData()

            // 使用之前定义的变量
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--output', generatedOutput,
                    '--existing', forgeResources,
                    '--existing', commonResources,
                    '--existing', forgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        server {
            server()
        }
    }

    mods {
        "${mod_id}" {
            sourceSet sourceSets.main
        }
    }
}

sourceSets.main.resources.srcDir project(':common').file('src/generated/resources')

dependencies {
    implementation(group: 'tschipp.carryon', name: 'carryon-neoforge-26.1.2', version: '3.0.1') {
        transitive = false
    }
    implementation "curse.maven:jade-324717:8068368"

}

// 配置sourceJar任务
tasks.named('sourcesJar', Jar) {
    dependsOn classes
    dependsOn project(':common').tasks.named('sourcesJar')  // 显式依赖common的source
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('sources')
    from sourceSets.main.allSource
    from project(':common').sourceSets.main.allSource
}

// 配置javadoc任务
tasks.named('javadoc', Javadoc) {
    source project(':common').sourceSets.main.allJava
    source sourceSets.main.allJava
    classpath = configurations.compileClasspath
    classpath += project(':common').sourceSets.main.compileClasspath
    options.encoding = 'UTF-8'
    options.charSet = 'UTF-8'
    options.links("https://docs.oracle.com/en/java/javase/21/docs/api/")
    options.memberLevel = JavadocMemberLevel.PUBLIC
    options.addBooleanOption('Xdoclint:none', true)
    options.addStringOption('doctitle', "${mod_id} ${minecraft_version} ${version} Javadoc")
}

// 配置javadocJar任务
tasks.named('javadocJar', Jar) {
    dependsOn javadoc
    dependsOn project(':common').tasks.named('javadoc')  // 显式依赖common的javadoc

    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
    from project(':common').javadoc.destinationDir
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

// 确保build任务包含所有需要的jar
tasks.named('build') {
    dependsOn tasks.named('sourcesJar')
    dependsOn tasks.named('javadocJar')
}

test {
    useJUnitPlatform()
}

// 处理资源
processResources {
    from project(':common').sourceSets.main.resources

    inputs.property "version", project.version
    inputs.property "minecraft_version", minecraft_version
    inputs.property "neoforge_version", neoforge_version
    inputs.property "mod_id", mod_id
    inputs.property "mod_name", mod_name
    inputs.property "description", description
    inputs.property "mod_author", mod_author
    inputs.property "credits", credits

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta', "*.mixins.json"]) {
        expand([
                version: project.version,
                minecraft_version: minecraft_version,
                neoforge_version: neoforge_version,
                mod_id: mod_id,
                mod_name: mod_name,
                description: description,
                mod_author: mod_author,
                "credits": credits
        ])
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
