plugins {
    id "com.github.johnrengelman.shadow" version "7.1.2"
}

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
    maven { url = 'https://repo.spongepowered.org/maven' }
}

architectury {
    platformSetupLoomIde()
    forge()
}

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

if (!build16) {
    sourceSets.main.java.exclude "ca/fxco/memoryleakfix/mixin/hugeScreenshotLeak/*", "ca/fxco/memoryleakfix/mixin/tagKeyLeak/*"
}

loom {
    /*
    accessWidenerPath = project(":common").loom.accessWidenerPath

    forge {
        convertAccessWideners = true
        extraAccessWideners.add loom.accessWidenerPath.get().asFile.name
    }
     */
    forge {
        def commonMixinId = "memoryleakfix" + (build16 ? "-16" : "")
        mixinConfig commonMixinId + ".mixins.json"
    }
}

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
    developmentForge.extendsFrom common
    shadowCommon.extendsFrom implementation
}

dependencies {
    forge "net.minecraftforge:forge:${build16 ? rootProject.forge_version_16 : rootProject.forge_version}"
    // Remove the next line if you don't want to depend on the API
    // modApi "dev.architectury:architectury-forge:${rootProject.architectury_version}"

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

    // MixinExtras
    forgeRuntimeLibrary(shadowCommon(annotationProcessor("io.github.llamalad7:mixinextras-common:${rootProject.mixinextras_version}"))) {transitive false}
/*
    // Mixin
    if (build16) {
        shadowCommon(annotationProcessor("org.spongepowered:mixin:0.8.5")) {
            transitive false
        }
    }

 */
}

processResources {
    def mcVersion
    def forgeVersion
    if (build16) {
        mcVersion = "[1.15.2,1.16.5]"
        forgeVersion = "[31.2.44,)"
    } else {
        mcVersion = "[1.17,)"
        forgeVersion = "[37,)"
    }
    
    def resInputs = [version: project.version, mcVersion: mcVersion, forgeVersion: forgeVersion]
    inputs.properties(resInputs)

    filesMatching("META-INF/mods.toml") {
        expand(resInputs)
    }
}

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

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

    relocate("com.llamalad7.mixinextras", "ca.fxco.memoryleakfix.mixinextras")
    mergeServiceFiles() // Very important!
}

remapJar {
    input.set shadowJar.archiveFile
    dependsOn shadowJar
    classifier null
}

jar {
    classifier "dev"
}

sourcesJar {
    def commonSources = project(":common").sourcesJar
    dependsOn commonSources
    from commonSources.archiveFile.map { zipTree(it) }
}

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

publishing {
    publications {
        mavenForge(MavenPublication) {
            artifactId = rootProject.archives_base_name + "-" + project.name
            from components.java
        }
    }

    // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing.
    repositories {
        // Add repositories to publish to here.
    }
}
