plugins {
    id 'java-library'
    id 'eclipse'
    id 'idea'
    id 'maven-publish'
    id 'net.neoforged.moddev' version '2.0.91'
    id 'com.gradleup.shadow' version '9.0.0-beta15'
}

version = mod_version

if (System.getenv('BUILD_NUMBER') != null) {
    version += "." + System.getenv('BUILD_NUMBER')
}

group = 'com.hollingsworth.ars_nouveau' // http://maven.apache.org/guides/mini/guide-naming-conventions.html

base {
    archivesName = mod_id + '-' + minecraft_version
}

java.toolchain.languageVersion = JavaLanguageVersion.of(21)
java.withSourcesJar()

configurations {
    libraries
    shade

    implementation.extendsFrom libraries
    runtimeClasspath.extendsFrom localRuntime
    implementation.extendsFrom shade
}

neoForge {
    version = project.neo_version
    validateAccessTransformers = true

    runs {
        configureEach {
            systemProperty 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            systemProperty 'forge.logging.console.level', 'debug'
        }

        client {
            client()
            gameDirectory = project.file('runs/client')
        }

        clientAlt {
            client()
            gameDirectory = project.file('runs/client')
            programArguments.addAll('--username', 'Dev2')
        }

        data {
            data()
            gameDirectory = project.file('runs/data')
            programArguments.addAll('--mod', mod_id, '--all', '--output', file('src/generated/resources/').getAbsolutePath(), '--existing', file('src/main/resources/').getAbsolutePath())
        }

        server {
            server()
            gameDirectory = project.file('server')
        }
    }

    mods {
        testproject {
            sourceSet sourceSets.main
        }
    }

    parchment {
        minecraftVersion = project.minecraft_version
        mappingsVersion = project.parchment_version
    }
}

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

repositories {
    mavenLocal()
    maven {
        name = 'GeckoLib'
        url 'https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/'
        content {
            includeGroup("software.bernie.geckolib")
        }
    }
    exclusiveContent {
        forRepository {
            maven {
                url "https://cursemaven.com"
            }
        }
        filter {
            includeGroup "curse.maven"
        }
    }
    exclusiveContent {
        forRepository {
            maven {
                name = "Modrinth"
                url = "https://api.modrinth.com/maven"
            }
        }
        filter {
            includeGroup "maven.modrinth"
        }
    }
    maven {
        name = "Illusive Soulworks maven"
        url = "https://maven.theillusivec4.top/"
    }
    maven {
        name = "Jared's maven"
        url = "https://maven.blamejared.com/"
    }
    maven {
        name = 'Forge'
        url = "https://maven.minecraftforge.net"
    }
    maven {
        name = "OctoStudios"
        url = uri("https://maven.octo-studios.com/releases")
    }
    maven {
        name = 'TerraformersMC'
        url = 'https://maven.terraformersmc.com/'
        content {
            includeGroup 'dev.emi'
        }
    }
    maven {
        name = "Gegy"
        url = uri("https://maven.gegy.dev/releases/")
        content {
            includeGroup "io.github.queerbric"
            includeGroup "dev.lambdaurora"
            includeGroup "dev.lambdaurora.lambdynamiclights"
        }
    }
}

def mappingsAttribute = Attribute.of("net.minecraft.mappings", String.class)

