plugins {
    id 'multiloader-loader'
    id 'net.neoforged.moddev'
}
def commonResources = project(':common').file('src/main/resources/').getAbsolutePath()
def neoforgeResources = file('src/main/resources/').getAbsolutePath()
def neoforgeBuildResources = 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
    // Automatically enable neoforge AccessTransformers if the file exists
    def at = project(':common').file('src/main/resources/META-INF/accesstransformer.cfg')
    if (at.exists()) {
        accessTransformers.from(at.absolutePath)
    }
    runs {
        configureEach {
            systemProperty('neoforge.enabledGameTestNamespaces', mod_id)
            ideName = "NeoForge ${it.name.capitalize()} (${project.path})" // Unify the run config names with fabric
        }
        client {
            client()
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--existing', neoforgeResources,
                    '--existing', commonResources,
                    '--existing', neoforgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        data {
            clientData()

            // DataGen can be run by - "./gradlew :neoforge:runData" in Terminal.
            // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--output', generatedOutput,
                    '--existing', neoforgeResources,
                    '--existing', commonResources,
                    '--existing', neoforgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
        server {
            server()
            programArguments.addAll(
                    '--mod', project.mod_id,
                    '--all',
                    '--existing', neoforgeResources,
                    '--existing', commonResources,
                    '--existing', neoforgeBuildResources,
                    '--existing', commonBuildResources
            )
        }
    }
    mods {
        "${mod_id}" {
            sourceSet sourceSets.main
        }
    }
}

// 配置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/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
    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')
}

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


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