plugins {
    id 'java-library'
    id 'maven-publish'
}

base {
    archivesName = "${mod_id}-${project.name}-${minecraft_version}"
}

java {
    toolchain.languageVersion = JavaLanguageVersion.of(java_version)
    withSourcesJar()
    withJavadocJar()
}

repositories {
    mavenCentral()
    // https://docs.gradle.org/current/userguide/declaring_repositories.html#declaring_content_exclusively_found_in_one_repository
    exclusiveContent {
        forRepository {
            maven {
                name = 'Sponge'
                url = 'https://repo.spongepowered.org/repository/maven-public'
            }
        }
        filter { includeGroupAndSubgroups('org.spongepowered') }
    }
    exclusiveContent {
        forRepositories(
                maven {
                    name = 'ParchmentMC'
                    url = 'https://maven.parchmentmc.org/'
                },
                maven { url = "https://neoforged.forgecdn.net/releases" },
                maven { url = "https://neoforged.forgecdn.net/mojang-meta" }
        )
        filter { includeGroup('org.parchmentmc.data') }
    }
    maven { url = "https://libraries.minecraft.net/" }


    maven {
        url "https://cursemaven.com"
        content { includeGroup "curse.maven" }
    }
    maven {
        name = 'BlameJared'
        url = 'https://maven.blamejared.com'
    }
}

// Declare capabilities on the outgoing configurations.
// Read more about capabilities here: https://docs.gradle.org/current/userguide/component_capabilities.html#sec:declaring-additional-capabilities-for-a-local-component
['apiElements', 'runtimeElements', 'sourcesElements', 'javadocElements'].each { variant ->
    configurations."$variant".outgoing {
        capability("$group:${project.name}:$version")
        capability("$group:${base.archivesName.get()}:$version")
        capability("$group:$mod_id-${project.name}-${minecraft_version}:$version")
        capability("$group:$mod_id:$version")
    }
    publishing.publications.configureEach {
        suppressPomMetadataWarningsFor(variant)
    }
}

sourcesJar {
    from(rootProject.file('LICENSE')) {
        rename { "${it}_${mod_name}" }
    }
}

jar {
    from(rootProject.file('LICENSE')) {
        rename { "${it}_${mod_name}" }
    }

    manifest {
        attributes([
                'Specification-Title'   : mod_name,
                'Specification-Vendor'  : mod_author,
                'Specification-Version' : project.jar.archiveVersion,
                'Implementation-Title'  : project.name,
                'Implementation-Version': project.jar.archiveVersion,
                'Implementation-Vendor' : mod_author,
                'Built-On-Minecraft'    : minecraft_version
        ])
    }
}

processResources {
    var expandProps = [
            'version'                      : version,
            'group'                        : project.group, //Else we target the task's group.
            'minecraft_version'            : minecraft_version,
            'minecraft_version_range'      : minecraft_version_range,
            'fabric_version'               : fabric_version,
            'fabric_loader_version'        : fabric_loader_version,
            'mod_name'                     : mod_name,
            'mod_author'                   : mod_author,
            'mod_id'                       : mod_id,
            'license'                      : license,
            'description'                  : project.description,
            "forge_version"                : forge_version,
            "forge_loader_version_range"   : forge_loader_version_range,
            'credits'                      : credits,
            'java_version'                 : java_version
    ]

    var jsonExpandProps = expandProps.collectEntries {
        key, value -> [(key): value instanceof String ? value.replace("\n", "\\\\n") : value]
    }

    filesMatching(['META-INF/mods.toml']) {
        expand expandProps
    }

    filesMatching(['pack.mcmeta', 'fabric.mod.json', '*.mixins.json']) {
        expand jsonExpandProps
    }

    inputs.properties(expandProps)
}

publishing {
    publications {
        register('mavenJava', MavenPublication) {
            artifactId base.archivesName.get()
            from components.java
        }
    }
    repositories {
        // 本地仓库
        maven {
            name = 'local'
            url = layout.buildDirectory.dir("repo")
        }
        // Nexus 远程仓库
        maven {
            name = 'LTDNexus'
            url = 'https://nexus.bot.leisuretimedock.top/repository/maven-releases/'
            credentials {
                username = System.getenv('LTDNexusUsername') ?: ''
                password = System.getenv('LTDNexusPassword') ?: ''
            }
        }
    }
}

// ==================== 任务依赖 ====================

tasks.withType(PublishToMavenRepository) {
    dependsOn assemble
    dependsOn javadoc
}

tasks.named('build') {
    dependsOn javadoc, sourcesJar
}

tasks.register('cleanRepo', Delete) {
    delete layout.buildDirectory.dir("repo")
}

tasks.named('clean') {
    dependsOn cleanRepo
}
