buildscript {
    repositories {
        mavenCentral()
		maven { url "file:///${project.projectDir}/deps/" }
        maven { url = 'https://maven.minecraftforge.net' }
        maven { url = 'https://dist.creeper.host/Sponge/maven' }
        maven { url 'https://plugins.gradle.org/m2/' }
        maven {
            url = 'https://repo.spongepowered.org/repository/maven-public/'
            content { includeGroup "org.spongepowered" }
        }
		maven {
			name "Gradle Plugin Portal"
			url 'https://plugins.gradle.org/m2/'
			content { 
				includeGroup "gradle.plugin.com.matthewprenger"
				includeGroup "com.modrinth"
			}
		}
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '[6.0.36,6.2)'
		classpath 'gradle.plugin.com.matthewprenger:CurseGradle:1.4.0'
        classpath 'org.spongepowered:mixingradle:0.7.+'
    }
}

plugins {
    id "com.modrinth.minotaur" version "2.+"
}

println("Applying plugins...")

apply plugin: 'net.minecraftforge.gradle'
apply plugin: 'org.spongepowered.mixin'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'maven-publish'
apply plugin: 'com.matthewprenger.cursegradle'

apply from: '../common.gradle'

println("Specifying project version/group/archivesBaseName...")
version = "${mod_version}"
group = "${maven_group}"
archivesBaseName = "${archives_base_name}"

println("Specifying Java compatibility...")
java.toolchain.languageVersion = JavaLanguageVersion.of(21)

println("Java Installation Data:")
println(" - Java Version: " + System.getProperty('java.version'))
println(" - JVM Version: " + System.getProperty('java.vm.version'))
println(" - Vendor: " + System.getProperty('java.vendor'))
println(" - OS Archetype: " + System.getProperty('os.arch'))

compileJava.options.encoding = "UTF-8"

mixin {
	println("Specifying refmaps for Mixin...")
    add sourceSets.main, "nochatreports.refmap.json"
	config "mixins/common/nochatreports.mixins.json"
	config "mixins/forge/nochatreports-forge.mixins.json"
	defaultObfuscationEnv "searge"
}

minecraft {
	println("Specifying MCP mappings...")
    mappings channel: 'official', version: minecraft_version
	
	println("Specifying Access Transformer configuration...")	
	accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
	
    reobf = false
    copyIdeResources = true
	
	runs {
		 configureEach {
            workingDirectory project.file('run')
            property 'forge.logging.markers', 'REGISTRIES'
            property 'forge.logging.console.level', 'debug'
        }

	
        client {
			properties 'org.gradle.jvmargs': '-Xms4G -Xmx4G'
			properties 'fml.earlyprogresswindow': 'false'
			properties 'mixin.env.disableRefMap': 'true'
			
			property 'forge.enabledGameTestNamespaces', mod_id
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
        }

        server {
			properties 'mixin.env.disableRefMap': 'true'
			property 'forge.enabledGameTestNamespaces', mod_id
            property 'forge.logging.markers', 'SCAN,REGISTRIES,REGISTRYDUMP'
            property 'forge.logging.console.level', 'debug'
            args '--nogui'
        }
    }
}

sourceSets.main.resources {
    // NO-OP
}

repositories {
	println("Specifying repositories...")	
	maven {  url "file:///${project.projectDir}/deps/"    } // local
	maven {  url = "https://maven.theillusivec4.top/"     } // curios
	maven {  url = "https://maven.blamejared.com/"        } // patchouli
	maven {  url = "https://repo.spongepowered.org/maven" } // mixin
	maven {  url "https://maven.shedaniel.me/"            } // cloth config
}

dependencies {
	println("Processing dependencies...")	

	// Minecraft & Forge
    minecraft "net.minecraftforge:forge:${forge_version}"
	implementation "generic:modmenu:5.0.2"
	implementation "generic:fabric-annotations:v1.0.0"
	implementation "generic:cloth-config-api:14.0.126"
	annotationProcessor "org.spongepowered:mixin:0.8.5:processor"
	
	// Hack fix for now, force jopt-simple to be exactly 5.0.4 because Mojang ships that version, but some transtive dependencies request 6.0+ 
    implementation('net.sf.jopt-simple:jopt-simple:5.0.4') { version { strictly '5.0.4' } }
}


jar {
	println("Processing manifest...")	

    manifest {
        attributes([
            "Specification-Title": "${mod_name}",
            "Specification-Vendor": "${mod_author}",
            "Specification-Version": "${version}",
            "Implementation-Title": "${mod_name}",
            "Implementation-Version": "${version}",
            "Implementation-Vendor" :"${mod_author}",
            "Implementation-Timestamp": new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"),
            "MixinConfigs": "mixins/common/nochatreports.mixins.json, mixins/forge/nochatreports-forge.mixins.json"
        ])
    }
}

processResources {
	println("Processing resources...")
	duplicatesStrategy = DuplicatesStrategy.EXCLUDE
	exclude 'META-INF/mods.toml'

    from("${project.projectDir}/docs/META-INF") {
		println("Inflating mods.toml...")	
	
		// Replace properties in mods.toml with ones derived from settings.gradle
        include 'mods.toml'
        expand 'mod_license': mod_license, 
		'mod_version': version, 
		'mod_id' : mod_id, 
		'mod_name': mod_name, 
		'mod_url': mod_url, 
		'mod_author': mod_author, 
		'mod_icon': mod_icon, 
		'mod_description': mod_description,
		'issue_tracker_url': issue_tracker_url,
		'dep_forge': dep_forge,
		'dep_minecraft': dep_minecraft,
		'contributor_0': contributor_0
		into 'META-INF'
    }

    from("${rootProject.projectDir}/docs") {
        include 'CHANGELOG.md'
    }
	
	from("${rootProject.projectDir}") {
        include 'LICENSE'
	}
}

curseforge {

    project {
		println("Pushing CurseGradle specifications for project ID: ${curse_id}")	
	
		// Specified as cmd argument
        apiKey = findProperty('curseKey') ?: 'none'
        
		id = "${curse_id}"
        releaseType = "${version_type}"
        changelogType = 'markdown'
        changelog = rootProject.file('docs/CHANGELOG.md')
		
		addGameVersion project.minecraft_version
		addGameVersion "1.21.9"
		addGameVersion "Java 21"
		addGameVersion "Forge"
		
        //addArtifact(sourcesJar)
        //addArtifact(apiJar)
    }
}

modrinth {
    token = findProperty('modrinthKey') ?: 'none'
    projectId = "no-chat-reports"
    versionNumber = "Forge-" + project.version
	versionName = "No Chat Reports Forge-" + project.version
    versionType = "${version_type}"
	changelog = rootProject.file("docs/CHANGELOG.md").text
    uploadFile = jar
    gameVersions = [ project.minecraft_version, "1.21.9" ]
	
	syncBodyFrom = rootProject.file("docs/README.md").text
}

task upload {
	dependsOn('modrinth')
	dependsOn('curseforge')
}


task updateResources() {
    // NO-OP
}

tasks.updateResources.dependsOn processResources

//tasks.withType(JavaCompile) {
//    options.failOnError(true)
//}

//jar.finalizedBy('reobfJar') 
//publish.dependsOn('reobfJar')

publishing {

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

sourceSets.each {
    def dir = layout.buildDirectory.dir("sourceSets/$it.name")
    it.output.resourcesDir = dir
    it.java.destinationDirectory = dir
}