plugins {
	id 'idea'
	id 'java'
	id 'fabric-loom' version '1.10-SNAPSHOT'
	id 'maven-publish'
}

version = project.plugin_version
group = project.maven_group
archivesBaseName = project.plugin_name

configurations {
	rusherhackApi
	compileOnly.extendsFrom(rusherhackApi)
	productionRuntime {
		extendsFrom configurations.minecraftLibraries
		extendsFrom configurations.loaderLibraries
		extendsFrom configurations.minecraftRuntimeLibraries
	}
}

repositories {
	// Add repositories to retrieve artifacts from in here.
	// Loom adds the essential maven repositories to download Minecraft and libraries from automatically.
	// See https://docs.gradle.org/current/userguide/declaring_repositories.html
	// for more information about repositories.

	maven {
		name = "rusherhack"
		//releases repository will have the latest api version for last stable rusherhack release
		//snapshots will always be the latest api version
		//url = "https://maven.rusherhack.org/releases"
		url = "https://maven.rusherhack.org/snapshots"
	}

	maven {
		name = 'ParchmentMC'
		url = 'https://maven.parchmentmc.org'
	}
}

dependencies {
	minecraft "com.mojang:minecraft:${project.minecraft_version}"
	productionRuntime(modImplementation("net.fabricmc:fabric-loader:${project.fabric_loader_version}"))
	productionRuntime("net.fabricmc:intermediary:${project.minecraft_version}")

	//mojmap + parchment mappings
	mappings loom.layered() {
		officialMojangMappings()
		parchment("org.parchmentmc.data:parchment-${project.parchment_version}@zip")
	}

	//rusherhack api
	rusherhackApi "org.rusherhack:rusherhack-api:$minecraft_version-SNAPSHOT"
}

tasks.register('updateAccessWidener', Copy) {
	group = "build"
	outputs.upToDateWhen { false }

	def accessWidenerFile = zipTree(this.project.configurations.rusherhackApi.singleFile).matching {
		include "rusherhack.accesswidener"
	}.files.iterator().next()

	println("rusherhack access widener: " + accessWidenerFile)

	from(accessWidenerFile)
	into(file("build/tmp/" + this.project.minecraft_version))
}

tasks.register('runPlugin', JavaExec) {
	group = "build"
	dependsOn remapJar, downloadAssets, copyPluginToRunDir
	classpath.from configurations.productionRuntime
	mainClass = "net.fabricmc.loader.impl.launch.knot.KnotClient"
	workingDir = file("run")

	doFirst {
		classpath.from loom.minecraftProvider.minecraftClientJar
		workingDir.mkdirs()

		args(
				"--assetIndex", loom.minecraftProvider.versionInfo.assetIndex().fabricId(loom.minecraftProvider.minecraftVersion()),
				"--assetsDir", new File(loom.files.userCache, "assets").absolutePath,
				"--gameDir", workingDir.absolutePath
		)

		def rusherLoaderJarFile = project.layout.getProjectDirectory().file("lib/rusherhack-loader.jar").asFile
		if (!rusherLoaderJarFile.exists()) {
			throw new GradleException("rusherhack-loader.jar must be copied to the lib directory!")
		}
		def rusherLoaderJarPath = project.layout.getProjectDirectory().file("lib/rusherhack-loader.jar").asFile.absolutePath

		jvmArgs(
				"-Drusherhack.enablePlugins=true",
				"-Dfabric.addMods=${rusherLoaderJarPath}",
		)
	}
}

tasks.register("copyPluginToRunDir", Copy) {
	group = "build"
	dependsOn remapJar
	from remapJar.outputs
	into file("run/rusherhack/plugins")
}

loom {
	def accessWidenerFile = new File("build/tmp/${this.project.minecraft_version}/rusherhack.accesswidener")
	if(accessWidenerFile.exists()) {
		accessWidenerPath = accessWidenerFile
	} else {
		println("RUSHERHACK ACCESS WIDENER NOT FOUND. Please run the 'updateAccessWidener' task.")
	}

	//disable run configs
	runConfigs.configureEach {
		ideConfigGenerated = false
	}

	mixin {
		//defaultRefmapName = "mixins.${this.project.plugin_name}-refmap.json"
		useLegacyMixinAp = false
	}
}

def targetJavaVersion = project.java_version

java.sourceCompatibility = JavaLanguageVersion.of(targetJavaVersion)
java.targetCompatibility = JavaLanguageVersion.of(targetJavaVersion)
java.toolchain.languageVersion.set(JavaLanguageVersion.of(targetJavaVersion))

tasks.withType(JavaCompile).configureEach {
	// ensure that the encoding is set to UTF-8, no matter what the system default is
	// this fixes some edge cases with special characters not displaying correctly
	// see http://yodaconditions.net/blog/fix-for-java-file-encoding-problems-with-gradle.html
	// If Javadoc is generated, this must be specified in that task too.
	it.options.encoding = "UTF-8"
	it.options.release = Integer.parseInt(targetJavaVersion)
}

processResources {
	filesMatching("rusherhack-plugin.json") {
		expand(project: project)
	}
}

jar {
	manifest {
		attributes(
				"Minecraft-Version": project.minecraft_version
		)
	}
}

//update access widener during intellij sync
this.tasks.ideaSyncTask.dependsOn("updateAccessWidener")
this.tasks.configureLaunch.dependsOn("updateAccessWidener")