plugins {
    id 'multiloader-loader'
    id 'net.fabricmc.fabric-loom'
}

dependencies {
    minecraft "com.mojang:minecraft:${minecraft_version}"
    implementation "net.fabricmc:fabric-loader:${fabric_loader_version}"
    implementation "net.fabricmc.fabric-api:fabric-api:${fabric_version}"
    implementation "curse.maven:jade-324717:8048381"
    testImplementation "net.fabricmc:fabric-loader-junit:${fabric_loader_version}"
    localRuntime 'net.covers1624:DevLogin:0.1.0.5'
}

loom {
    def commonResources = project(':common').file('src/main/resources/').getAbsolutePath().toString()
    def fabricResources = file('src/main/resources/').getAbsolutePath().toString()
    def fabricBuildResources = file('build/resources/main/').getAbsolutePath().toString()
    def commonBuildResources = project(':common').file('build/resources/main/').getAbsolutePath().toString()
    def generatedOutput = project(':common').file('src/generated/resources/').getAbsolutePath().toString()

    if (project(":common").file("src/main/resources/${mod_id}.accesswidener").exists()) {
        accessWidenerPath.set(project(":common").file("src/main/resources/${mod_id}.accesswidener"))
    }
    mixin {
        defaultRefmapName.set("${mod_id}.fabric.refmap.json")
    }
    runs {
        client {
            client()
            ideConfigGenerated true
            programArgs '--launch_target', 'net.fabricmc.loader.impl.launch.knot.KnotClient'
            mainClass.set 'net.covers1624.devlogin.DevLogin'
            setConfigName("Fabric Client")
            ideConfigGenerated(true)
            runDir("run")
            def args = [
                    "-Dlib39.modid=${mod_id}".toString(),
                    "-Dlib39.output=${generatedOutput}".toString(),
                    "-Dlib39.existing.fabric=${fabricResources}".toString(),
                    "-Dlib39.existing.common=${commonResources}".toString(),
                    "-Dlib39.existing.fabricBuild=${fabricBuildResources}".toString(),
                    "-Dlib39.existing.commonBuild=${commonBuildResources}".toString()
            ]
            vmArgs.addAll(args)

            // 也可以添加JVM参数
            vmArgs.addAll(
                    "-Dfabric.log.level=info",
                    "-Dmixin.debug.export=true"
            )
        }

        server {
            server()
            serverWithGui()
            setConfigName("Fabric Server")
            ideConfigGenerated(true)
            runDir("run")

            def args = [
                    "-Dlib39.modid=${mod_id}".toString(),
                    "-Dlib39.output=${generatedOutput}".toString(),
                    "-Dlib39.existing.fabric=${fabricResources}".toString(),
                    "-Dlib39.existing.common=${commonResources}".toString(),
                    "-Dlib39.existing.fabricBuild=${fabricBuildResources}".toString(),
                    "-Dlib39.existing.commonBuild=${commonBuildResources}".toString()
            ]
            vmArgs.addAll(args)
        }
    }
}

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
    dependsOn project(':common').tasks.named('javadoc')  // 显式依赖common的javadoc
    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/25/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
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    archiveClassifier.set('javadoc')
    from javadoc.destinationDir
    from project(':common').javadoc.destinationDir
}

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

def loaderAttribute = Attribute.of('io.github.mcgradleconventions.loader', String)
['apiElements', 'runtimeElements', 'sourcesElements', 'javadocElements', 'includeInternal', 'modCompileClasspath'].each { variant ->
    configurations.named("$variant") {
        attributes {
            attribute(loaderAttribute, 'fabric')
        }
    }
}
sourceSets.configureEach {
    [it.compileClasspathConfigurationName, it.runtimeClasspathConfigurationName].each { variant->
        configurations.named("$variant") {
            attributes {
                attribute(loaderAttribute, 'fabric')
            }
        }
    }
}