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}"
    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"))
    }
    runs {
        client {
            client()
            setConfigName('Fabric Client')
            ideConfigGenerated(true)
            runDir('runs/client')
            programArgs "--username", "dev"
            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()
            setConfigName('Fabric Server')
            ideConfigGenerated(true)
            runDir('runs/server')
            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
    }

    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")
    }

    tasks.named('javadocJar', Jar) {
        dependsOn javadoc
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
        archiveClassifier.set('javadoc')
        from javadoc.destinationDir
        from project(':common').javadoc.destinationDir
    }

    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')
            }
        }
    }
}