dependencies {
    attributesSchema {
        attribute(mappingsAttribute)
    }

    compileOnly('org.jetbrains:annotations:26.0.2')

    implementation "software.bernie.geckolib:geckolib-neoforge-1.21.1:${geckolib_version}"
    compileOnly('vazkii.patchouli:Patchouli:1.21-87-NEOFORGE')

    localRuntime "com.illusivesoulworks.caelus:caelus-neoforge:${caelus_version}"
    compileOnly "com.illusivesoulworks.caelus:caelus-neoforge:${caelus_version}:api"

    compileOnly "mezz.jei:jei-1.21.1-neoforge-api:${jei_version}"
    implementation "mezz.jei:jei-1.21.1-neoforge:${jei_version}"
    compileOnly "dev.emi:emi-neoforge:${emi_version}:api"
    implementation 'com.github.glitchfiend:TerraBlender-neoforge:1.21-4.0.0.0'
    implementation "curse.maven:jade-324717:5444008"
    implementation "top.theillusivec4.curios:curios-neoforge:${curios_version}+1.21.1"
    compileOnly("dev.lambdaurora.lambdynamiclights:lambdynamiclights-api:${lambdynamiclights_version}") {
        attributes {
            attribute(mappingsAttribute, "mojmap")
        }
    }
    jarJar("dev.lambdaurora.lambdynamiclights:lambdynamiclights-api:${lambdynamiclights_version}") {
        attributes {
            attribute(mappingsAttribute, "mojmap")
        }
    }

    localRuntime "dev.emi:emi-neoforge:${emi_version}"
    localRuntime 'curse.maven:ftb-teams-forge-404468:5631446'
    localRuntime 'curse.maven:ftb-chunks-forge-314906:5710609'
    localRuntime 'curse.maven:ftb-library-forge-404465:5754910'
    localRuntime 'curse.maven:architectury-api-419699:5553800'
    localRuntime("dev.lambdaurora.lambdynamiclights:lambdynamiclights-runtime:${lambdynamiclights_version}") {
        attributes {
            attribute(mappingsAttribute, "mojmap")
        }
    }
//    localRuntime 'curse.maven:productivebees-377897:6154395'

    jarJar(implementation('com.hollingsworth.nuggets:nuggets-neoforge-1.21') {
        version {
            strictly '[1.0.10,)'
            prefer '1.0.7.38'
        }
    })
    localRuntime "maven.modrinth:cloth-config:15.0.140+neoforge"

    // Apotheosis
    localRuntime "curse.maven:apothic-attributes-898963:6430300"
    localRuntime "curse.maven:placebo-283644:6446766"
    //localRuntime "curse.maven:immersive-portals-for-forge-355440:6013197"
//    implementation "curse.maven:ars-elemental-561470:6419081"
    additionalRuntimeClasspath(shade('org.apache.lucene:lucene-core:10.1.0'))
    additionalRuntimeClasspath(shade('org.apache.lucene:lucene-analysis-common:10.1.0'))
    additionalRuntimeClasspath(shade('org.apache.lucene:lucene-queryparser:10.1.0'))
//    implementation "curse.maven:immersive-engineering-231951:6506639"
}

jar {
    manifest {
        attributes(["Specification-Title"     : "ArsNouveau",
                    "Specification-Vendor"    : "baileyh",
                    "Specification-Version"   : "1", // We are version 1 of ourselves
                    "Implementation-Title"    : project.name,
                    "Implementation-Version"  : mod_version,
                    "Implementation-Vendor"   : "baileyh",
                    "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
                    "MixinConfigs"            : "ars_nouveau.mixins.json"])
    }
}

shadowJar {
    from(tasks.jarJar)
    configurations = [project.configurations.shade] // <- Tell shadowJar to shade dependencies from 'shade'
    archiveClassifier = ''
    relocate 'org.apache.lucene', 'org.apache.lucene.ars_nouveau'
    archiveClassifier = null
    mergeServiceFiles()
}

assemble.dependsOn(shadowJar)

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId project.group
            artifactId project.archivesBaseName
            version project.version
            from components.java
        }
    }
    repositories {
        maven {
            url "file://" + System.getenv("local_maven")
        }
    }
}

tasks.withType(PluginUnderTestMetadata).configureEach {
    it.pluginClasspath.setFrom tasks.shadowJar, configurations.runtimeClasspath
    it.inputs.file(shadowJar.archiveFile)
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8' // Use the UTF-8 charset for Java compilation
}

// IDEA no longer automatically downloads sources/javadoc jars for dependencies, so we need to explicitly enable the behavior.
idea {
    module {
        downloadSources = true
        downloadJavadoc = true
    }
}