plugins {
    id 'eclipse'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '5.1.+'
    id "com.matthewprenger.cursegradle" version "1.4.0"
}

apply {
    // "Simple" fix for M1 (Apple Silicon) macs in the dev environment... All thanks to @mezz
    from "https://raw.githubusercontent.com/mezz/JustEnoughItems/1.18/Forge/buildtools/AppleSiliconSupport.gradle"
}

def ENV = System.getenv()

version = "${version}-build.${ENV.GITHUB_RUN_NUMBER ?: 9999}+mc${minecraft_version}"
group = 'com.direwolf20'
archivesBaseName = "buildinggadgets"

def forge_major = forge_version.tokenize('.')[0]

java.toolchain.languageVersion = JavaLanguageVersion.of(17)

minecraft {
    mappings channel: 'official', version: "${minecraft_version}"
    // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.

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

    runs {
        client {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            mods {
                buildinggadgets {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            mods {
                buildinggadgets {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            args '--mod', 'buildinggadgets', '--all', '--output', file('src/generated/resources/')
            mods {
                buildinggadgets {
                    source sourceSets.main
                }
            }
        }
    }
}

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

repositories {
    maven {
        url = "https://modmaven.dev/"
    }
}

dependencies {
    minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"

    compileOnly fg.deobf("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")
    compileOnly fg.deobf("mezz.jei:jei-${minecraft_version}-forge-api:${jei_version}")
    runtimeOnly fg.deobf("mezz.jei:jei-${minecraft_version}-forge:${jei_version}")
}

processResources {
    // This might be the wrong way of doing it but basically this file doesn't always get updated
    // for some reason so I'm deleting it before it's generated again
    def path = new File("${project.buildDir}/resources/main/META-INF/mods.toml")
    if (path.exists()) {
        path.delete()
    }

    exclude '.cache'
    inputs.property "version", project.version

    filesMatching("META-INF/mods.toml") {
        expand "version": project.version,
                "forgeversion": project.forge_version,
                "forgeshortversion": project.forge_version.split("\\.")[0],
                "mcversion": project.minecraft_version
    }
}

jar {
    finalizedBy 'reobfJar'

    manifest {
        attributes(["Specification-Title"     : "BuildingGadgets",
                    "Specification-Vendor"    : "Direwolf20",
                    "Specification-Version"   : forge_major, // We are version 1 of the modlauncher specification
                    "Implementation-Title"    : project.archivesBaseName,
                    "Implementation-Version"  : project.version,
                    "Implementation-Vendor"   : "Direwolf20",
                    "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")])
    }
}
java {
    withSourcesJar()
}

task deobfJar(type: Jar) {
    from sourceSets.main.output
    classifier 'deobf'
}

artifacts {
    archives deobfJar
}

publishing {
    publications {
        maven(MavenPublication) {
            from components.java
            artifactId = project.archivesBaseName.toLowerCase()
            artifact(deobfJar) {
                classifier 'deobf'
            }
        }
    }

    repositories {
        maven {
            name = "GitHubPackages"
            url = "https://maven.pkg.github.com/Direwolf20-MC/BuildingGadgets"
            credentials {
                username = ENV.GITHUB_ACTOR
                password = ENV.GITHUB_TOKEN
            }
        }
    }
}

if (ENV.CURSE_TOKEN) {
    curseforge {
        apiKey = ENV.CURSE_TOKEN
        project {
            id = "298187"
            releaseType = "release"
            addGameVersion "${minecraft_version}"
            addGameVersion "Forge"
            addGameVersion "Java 17"
            mainArtifact(jar) {
                relations {
                    optionalDependency 'charging-gadgets'
                }
            }
            changelog = file('CHANGELOG.md')
            changelogType = 'markdown'
        }
    }
}
