// ====================================================================================================================
// AgriCraft Buildscript

// ====================================================================================================================

// --------------------------------------------------------------------------------------------------------------------
// Buildscript Dependencies
// --------------------------------------------------------------------------------------------------------------------

buildscript {
    repositories {
        maven { url = 'https://maven.minecraftforge.net' }
		maven { url = 'https://maven.parchmentmc.org' }
        jcenter()
        mavenCentral()
        flatDir{
            dirs 'lib'
        }
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '5.1.+', changing: true// MixinGradle:
        classpath 'org.spongepowered:mixingradle:0.7.+'
		classpath 'org.parchmentmc:librarian:1.+'
    }
}


// --------------------------------------------------------------------------------------------------------------------
// Buildscript Plugins
// --------------------------------------------------------------------------------------------------------------------
plugins {
    id 'eclipse'
    id 'maven-publish'
    id 'jacoco'
    id 'com.github.johnrengelman.shadow' version '7.1.2'
}


// --------------------------------------------------------------------------------------------------------------------
// ForgeGradle Plugin
// --------------------------------------------------------------------------------------------------------------------
apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.parchmentmc.librarian.forgegradle'
apply plugin: 'org.spongepowered.mixin'


// --------------------------------------------------------------------------------------------------------------------
// Mod Properties
// --------------------------------------------------------------------------------------------------------------------

// Load Mod Properties
ext.configFile = file('mod.properties')
ext.config = parseConfig(configFile)

// Mod Properties Transcription
version = "${config.version_minecraft}-${config.version_major}.${config.version_minor}.${config.version_patch}"
group = "${config.package}" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
archivesBaseName = "${config.id}"

// Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17
java.toolchain.languageVersion = JavaLanguageVersion.of(17)

// Mod Properties Loader
def parseConfig(File config) {
    config.withReader {
        def prop = new Properties()
        prop.load(it)
        return (new ConfigSlurper().parse(prop))
    }
}

