plugins {
    id 'idea'
    id 'net.minecraftforge.gradle' version '[6.0,6.2)'
    id 'org.parchmentmc.librarian.forgegradle' version '1.+'
    id 'org.spongepowered.mixin' version '0.7.+'
}

version = '1.30'
group = 'thedarkcolour.exdeorum'
base {
    archivesName = 'exdeorum'
}

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

minecraft {
    mappings channel: 'parchment', version: "$parchment_mappings-$mc_version"
    accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

    runs {
        copyIdeResources = true

        client {
            workingDirectory project.file('run')

            jvmArgs.add('-XX:+AllowEnhancedClassRedefinition')

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

            mods {
                exdeorum {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run/server')

            jvmArgs.add('-XX:+AllowEnhancedClassRedefinition')

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

            mods {
                exdeorum {
                    source sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')

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

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

            mods {
                exdeorum {
                    source sourceSets.main
                }
            }
        }
    }
}

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

repositories {
    mavenCentral()
    maven {
        name = 'McJty maven'
        url = 'https://maven.k-4u.nl'
        content { includeGroup 'mcjty.theoneprobe' }
    }
    maven {
        name = 'jei'
        url = 'https://maven.blamejared.com/'
        content { includeGroup "mezz.jei" }
    }
    maven {
        name = 'rei'
        url = "https://maven.shedaniel.me"
    }
    maven {
        name = 'Architectury API'
        url = "https://maven.architectury.dev"
        content { includeGroup "dev.architectury" }
    }
    maven {
        name = 'KubeJS and Rhino'
        url = "https://maven.saps.dev/minecraft"
        content { includeGroup "dev.latvian.mods" }
    }
    maven {
        name = 'ModKit'
        url 'https://jitpack.io'
        content { includeGroup 'com.github.thedarkcolour' }
    }
    maven {
        name = 'TerraformersMC'
        url = 'https://maven.terraformersmc.com'
    }
    maven {
        name = "Modrinth"
        url = "https://api.modrinth.com/maven"
        content {
            includeModule("maven.modrinth", "oculus")
            includeModule("maven.modrinth", "embeddium")
        }
    }
    maven {
        url "https://cursemaven.com"
        content {
            includeGroup "curse.maven"
        }
    }
}

dependencies {
    minecraft("net.minecraftforge:forge:${mc_version}-${forge_version}")

    // TOP OPTIONAL
    compileOnly(fg.deobf("mcjty.theoneprobe:theoneprobe:1.20.1-${top_version}") {
        transitive = false
    })
    // JADE OPTIONAL
    implementation(fg.deobf("curse.maven:jade-324717:4986594"))
    // JEI OPTIONAL
    compileOnly(fg.deobf("mezz.jei:jei-${mc_version}-forge-api:${jei_version}"))
    runtimeOnly(fg.deobf("mezz.jei:jei-${mc_version}-forge:${jei_version}"))
    // REI OPTIONAL
    compileOnly(fg.deobf("me.shedaniel:RoughlyEnoughItems-forge:${rei_version}"))
    compileOnly(fg.deobf("me.shedaniel.cloth:cloth-config-forge:${cloth_config_version}"))
    compileOnly(fg.deobf("curse.maven:reipc-521393:4837449"))
    // EMI OPTIONAL
    compileOnly(fg.deobf("dev.emi:emi-forge:${emi_version}:api"))
    runtimeOnly(fg.deobf("dev.emi:emi-forge:${emi_version}"))
    // KubeJS OPTIONAL
    implementation fg.deobf("dev.architectury:architectury-forge:${architectury_version}")
    implementation fg.deobf("dev.latvian.mods:rhino-forge:${rhino_version}")
    implementation fg.deobf("dev.latvian.mods:kubejs-forge:${kubejs_version}")

    // ModKit DEV ONLY
    implementation fg.deobf('com.github.thedarkcolour:ModKit:78f393bfac')

    // Oculus + Embeddium OPTIONAL
    compileOnly fg.deobf('maven.modrinth:oculus:1.20.1-1.6.9')
    compileOnly fg.deobf('maven.modrinth:embeddium:0.2.12+mc1.20.1')

    // testing
    implementation fg.deobf("curse.maven:allthecompressed-514045:4938351")
    //implementation fg.deobf("curse.maven:inventorysorter-240633:4655091")
    implementation fg.deobf("curse.maven:cyclic-239286:4994392")
    implementation fg.deobf("curse.maven:flib-661261:4724762")

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

def replaceProperties = [ mc_version: mc_version, mod_version: version ]

processResources {
    inputs.properties replaceProperties
    replaceProperties.put 'project', project

    filesMatching(['META-INF/mods.toml']) {
        expand replaceProperties
    }

    // Minify JSON resources
    // Actual reduction in file size is just a few KB, but why not?
    doLast {
        fileTree(dir: outputs.files.asPath, include: "**/*.json").each {
            File file ->
                //noinspection UnnecessaryQualifiedReference
                try {
                    file.text = groovy.json.JsonOutput.toJson(new groovy.json.JsonSlurper().parse(file))
                } catch (Exception e) {
                    println "Unable to minify file ${file.name}"
                    throw new RuntimeException(e)
                }
        }
    }
}

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