buildscript {
    repositories {
        maven { url = 'https://files.minecraftforge.net/maven' }
        maven { url = 'https://repo.spongepowered.org/maven' }
        mavenCentral()
        maven { url = 'https://maven.izzel.io/releases' }
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: "${forge_gradle_version}"
        classpath 'org.spongepowered:mixingradle:0.7-SNAPSHOT'
        classpath "io.izzel.arclight:arclight-gradle-plugin:$agpVersion"
    }
}

apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.spongepowered.mixin'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'io.izzel.arclight'
apply plugin: 'maven-publish'

arclight {
    mcVersion = minecraftVersion
    forgeVersion = project.ext.forgeVersion
    bukkitVersion = 'v1_16_R3'
    wipeVersion = true
    reobfVersion = true
    accessTransformer = project(':arclight-common').file('bukkit.at')
    packageName = 'spigot'
}

sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

sourceSets {
    applaunch {
        java {
            compileClasspath += main.output
            runtimeClasspath += main.output
        }
    }
}

configurations {
    embed
    implementation.extendsFrom(embed)
}

minecraft {
    mappings channel: 'snapshot', version: "20210215-1.16.3"
    accessTransformer = project(':arclight-common').file('src/main/resources/META-INF/accesstransformer.cfg')
}

repositories {
    maven {
        name = 'sponge-repo'
        url = 'https://repo.spongepowered.org/maven'
    }
    maven { url = 'https://oss.sonatype.org/content/repositories/snapshots/' }
    maven { url = 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' }
    maven { url = 'https://files.minecraftforge.net/maven/' }
    maven { url = 'https://jitpack.io/' }
    maven { url = 'https://maven.izzel.io/releases' }
}

def embedLibs = [/*'org.spongepowered:mixin:0.8.1',*/ 'org.ow2.asm:asm-util:9.0',
                 'org.ow2.asm:asm-analysis:9.0', 'org.yaml:snakeyaml:1.27',
                 'org.xerial:sqlite-jdbc:3.34.0', 'mysql:mysql-connector-java:5.1.49',
                 'commons-lang:commons-lang:2.6', 'com.googlecode.json-simple:json-simple:1.1.1',
                 'org.apache.logging.log4j:log4j-jul:2.11.2', 'net.md-5:SpecialSource:1.9.0',
                 'org.jline:jline-terminal-jansi:3.12.1', 'org.fusesource.jansi:jansi:1.18',
                 'org.jline:jline-terminal:3.12.1', 'org.jline:jline-reader:3.12.1',
                 'jline:jline:2.12.1', 'org.apache.maven:maven-resolver-provider:3.8.1',
                 'org.apache.maven.resolver:maven-resolver-connector-basic:1.6.2', 'org.apache.maven.resolver:maven-resolver-transport-http:1.6.2',
                 'org.apache.maven:maven-model:3.8.1', 'org.codehaus.plexus:plexus-utils:3.2.1',
                 'org.apache.maven:maven-model-builder:3.8.1', 'org.codehaus.plexus:plexus-interpolation:1.25',
                 'org.eclipse.sisu:org.eclipse.sisu.inject:0.3.4', 'org.apache.maven:maven-builder-support:3.8.1',
                 'org.apache.maven:maven-repository-metadata:3.8.1', 'org.apache.maven.resolver:maven-resolver-api:1.6.2',
                 'org.apache.maven.resolver:maven-resolver-spi:1.6.2', 'org.apache.maven.resolver:maven-resolver-util:1.6.2',
                 'org.apache.maven.resolver:maven-resolver-impl:1.6.2', 'org.apache.httpcomponents:httpclient:4.5.12',
                 'org.apache.httpcomponents:httpcore:4.4.13', 'commons-codec:commons-codec:1.11',
                 'org.slf4j:jcl-over-slf4j:1.7.30']

dependencies {
    minecraft "net.minecraftforge:forge:$minecraftVersion-$forgeVersion"
    implementation group: 'org.jetbrains', name: 'annotations', version: '19.0.0'
    implementation(project(':arclight-common')) {
        exclude module: 'forge'
    }
    embed project(':i18n-config')
    embed project(':forge-installer')
    for (def lib : embedLibs) {
        arclight lib
    }
    embed('org.apache.logging.log4j:log4j-slf4j-impl:2.8.1') {
        exclude module: 'log4j-api'
    }
    embed 'net.md-5:bungeecord-chat:1.16-R0.4@jar'
    embed "org.spigotmc:spigot-api:$minecraftVersion-R0.1-SNAPSHOT@jar"
    embed 'com.github.ArclightTeam:mixin-tools:1.0.0@jar'
    embed "io.izzel:tools:$toolsVersion"
    embed "io.izzel.arclight:arclight-api:$apiVersion"
    annotationProcessor 'org.spongepowered:mixin:0.8.2:processor'
    annotationProcessor 'com.github.ArclightTeam:mixin-tools:1.0.0'
}

def getGitHash = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'rev-parse', '--short', 'HEAD'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

processResources {
    filesMatching("**/mods.toml") {
        expand 'version': "$minecraftVersion-${project.version}-${getGitHash()}"
    }
}

jar {
    manifest.attributes 'MixinConnector': 'io.izzel.arclight.impl.ArclightConnector_1_16'
    manifest.attributes 'Main-Class': 'io.izzel.arclight.server.Launcher'
    manifest.attributes 'Implementation-Title': 'Arclight'
    manifest.attributes 'Implementation-Version': "arclight-$minecraftVersion-${project.version}-${getGitHash()}"
    manifest.attributes 'Implementation-Vendor': 'Arclight Team'
    manifest.attributes 'Implementation-Timestamp': new Date().format("yyyy-MM-dd HH:mm:ss")
    manifest.attributes 'Add-Opens': 'java.base/sun.security.util java.base/java.util.jar'
    manifest.attributes 'Add-Exports': 'java.base/sun.security.util'
    from(configurations.embed.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/MANIFEST.MF"
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
        exclude "LICENSE.txt"
    }
    from(project(':arclight-common').tasks.jar.outputs.files.collect { it.isDirectory() ? it : zipTree(it) })
    from sourceSets.applaunch.output.classesDirs
}

remapSpigotJar {
    includes.add('net/minecraft/block/ChestBlock$DoubleInventory')
    includes.add('net/minecraft/tileentity/LecternTileEntity$LecternInventory')
}

processMapping {
    process { csrg, srg ->
        csrg.classes.put('net/minecraft/world/level/ColorResolver', "net/minecraft/server/$bukkitVersion/ColorResolver".toString())
    }
}

mixin {
    add sourceSets.main, 'mixins.arclight.impl.refmap.1_16.json'
}

compileJava {
    options.compilerArgs << '-XDignore.symbol.file' << '-XDenableSunApiLintControl'
    options.encoding = 'UTF-8'
}

compileApplaunchJava {
    sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_6
}

task srgJar(type: Jar) {
    from(tasks.jar.outputs.files.collect { it.isDirectory() ? it : zipTree(it) }) {
        include 'io/izzel/**'
        exclude 'io/izzel/arclight/common/mixin/**'
    }
    classifier = 'srg'
}

task spigotJar(type: Jar) {
    with tasks.srgJar
    doLast {
        def task = tasks.create('renameSpigotJar', net.minecraftforge.gradle.userdev.tasks.RenameJarInPlace)
        task.input = archivePath
        task.hasLog = false
        task.mappings = project(':arclight-common').file('build/arclight_cache/tmp_srg/bukkit_srg.srg')
        def args = task.args.toList()
        args.add('--reverse')
        task.setArgs(args)
        task.apply()
    }
    classifier = 'spigot'
}

task sourceJar(type: Jar) {
    from(sourceSets.main.allSource)
    for (def s in [':i18n-config', ':arclight-common', ':forge-installer']) {
        from(project(s).sourceSets.main.allSource) {
            exclude 'io/izzel/arclight/common/mixin/**'
        }
    }
    classifier = 'sources'
}

publishing {
    repositories {
        maven {
            name = "IzzelAliz"
            url = uri('https://maven.izzel.io/' + (project.version.toString().endsWith('SNAPSHOT') ? 'snapshots' : 'releases'))
            credentials {
                username = project.findProperty("mavenUser") ?: System.getenv("ARCLIGHT_USER")
                password = project.findProperty("mavenToken") ?: System.getenv("ARCLIGHT_TOKEN")
            }
        }
    }
    publications {
        mavenJava(MavenPublication) {
            artifact srgJar
            artifact spigotJar
            artifact sourceJar
        }
    }
}

if ('true'.equalsIgnoreCase(System.getenv('APPVEYOR_REPO_TAG'))) {
    tasks.build.dependsOn(tasks.publish)
}