// --------------------------------------------------------------------------------------------------------------------
// Minecraft Task
// --------------------------------------------------------------------------------------------------------------------
println('Java: ' + System.getProperty('java.version') + ' JVM: ' + System.getProperty('java.vm.version') + '(' + System.getProperty('java.vendor') + ') Arch: ' + System.getProperty('os.arch'))
minecraft {
    mappings channel: "${config.mapping_channel}", version: "${config.mapping_version}"

    // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
    // accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

    // Default run configurations.
    runs {
        client {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            // Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
            property 'forge.enabledGameTestNamespaces', 'agricraft'

            mods {
                agricraft {
                    source sourceSets.main
                }
                infinitylib {
                    //This still grabs the sourceSets from Agricraft, therefore one must manually correct it in runClient afterwards
                    source project("InfinityLib").sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            // Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
            property 'forge.enabledGameTestNamespaces', 'agricraft'

            mods {
                agricraft {
                    source sourceSets.main
                }
                infinitylib {
                    //This still grabs the sourceSets from Agricraft, therefore one must manually correct it in runClient afterwards
                    source project("InfinityLib").sourceSets.main
                }
            }
        }

        gameTestServer {
            workingDirectory project.file('run')

            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            // Comma-separated list of namespaces to load gametests from. Empty = all namespaces.
            property 'forge.enabledGameTestNamespaces', 'agricraft'

            mods {
                agricraft {
                    source sourceSets.main
                }
                infinitylib {
                    //This still grabs the sourceSets from Agricraft, therefore one must manually correct it in runClient afterwards
                    source project("InfinityLib").sourceSets.main
                }
            }
        }

        data {
            workingDirectory project.file('run')

            // Recommended logging data for a userdev environment
            property 'forge.logging.markers', 'REGISTRIES'

            // Recommended logging level for the console
            property 'forge.logging.console.level', 'debug'

            // Specify the modid for data generation, where to output the resulting resource, and where to look for existing resources.
            args '--mod', 'agricraft', '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')

            mods {
                agricraft {
                    source sourceSets.main
                }
                infinitylib {
                    //This still grabs the sourceSets from Agricraft, therefore one must manually correct it in runClient afterwards
                    source project("InfinityLib").sourceSets.main
                }
            }
        }
    }
}

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

repositories {
    maven {
        // for any curseforge mod
        url "https://cursemaven.com"
    }

    maven {
        // for JEI
        url "https://dvs1.progwml6.com/files/maven"
    }

    maven {
        // for Botania and Immersive Engineering
        url 'https://maven.blamejared.com'
    }

    maven {
        // for Mystical Agriculture
        url 'https://maven.blakesmods.com'
    }

    maven { // Registrate
        url "https://maven.tterrag.com/"
    }

    // For AgriCore dependencies
    mavenCentral()
}


// Add "embedded" configuration
configurations {
    shade
    implementation.extendsFrom shade
}

// List Deps.
dependencies {
    // Minecraft
    minecraft 'net.minecraftforge:forge:1.18.2-40.2.4'//"net.minecraftforge:forge:${config.version_minecraft}-${config.version_forge}"

    // Apply Mixin AP
    annotationProcessor 'org.spongepowered:mixin:0.8.5:processor'

    // AgriCore
    minecraftLibrary project(':AgriCore')
    shade project(':AgriCore')
    shade 'org.reflections:reflections:0.10.2'

    // InfinityLib
    implementation project(":InfinityLib")

    //JEI
    compileOnly fg.deobf("mezz.jei:jei-1.18.2:9.7.2.281")
    runtimeOnly fg.deobf("mezz.jei:jei-1.18.2:9.7.2.281")

    //Botania
    compileOnly fg.deobf("vazkii.botania:Botania:1.18.2-430:api")
    //runtimeOnly fg.deobf("vazkii.botania:Botania:1.18.2-430")

    //Create
    compileOnly fg.deobf("curse.maven:create-328085:4550986")  // 1.18.2-0.5.1b
    compileOnly fg.deobf("curse.maven:flywheel-486392:4341461")  // 1.18.2-0.6.8.a
    compileOnly fg.deobf("com.tterrag.registrate:Registrate:MC1.18.2-1.1.3")
    runtimeOnly fg.deobf("curse.maven:create-328085:4550986")
    runtimeOnly fg.deobf("curse.maven:flywheel-486392:4341461")

    //Cyclic
    compileOnly fg.deobf("curse.maven:cyclic-239286:3708026")
    //runtimeOnly fg.deobf("curse.maven:cyclic-239286:3708026")

    //Immersive Engineering
    compileOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:1.18.2-8.0.0-146.7")
//    runtimeOnly fg.deobf("blusunrize.immersiveengineering:ImmersiveEngineering:1.18.2-8.0.0-146.7")

    //Industrial Foregoing
    compileOnly fg.deobf("curse.maven:industrial_foregoing-266515:3817385")
    //runtimeOnly fg.deobf("curse.maven:industrial_foregoing-266515:3817385")
    //runtimeOnly fg.deobf("curse.maven:titanium-287342:3817355") // Industrial Foregoing runtime dependency 1.18.2-3.5.5-37

    //Jade
    compileOnly fg.deobf("curse.maven:jade-324717:3865918")
    //runtimeOnly fg.deobf("curse.maven:jade-324717:3865918")

    //Mystical Agriculture
    compileOnly fg.deobf("com.blakebr0.mysticalagriculture:MysticalAgriculture:1.18.2-5.1.0")
	//runtimeOnly fg.deobf("com.blakebr0.mysticalagriculture:MysticalAgriculture:1.18.2-5.1.0")
	//runtimeOnly fg.deobf("com.blakebr0.cucumber:Cucumber:1.18.2-5.1.0")   //Mystical Agriculture runtime dependency

    //Quark
    compileOnly fg.deobf("vazkii.quark:Quark:3.2-358.2422")
    compileOnly fg.deobf("vazkii.autoreglib:AutoRegLib:1.7-53.111")
//    runtimeOnly fg.deobf("vazkii.quark:Quark:3.2-358.2422")
//    runtimeOnly fg.deobf("vazkii.autoreglib:AutoRegLib:1.7-53.111")


    //Serene Seasons
    compileOnly fg.deobf("curse.maven:serene_seasons-291874:3693807")
    //runtimeOnly fg.deobf("curse.maven:serene_seasons-291874:3693807")

    //Straw Golem Reborn
    compileOnly fg.deobf("curse.maven:straw_golem_reborn-387580:3824741")
    //runtimeOnly fg.deobf("curse.maven:straw_golem_reborn-387580:3824741")

    //The One Probe
    compileOnly fg.deobf("curse.maven:theoneprobe-245211:3671753")
    //runtimeOnly fg.deobf("curse.maven:theoneprobe-245211:3671753")
}

// Delete Old Plants
task deleteOldJsons(type: Delete) {
    delete "run/config/agricraft/json"
}

processResources {
	// Add AgriPlants
    from('lib/AgriPlants') {
        include '**/*.json'
        into 'json/defaults'
    }
    outputs.upToDateWhen{ false }
}

jar {
    archiveClassifier = 'slim'
    // Manifest
    manifest {
        attributes([
                "Specification-Title"     : "${config.id}",
                "Specification-Vendor"    : "${config.author}",
                "Specification-Version"   : "1", // We are version 1 of ourselves
                "Implementation-Title"    : "${config.name}",
                "Implementation-Version"  : "${config.version_major}.${config.version_minor}.${config.version_patch}",
                "Implementation-Vendor"   : "${config.author}",
                "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ")
        ])
    }
}

shadowJar {
    archiveClassifier = ''
    configurations = [project.configurations.shade]
    relocate 'org.reflections', "com.infinityraider.agricraft.relocated.reflections"
    exclude 'org/slf4j', 'org/slf4j/**'
    exclude 'com/google', 'com/google/**'
    finalizedBy 'reobfShadowJar'
}
assemble.dependsOn shadowJar

reobf {
    shadowJar {}
}

// Example configuration to allow publishing using the maven-publish task
// This is the preferred method to reobfuscate your jar file
jar.finalizedBy('reobfJar')

task apiJar(type: Jar) {
    archiveClassifier.set('api')
    from(sourceSets.main.output)
    include 'com/infinityraider/agricraft/api/**'
}
task apiSrcJar(type: Jar) {
    archiveClassifier.set('api-sources')
    from sourceSets.main.java
    include 'com/infinityraider/agricraft/api/**'
}

artifacts {
    archives apiJar, apiSrcJar
}

test {
    finalizedBy jacocoTestReport // report is always generated after tests run
}

jacocoTestReport {
    dependsOn test // tests are required to run before generating the report
    reports {
        xml.enabled true
        html.enabled false
    }
}
