plugins {
    id 'eclipse'
    id 'idea'
    id 'maven-publish'
    id 'net.minecraftforge.gradle' version '[6.0,6.2)'
    id 'org.parchmentmc.librarian.forgegradle' version '1.+'
}

version = mod_version
group = mod_group_id

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

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

println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}"
minecraft {
    mappings channel: mapping_channel, version: mapping_version
    //reobf(true)
    copyIdeResources = true

    runs {
        configureEach {
            workingDirectory project.file('runs_forge')
            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'
            mods {
                "${mod_id}" {
                    source sourceSets.main
                    source sourceSets.test
                }
            }
        }

        client {
            property 'forge.enabledGameTestNamespaces', mod_id
            args '--username', 'Dev###'
            jvmArg '-XX:+AllowEnhancedClassRedefinition'
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
        }

        server {
            workingDirectory project.file('runs_server_forge')
            property 'forge.enabledGameTestNamespaces', mod_id
            args '--nogui'
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
        }

        gameTestServer {
            property 'forge.enabledGameTestNamespaces', mod_id
        }

        data {
            workingDirectory project.file('runs_data_forge')
            args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
            property 'mixin.env.remapRefMap', 'true'
            property 'mixin.env.refMapRemappingFile', "${buildDir}/createSrgToMcp/output.srg"
        }
    }
}

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

repositories {
    maven {
        url "https://www.cursemaven.com"
        content {
            includeGroup "curse.maven"
        }
    }
    maven {
        url = "https://api.modrinth.com/maven"
    }
    maven {
        name = "Jared's maven"
        url = "https://maven.blamejared.com/"
    }
    maven {
        name = "ModMaven"
        url = "https://modmaven.dev"
    }
    maven {
        // EMI
        name = "TerraformersMC"
        url = "https://maven.terraformersmc.com/"
    }
    maven {
        // REI
        url "https://maven.shedaniel.me/"
    }
    mavenLocal()
    flatDir {
        dir 'libs'
    }
}

dependencies {

    minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}"
    implementation fg.deobf("curse.maven:jade-324717:${jade_id}")

    compileOnly fg.deobf("mezz.jei:jei-${minecraft_version}-common-api:${jei_version}")
    compileOnly fg.deobf("mezz.jei:jei-${minecraft_version}-forge-api:${jei_version}")
    compileOnly fg.deobf("dev.emi:emi-forge:${emi_version}:api")

    runtimeOnly fg.deobf("curse.maven:appleskin-248787:${appleskin_id}")
    runtimeOnly fg.deobf("curse.maven:patchouli-306770:${patchouli_id}")
    runtimeOnly fg.deobf("curse.maven:pipez-443900:6368313")
    runtimeOnly fg.deobf("curse.maven:mekanism-268560:6552911")
    runtimeOnly fg.deobf("curse.maven:mekanism-generators-268566:6552914")

    // We add the full version to localRuntime so that we do not publish a dependency on it
    if (runtime_recipe_viewer == "jei") {
        runtimeOnly fg.deobf("mezz.jei:jei-${minecraft_version}-forge:${jei_version}")
    } else if (runtime_recipe_viewer == "rei") {
        runtimeOnly fg.deobf("me.shedaniel:RoughlyEnoughItems-forge:${rei_version}")
    } else if (runtime_recipe_viewer == "emi") {
        runtimeOnly fg.deobf("dev.emi:emi-forge:${emi_version}")
    } else if (runtime_recipe_viewer == "emi+") {
        runtimeOnly fg.deobf("mezz.jei:jei-${minecraft_version}-forge:${jei_version}")
        runtimeOnly fg.deobf("dev.emi:emi-forge:${emi_version}")
    }

    if (runtime_biome_test == "bop") {
        runtimeOnly fg.deobf("curse.maven:biomes-o-plenty-220318:${bop_id}")
        runtimeOnly fg.deobf("curse.maven:glitchcore-955399:${glitchcore_id}")
        runtimeOnly fg.deobf("curse.maven:terrablender-563928:${terrablender_id}")
    } else if (runtime_biome_test == "tl") {
        runtimeOnly fg.deobf("curse.maven:terralith-513688:${terralith_id}")
    } else if (runtime_biome_test == "bwg") {
        runtimeOnly fg.deobf("curse.maven:oh-the-biomes-weve-gone-1070751:${bwg_id}")
        runtimeOnly fg.deobf("curse.maven:terrablender-563928:${terrablender_id}")
        runtimeOnly fg.deobf("curse.maven:corgilib-693313:6616784")
        runtimeOnly fg.deobf("curse.maven:geckolib-388172:6658942")
        runtimeOnly fg.deobf("curse.maven:oh-the-trees-youll-grow-962544:6608610")
    }

    // For more info:
    // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
    // http://www.gradle.org/docs/current/userguide/dependency_management.html
}


tasks.named('processResources', ProcessResources).configure {
    var replaceProperties = [
            minecraft_version   : minecraft_version, minecraft_version_range: minecraft_version_range,
            forge_version       : forge_version, forge_version_range: forge_version_range,
            loader_version_range: loader_version_range,
            mod_id              : mod_id, mod_name: mod_name, mod_license: mod_license, mod_version: mod_version,
            mod_authors         : mod_authors, mod_description: mod_description,
    ]
    inputs.properties replaceProperties

    filesMatching(['META-INF/mods.toml', 'pack.mcmeta']) {
        expand replaceProperties + [project: project]
    }
}


tasks.named('jar', Jar).configure {
    manifest {
        attributes([
                'Specification-Title'     : mod_id,
                'Specification-Vendor'    : mod_authors,
                'Specification-Version'   : '1',
                'Implementation-Title'    : project.name,
                'Implementation-Version'  : project.jar.archiveVersion,
                'Implementation-Vendor'   : mod_authors,
                'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
        ])
    }

    finalizedBy 'reobfJar'
}

publishing {
    publications {
        register('mavenJava', MavenPublication) {
            artifact jar
        }
    }
    repositories {
        maven {
            url "file://${project.projectDir}/mcmodsrepo"
        }
    }
}

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