//file:noinspection GroovyAssignabilityCheck

buildscript {
    dependencies {
        classpath 'org.spongepowered:mixingradle:0.7.+'
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.0"
    }
}

plugins {
    id 'net.minecraftforge.gradle' version '5.1.+'
    id 'org.jetbrains.kotlin.jvm' version '1.8.0'
    id 'org.jetbrains.kotlin.plugin.serialization' version '1.8.0'
    id 'org.parchmentmc.librarian.forgegradle' version '1.+'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}

version = '2.0.0'
group = 'org.eln2.mc'
archivesBaseName = 'eln2'

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

tasks.build.dependsOn kotlinSourcesJar
tasks.build.dependsOn shadowJar

println(
    'Grissess Status - Java: ' + System.getProperty('java.version') +
        ' JVM: ' + System.getProperty('java.vm.version') +
        '(' + System.getProperty('java.vendor') + ') ' +
        'Arch: ' + System.getProperty('os.arch')
)

minecraft {
    mappings channel: 'parchment', version: '2023.03.12-1.19.3'

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

    runs {
        client {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${projectDir}/build/createSrgToMcp/output.srg"

            mods {
                eln2 {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'

            mods {
                eln2 {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'

            args '--mod', 'eln2', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')

            mods {
                eln2 {
                    source sourceSets.main
                }
            }
        }
    }
}

configurations {
    library
    implementation.extendsFrom library
}

reobf {
    shadowJar {
        // empty
    }
}

test {
    useJUnitPlatform()
}

// Include resources generated by data generators.
sourceSets.main.resources {
    srcDir 'src/generated/resources'
}

repositories {
    mavenCentral()
    maven {
        name 'JitPack'
        url 'https://jitpack.io'
    }
    maven {
        name 'tterrag maven'
        url 'https://maven.tterrag.com/'
    }
    maven {
        name = 'Kotlin for Forge'
        url = 'https://thedarkcolour.github.io/KotlinForForge/'
        content {
            includeGroup 'thedarkcolour'
        }
    }
    maven {
        url = 'https://maven2.bai.lol'
        content {
            includeGroup 'lol.bai'
            includeGroup 'mcp.mobius.waila'
        }
    }
}

dependencies {
    minecraft 'net.minecraftforge:forge:1.19.3-44.1.23'
    library ("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") {
        exclude group: 'org.jetbrains', module: 'annotations'
    }
    library("org.jetbrains.kotlin:kotlin-reflect:1.8.0")
    library("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")

    compileOnly fg.deobf('mcp.mobius.waila:wthit-api:forge-6.4.2')
    runtimeOnly fg.deobf('mcp.mobius.waila:wthit:forge-6.4.2')
    runtimeOnly fg.deobf('lol.bai:badpackets:forge-0.3.4')
    implementation fg.deobf('com.jozufozu.flywheel:flywheel-forge-1.19.3:0.6.8.a-1')

    library 'com.github.age-series:LibAge:8ed5950a0d'
    library 'org.apache.commons:commons-math3:3.6.1'
    library('com.charleskorn.kaml:kaml:0.50.0')

    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
}

shadowJar {
    archiveClassifier.set("")
    configurations = [project.configurations.library]

    def rlTarget = "${project.group}.relocated"

    relocate 'kotlin', "${rlTarget}.kotlin"
    relocate 'com.charleskorn', "${rlTarget}.charleskorn"
    relocate 'org.snakeyaml', "${rlTarget}.snakeyaml"
    relocate 'org.apache.commons', "${rlTarget}.apache.commons"
    relocate 'org.ageseries', "${rlTarget}.ageseries"
    relocate 'org.intellij', "${rlTarget}.intellij"
    relocate 'jetbrains', "${rlTarget}.jetbrains"

    afterEvaluate {
        finalizedBy reobfShadowJar
    }
}

jar {
    //noinspection GroovyAssignabilityCheck
    archiveClassifier = 'slim'
    manifest {
        attributes([
            "Specification-Title"     : 'eln2',
            "Specification-Vendor"    : 'age-series-team',
            "Specification-Version"   : '2',
            "Implementation-Title"    : project.name,
            "Implementation-Version"  : project.jar.archiveVersion,
            "Implementation-Vendor"   : 'age-series-team',
            "Implementation-Timestamp": new Date().format('yyyy-MM-dd\'T\'HH:mm:ssZ')
        ])
    }
}

final def classpathBlacklist = [
    'annotations-23.0.0.jar'
]

minecraft.runs.configureEach {
    lazyToken('minecraft_classpath') {
        configurations.library.copyRecursive()
            .resolve()
            .stream()
            .filter {
                !classpathBlacklist.any { final blacklisted ->
                    it.path.endsWith(blacklisted)
                }
            }
            .collect { it.absolutePath }
            .join(File.pathSeparator)
    }
}

reobf {
    jar { classpath.from(sourceSets.main.compileClasspath) }
    shadowJar {}
}

tasks.withType(JavaCompile).configureEach {
    options.encoding = 'UTF-8'
}

jar.finalizedBy('reobfJar')

