import net.minecraftforge.gradle.common.util.Utils
import net.minecraftforge.gradle.common.util.VersionJson
import org.jetbrains.gradle.ext.*

buildscript {
    repositories {
        mavenLocal()
        maven { url = 'https://maven.minecraftforge.net' }
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath 'net.minecraftforge.gradle:ForgeGradle:FORGE_GRADLE+'
        classpath "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext:1.1.3"
    }
}

apply plugin: 'eclipse'
apply plugin: 'net.minecraftforge.gradle.patcher'
apply plugin: "org.jetbrains.gradle.plugin.idea-ext"

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

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

configurations {
    shade
    compile.extendsFrom shade
}

group = 'me.yourname'
version = '1.0.0'

ext {
    minecraft_version = 'MINECRAFT_VERSION'
    mcp_version = 'MCP_VERSION'
    mappings_channel = 'MAPPINGS_CHANNEL'
    mappings_version = 'MAPPINGS_VERSION'
    spi_version = 'SPI_VERSION'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'net.minecraftforge:forgespi:' + spi_version
//    Use the shade to add the lib to the jar
//    or use compile if you want to load the lib from the version.json
//    from a maven repo
//    shade 'package-here'
//    compile 'package-here'
}

project(':mcp') {
    apply plugin: 'net.minecraftforge.gradle.mcp'
    mcp {
        config = minecraft_version + '-' + mcp_version
        pipeline = 'joined'
    }
}

evaluationDependsOn(':mcp')

patcher {
    parent = project(':mcp')
    patchedSrc = file('src/main/java')
    mappings channel: mappings_channel, version: mappings_version
    mcVersion = minecraft_version
}

jar {
    configurations.shade.each { dep ->
        from(project.zipTree(dep)) {
            exclude 'META-INF', 'META-INF/**'
        }
    }
}

var assetsFolder = file('run/assets')

tasks.register('runClient', JavaExec) {
    group = "MCP"
    description = "Runs the client"
    classpath sourceSets.main.runtimeClasspath
    if (System.getProperty("os.name").toLowerCase().contains("mac")) {
        jvmArgs '-XstartOnFirstThread'
    }

    var runFolder = file('run')
    var versionJson = Utils.loadJson(downloadAssets.getMeta().get().getAsFile(), VersionJson.class)

    workingDir runFolder
    args '--gameDir', '.'
    args '--version', minecraft_version
    args '--assetsDir', runFolder.relativePath(assetsFolder)
    args '--assetIndex', versionJson.assetIndex.id
    args '--accessToken', '0'
    main 'net.minecraft.client.main.Main'
}


tasks.register('setup') {
    group = "MCP"
    description = "Setups the dev workspace"
    dependsOn ':extractMapped'
    mkdir assetsFolder
    copy {
        from downloadAssets.output.path
        into assetsFolder
    }
}

tasks.register('copyAssets') {
    group = "MCP"
    description = "Download and place the assets into the run folder"
    dependsOn ':downloadAssets'
    mkdir 'run/assets'
    copy {
        from downloadAssets.output.path
        into assetsFolder
    }
}

tasks.register('runServer', JavaExec) {
    mkdir 'runserver'
    group = "MCP"
    description = "Runs the server"
    standardInput = System.in
    classpath sourceSets.main.runtimeClasspath
    main 'net.minecraft.server.Main'
    workingDir 'runserver'
}

idea.project.settings {
    runConfigurations {
        "Minecraft"(Application) {
            mainClass = 'mcp.client.Start'
            workingDirectory = "$projectDir/run"
            moduleName = idea.module.name + '.main'
        }
    }
}