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

architectury {
    platformSetupLoomIde()
    forge()
}

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

    forge {
        convertAccessWideners = true
        extraAccessWideners.add loom.accessWidenerPath.get().asFile.name

        mixinConfig "figura-common.mixins.json"
        mixinConfig "figura.mixins.json"
    }
}

configurations {
    common
    shadowCommon // Don't use shadow from the shadow plugin since it *excludes* files.
    compileClasspath.extendsFrom common
    runtimeClasspath.extendsFrom common
    developmentForge.extendsFrom common
}

dependencies {
    mappings loom.layered {
        mappings("org.quiltmc:quilt-mappings:$minecraft_version+build.$mappings:intermediary-v2")
        officialMojangMappings()
    }

    compileOnly(annotationProcessor("io.github.llamalad7:mixinextras-common:0.3.6"))
    implementation(include("io.github.llamalad7:mixinextras-forge:0.3.6"))
    
    // Libraries
    include(forgeRuntimeLibrary("com.github.FiguraMC.luaj:luaj-core:$luaj-figura"))
    include(forgeRuntimeLibrary("com.github.FiguraMC.luaj:luaj-jse:$luaj-figura"))
    include(forgeRuntimeLibrary("com.neovisionaries:nv-websocket-client:$nv_websocket"))

    include(forgeRuntimeLibrary("org.chenliang.oggus:oggus:1.2.0"))
    include(forgeRuntimeLibrary("org.concentus:Concentus:1.0-SNAPSHOT"))

    forge "net.minecraftforge:forge:${rootProject.forge_version}"

    if (rootProject.run_with_geckolib == "true") {
        modRuntimeOnly("software.bernie.geckolib:geckolib-forge-$geckolib_version")
        forgeRuntimeLibrary(group: 'com.eliotlash.mclib', name: 'mclib', version: "${mclib_version}")
    }
    common(project(path: ":common", configuration: "namedElements")) { transitive false }
    shadowCommon(project(path: ":common", configuration: "transformProductionForge")) { transitive = false }

    common(project(path: ":server-common")) { transitive false }
    shadowCommon(project(path: ":server-common")) { transitive false }
}

processResources {
   Map<String, Object> properties = new HashMap<>()

    properties.put("version", project.jarVersion)
    properties.put("java_version", rootProject.java_version)
    properties.put("minecraft_version", rootProject.minecraft_version)
    properties.put("assets_version", rootProject.assets_version)

    properties.forEach((k, v) -> inputs.property(k, v.toString()))
    filesMatching("META-INF/mods.toml") {
        expand properties
    }
}

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

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

remapJar {
    input.set shadowJar.archiveFile
    dependsOn shadowJar
    archiveClassifier.set "forge-mc"
}

jar {
    archiveClassifier.set "dev"
}

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

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

publishing {
    publications {
        maven(MavenPublication) {
            artifactId = "${project.archivesBaseName}-${project.name}"
            version = project.version
            artifact(remapJar) {
                builtBy remapJar
                classifier ''
            }
            artifact(sourcesJar) {
                builtBy sourcesJar
                classifier 'sources'
            }
        }
    }
    setupRepositories(repositories)
}

void setupRepositories(RepositoryHandler repositories) {
    if (project.hasProperty("mavenUrl")) {
        repositories.maven {
            name = "figuraMaven"
            url project.mavenUrl
            credentials(PasswordCredentials)
            authentication {
                basic(BasicAuthentication)
            }
        }
    }
}
