plugins {
	id 'net.fabricmc.fabric-loom' version "${loom_version}"
	id 'maven-publish'
}

version = project.mod_version
group = project.maven_group
base.archivesName = project.archives_base_name

repositories {
	maven { url = 'https://maven.terraformersmc.com/' }
	maven { url = 'https://maven.shedaniel.me/' }
}

dependencies {
	minecraft "com.mojang:minecraft:${project.minecraft_version}"
	implementation "net.fabricmc:fabric-loader:${project.loader_version}"
	implementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_api_version}"
	compileOnly "com.terraformersmc:modmenu:${project.modmenu_version}"
	compileOnly "me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}"
}

processResources {
	def ver = project.version
	inputs.property 'version', ver
	filesMatching('fabric.mod.json') {
		expand 'version': ver
	}
}

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

java {
	withSourcesJar()
	sourceCompatibility = JavaVersion.VERSION_25
	targetCompatibility = JavaVersion.VERSION_25
}

tasks.named('sourcesJar', org.gradle.api.tasks.bundling.Jar).configure {
	exclude 'AGENTS.md'
	exclude '**/AGENTS.md'
}

jar {
	from('LICENSE') {
		rename { "${it}_${project.archives_base_name}" }
	}
	from('README.md')
	exclude 'AGENTS.md'
	exclude '**/AGENTS.md'
	manifest {
		attributes(
			'Specification-Title': 'STEN Vault',
			'Specification-Vendor': 'sten.cafe',
			'Specification-Version': project.version,
			'Implementation-Title': 'STEN Vault',
			'Implementation-Vendor': 'sten.wtf',
			'Implementation-Version': project.version,
			'Implementation-URL': 'https://sten.wtf',
			'Built-By': 'sten.cafe',
			'Fabric-Loom-Version': project.loom_version,
		)
	}
}

tasks.register('printJarHash') {
	dependsOn jar
	doLast {
		def built = tasks.named('jar').get().archiveFile.get().asFile
		def digest = java.security.MessageDigest.getInstance('SHA-256')
		built.withInputStream { stream ->
			def buf = new byte[8192]
			int n
			while ((n = stream.read(buf)) > 0) digest.update(buf, 0, n)
		}
		def hex = digest.digest().collect { String.format('%02x', it) }.join()
		println "STEN_VAULT_OFFICIAL_HASHES=${hex}"
		println built
	}
}

publishing {
	publications {
		create('mavenJava', MavenPublication) {
			from components.java
		}
	}
}
