//file:noinspection GroovyAssignabilityCheck
buildscript {
    repositories {
        maven { url = 'https://repo.spongepowered.org/repository/maven-public/' }
        mavenCentral()
    }
    dependencies {
        classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
    }
}

plugins {
    id 'eclipse'
    id 'idea'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '[6.0.16,6.2)'
    id 'org.parchmentmc.librarian.forgegradle' version '1.+'
}

apply plugin: 'org.spongepowered.mixin'

group = mod_group_id
version = mod_version

base {
    archivesName = mod_id
}

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

minecraft {
    mappings channel: mapping_channel, version: mapping_version

    accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

    copyIdeResources = true

    runs {
        configureEach {
            workingDirectory(file('run'))

            property 'forge.logging.markers', 'REGISTRIES'

            property 'forge.logging.console.level', 'debug'

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

        var client = client {
            property 'forge.enabledGameTestNamespaces', mod_id
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${project.projectDir}/build/createSrgToMcp/output.srg"
        }

        client_0 {
            parent(client)
            args("--username", "player_0")
        }

        client_1 {
            parent(client)
            args("--username", "player_1")
        }

        server {
            property 'forge.enabledGameTestNamespaces', mod_id
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${project.projectDir}/build/createSrgToMcp/output.srg"
            args '--nogui'
        }

        gameTestServer {
            property 'forge.enabledGameTestNamespaces', mod_id
        }

        data {
            workingDirectory(file('run-data'))
            property('gradle.task', 'runData')
            args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
        }
    }
}

mixin {
    add sourceSets.main, "${mod_id}.refmap.json"
    config "${mod_id}.mixins.json"
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

repositories {
    flatDir {
        dir 'libs'
    }
    maven { // TOP
        url "https://cursemaven.com"
    }
    maven { //mirror
        name = "ModMaven"
        url = "https://modmaven.dev"
    }
    maven {
        name "KosmX's maven"
        url 'https://maven.kosmx.dev/'
    }
    maven {
        name = "Illusive Soulworks maven"
        url = "https://maven.theillusivec4.top/"
    }
    maven {
        name = "LTD Maven"
        url = "https://nexus.bot.leisuretimedock.top/repository/maven-public/"
    }
}

jarJar.enable()

dependencies {
    annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'
    minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
    compileOnly 'org.spongepowered:mixin:0.8.5'
    implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:0.4.1"))
    implementation(jarJar("io.github.llamalad7:mixinextras-forge:0.4.1") {
        jarJar.ranged(it, "[0.4.1,)")
    })

    implementation fg.deobf("dev.kosmx.player-anim:player-animation-lib-forge:1.0.2-rc1+1.20")
    implementation fg.deobf("io.github.kosmx.bendy-lib:bendy-lib-forge:4.0.0")
    implementation("io.zershyan:sccore:1.20.1-1.0.0")
}

tasks.named('processResources', ProcessResources).configure {
    var replaceProperties = [
            minecraft_version: minecraft_version,
            minecraft_version_range: minecraft_version_range,
            forge_version: forge_version,
            forge_version_range: forge_version_range,
            loader_version_range: loader_version_range,
            mod_id: mod_id,
            mod_name: mod_name,
            mod_license: mod_license,
            mod_version: mod_version,
            mod_authors: mod_authors,
            mod_description: mod_description,
            mod_credits: mod_credits,
            mod_url: mod_url,
            sccore_verson: sccore_verson
    ]

    inputs.properties replaceProperties

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand replaceProperties + [project: project]
    }
}

tasks.named('jar', Jar).configure {
    manifest {
        attributes(["Specification-Title"     : mod_id,
                    "Specification-Vendor"    : mod_authors,
                    "Specification-Version"   : "1",
                    "Implementation-Title"    : project.name,
                    "Implementation-Version"  : project.jar.archiveVersion,
                    "Implementation-Vendor"   : mod_authors,
                    "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
                    "MixinConfigs"            : "${mod_id}.mixins.json"
        ])
    }

    archiveFileName = "${mod_id}-${mod_version}-origin.jar"
    finalizedBy 'reobfJar'
}

tasks.withType(Javadoc).configureEach {
    options.addStringOption('Xdoclint:none', '-quiet')
    options.addStringOption('Xdoclint:-missing', '-quiet')
    options.encoding = 'UTF-8'
    failOnError = false
}

tasks.named('publish') {
    dependsOn build
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact jar
            artifact javadocJar
            artifact sourcesJar

            groupId = project.group
            artifactId = mod_id
            version = project.version
            pom {
                name = mod_id
                description = mod_description
                url = mod_url
                licenses {
                    license {
                        name = mod_license
                    }
                }
                developers {
                    developer {
                        id = "{mod_id}"
                        name = mod_authors
                    }
                }
            }
        }
    }
    repositories {
        maven {
            name = 'LTDNexus'
            url = 'https://nexus.bot.leisuretimedock.top/repository/maven-releases/'
            credentials {
                username = System.getenv('LTDNexusUsername') ?: ''
                password = System.getenv('LTDNexusPassword') ?: ''
            }
        }
    }
}

gradle.buildFinished {
    def allJarsJar = file("build/libs/${mod_id}-${mod_version}-all.jar")
    if (allJarsJar.exists()) {
        ant.move(file: allJarsJar, tofile: file("build/libs/${mod_id}-${mod_version}.jar"))
    }
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}
