plugins {
    id "net.minecraftforge.gradle" version "[6.0.24,6.2.)"
    id "org.spongepowered.mixin" version '0.7.+'
    id "com.matthewprenger.cursegradle" version "1.4.0"
    id "com.modrinth.minotaur" version "2.+"
}

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

minecraft {
    mappings channel: 'official', version: minecraft_version
    accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
    reobf = false

    runs {
        client {
            workingDirectory project.file('run')
            args "-mixin.config=${mod_id}.mixins.json", "-mixin.config=${mod_id}.forge.mixins.json"
            ideaModule "${rootProject.name}.${project.name}.main"
            taskName 'Client'
        }

        server {
            workingDirectory project.file('run')
            args "-mixin.config=${mod_id}.mixins.json", "-mixin.config=${mod_id}.forge.mixins.json"
            ideaModule "${rootProject.name}.${project.name}.main"
            taskName 'Server'
        }

        data {
            workingDirectory project.file('run')
            ideaModule "${rootProject.name}.${project.name}.main"
            args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/'), "-mixin.config=${mod_id}.mixins.json", "-mixin.config=${mod_id}_forge.mixins.json"
            taskName 'Data'
        }
    }
}

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

dependencies {
    minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
    compileOnly project(":Common")
    annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'

    // Hack fix for now, force jopt-simple to be exactly 5.0.4 because Mojang ships that version, but some transtive dependencies request 6.0+
    implementation('net.sf.jopt-simple:jopt-simple:5.0.4') { version { strictly '5.0.4' } }
}

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

// As of 1.20.6 Forge no longer has reobf tasks. This has broken mixins adding configs correctly into the manifest file
// See: https://github.com/SpongePowered/MixinGradle/blob/f800b26d2b180d98d9aa9355e5b3086d71218508/src/main/groovy/org/spongepowered/asm/gradle/plugins/MixinExtension.groovy#L184
// Instead, we will do it ourselves.
jar {
    manifest {
        attributes([
            'MixinConfigs': "${mod_id}.mixins.json" + "," + "${mod_id}.forge.mixins.json",
        ])
    }
}

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

// Merge the resources and classes into the same directory.
// This is done because java expects modules to be in a single directory.
// And if we have it in multiple we have to do performance intensive hacks like having the UnionFileSystem
// This will eventually be migrated to ForgeGradle so modders don't need to manually do it. But that is later.
sourceSets.each {
    def dir = layout.buildDirectory.dir("sourcesSets/$it.name")
    it.output.resourcesDir = dir
    it.java.destinationDirectory = dir
}

processResources {
    from project(":Common").sourceSets.main.resources

    filesMatching("${mod_id}.mixins.json") {
        expand "refmap_target": "${mod_id}."
    }
}

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

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

        releaseType = release_channel
        addGameVersion minecraft_version

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

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

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

publishing {
    publications {
        mavenJava(MavenPublication) {
            // We must use artifact jar to ensure the Forge dependency is excluded from the pom, breaking fg.deobf()
            artifact jar
            groupId project.group
            artifactId project.archivesBaseName
            version project.version

            pom {
                name = 'TerraBlender-forge'
                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")
            }
        }
    }
}
