plugins {
    id "net.neoforged.gradle.userdev" version "7.+"
    id "net.neoforged.gradle.mixin" version "7.+"
    id "com.matthewprenger.cursegradle" version "1.4.0"
    id "com.modrinth.minotaur" version "2.+"
}

base.archivesName.set("${mod_name}-neoforge")

minecraft {
    mappings {
        channel = official()
        version.put "minecraft", minecraft_version
    }
    accessTransformers.file('src/main/resources/META-INF/accesstransformer.cfg')
}

runs {
    configureEach {
        modSource project.sourceSets.main
    }

    client {
        workingDirectory.set(project.file('run'))
        systemProperty 'neoforge.enabledGameTestNamespaces', mod_id
    }

    server {
        workingDirectory.set(project.file('run'))
        systemProperty 'neoforge.enabledGameTestNamespaces', mod_id
        programArgument '--nogui'
    }
}

dependencies {
    implementation "net.neoforged:neoforge:${neoforge_version}"
    compileOnly project(":Common")
}

// NeoGradle compiles the game, but we don't want to add our common code to the game's code
TaskCollection.metaClass.excludingNeoTasks = { ->
    delegate.matching { !it.name.startsWith("neo") }
}

tasks.withType(JavaCompile).excludingNeoTasks().configureEach {
    source(project(":Common").sourceSets.main.allSource)
}

tasks.withType(Javadoc).excludingNeoTasks().configureEach {
    source(project(":Common").sourceSets.main.allJava)
}

tasks.named("sourcesJar", Jar) {
    from(project(":Common").sourceSets.main.allSource)
}

tasks.withType(ProcessResources).excludingNeoTasks().configureEach {
    from project(":Common").sourceSets.main.resources
    filesMatching("${mod_id}.mixins.json") {
        expand "refmap_target": "${mod_id}."
    }
}

curseforge {
    apiKey = project.findProperty('curseApiKey') ?: 'unset'
    project {
        id = '940057'

        if (changelog_file.exists()) {
            changelog = changelog_file
        }

        releaseType = release_channel
        addGameVersion minecraft_version
        addGameVersion 'NeoForge' // NeoForge isn't auto-detected and must be manually specified

        mainArtifact(jar) {
            displayName = "TerraBlender NeoForge ${version}"
        }
    }
}

modrinth {
    token = project.findProperty('modrinthToken')
    projectId = "terrablender"
    versionNumber = mod_version
    versionName = mod_version + " for NeoForge " + minecraft_version
    versionType = release_channel // can be release, beta, or alpha
    uploadFile = jar
    gameVersions = [minecraft_version]
    loaders = ['neoforge']

    if (changelog_file.exists()) {
        changelog = trimChangelog(changelog_file.text)
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact jar
            groupId project.group
            artifactId project.archivesBaseName
            version project.version

            pom {
                name = 'TerraBlender-neoforge'
                description = "A library mod for adding biomes in a simple and compatible manner with Minecraft's new biome/terrain system."
                url = 'https://github.com/Glitchfiend/TerraBlender'
                scm {
                    url = 'https://github.com/Glitchfiend/TerraBlender'
                    connection = 'scm:git:git://github.com/Glitchfiend/TerraBlender.git'
                    developerConnection = 'scm:git:git@github.com:Glitchfiend/TerraBlender.git'
                }
                issueManagement {
                    system = 'github'
                    url = 'https://github.com/Glitchfiend/TerraBlender/issues'
                }
                licenses {
                    license {
                        name = 'LGPLv3'
                        url = 'https://www.gnu.org/licenses/lgpl-3.0.txt'
                    }
                }
                developers {
                    developer {
                        id = 'adubbz'
                        name = 'Adubbz'
                    }
                }
            }
        }
    }
    repositories {
        maven {
            url = "https://maven.minecraftforge.net/releases/"
            credentials {
                username = System.getenv("MAVEN_USER")
                password = System.getenv("MAVEN_PASSWORD")
            }
        }
    }
}
