plugins {
    id "com.github.johnrengelman.shadow" version "8.1.1"
    id "me.shedaniel.unified-publishing"
}

loom {
    accessWidenerPath = project(":common").loom.accessWidenerPath
}

architectury {
    platformSetupLoomIde()
    neoForge {
        platformPackage = "forge"
    }
}

configurations {
    common
    shadowCommon // Don't use shadow from the shadow plugin because we don't want IDEA to index this.
    compileClasspath.extendsFrom common
    runtimeClasspath.extendsFrom common
    developmentNeoForge.extendsFrom common
}

dependencies {
    neoForge "net.neoforged:neoforge:${rootProject.neoforge_version}"

    common(project(path: ":common", configuration: "namedElements")) { transitive false }
    shadowCommon(project(path: ":common", configuration: "transformProductionNeoForge")) { transitive false }
}

processResources {
    filesMatching("META-INF/neoforge.mods.toml") {
        expand "version": project.version
    }
    inputs.property "META-INF/neoforge.mods.toml", project.version
}

shadowJar {
    exclude "fabric.mod.json"
    exclude "architectury-common.accessWidener"
    exclude "architectury.common.json"

    configurations = [project.configurations.shadowCommon]
    archiveClassifier = "dev-shadow"

    // Replace classes with forge's version
    exclude "dev/architectury/core/block/ArchitecturyLiquidBlock.class"
    exclude "dev/architectury/core/fluid/ArchitecturyFlowingFluid.class"
    exclude 'dev/architectury/core/fluid/ArchitecturyFlowingFluid$Source.class'
    exclude 'dev/architectury/core/fluid/ArchitecturyFlowingFluid$Flowing.class'
    exclude 'dev/architectury/core/item/ArchitecturyBucketItem.class'
    exclude 'dev/architectury/core/item/ArchitecturyMobBucketItem.class'
    relocate "dev.architectury.core.block.forge.imitator", "dev.architectury.core.block"
    relocate "dev.architectury.core.fluid.forge.imitator", "dev.architectury.core.fluid"
    relocate "dev.architectury.core.item.forge.imitator", "dev.architectury.core.item"
}

remapJar {
    input.set shadowJar.archiveFile
    dependsOn shadowJar
    archiveClassifier = null
    atAccessWideners.add "architectury.accessWidener"
}

task renameJarForPublication(type: Zip, dependsOn: remapJar) {
    from remapJar.archiveFile.map { zipTree(it) }
    archiveExtension = "jar"
    metadataCharset "UTF-8"
    destinationDirectory = base.libsDirectory
    archiveClassifier = project.name
}

assemble.dependsOn renameJarForPublication

jar {
    archiveClassifier = "dev"
}

sourcesJar {
    afterEvaluate {
        [":common"].forEach {
            def depSources = project(it).sourcesJar
            dependsOn depSources
            from(depSources.archiveFile.map { zipTree(it) }) {
                exclude "architectury.accessWidener"
            }
        }
    }
}

components.java {
    withVariantsFromConfiguration(project.configurations.shadowRuntimeElements) {
        skip()
    }
}

publishing {
    publications {
        mavenNeoForge(MavenPublication) {
            artifactId = rootProject.archivesBaseName + "-neoforge"
            from components.java
        }
    }

    repositories {
        if (System.getenv("MAVEN_PASS") != null) {
            maven {
                url = "https://deploy.shedaniel.me/"
                credentials {
                    username = "shedaniel"
                    password = System.getenv("MAVEN_PASS")
                }
            }
        }
    }
}

unifiedPublishing {
    if (rootProject.neoforge_pr != "") return // Don't publish PRs
    project {
        displayName = "[NeoForge $rootProject.supported_version] v$project.version"
        releaseType = "$rootProject.artifact_type"
        changelog = releaseChangelog()
        gameVersions = ["1.20.5"]
        gameLoaders = ["neoforge"]
        mainPublication renameJarForPublication

        var CURSE_API_KEY = project.findProperty("CURSE_API_KEY") ?: System.getenv("CURSE_API_KEY")
        if (CURSE_API_KEY != null) {
            curseforge {
                token = CURSE_API_KEY
                id = rootProject.curseforge_id
                gameVersions.addAll "Java 21"
            }
        }

        var MODRINTH_TOKEN = project.findProperty("MODRINTH_TOKEN") ?: System.getenv("MODRINTH_TOKEN")
        if (MODRINTH_TOKEN != null) {
            modrinth {
                token = MODRINTH_TOKEN
                id = rootProject.modrinth_id
                version = "$project.version+$project.name"
            }
        }
    }
}

