plugins {
	`multiloader-loader`
	id("net.minecraftforge.gradle")
	id("net.minecraftforge.jarjar")
	// id("net.minecraftforge.accesstransformers")
}

repositories {
	mavenCentral()
	maven("https://maven.architectury.dev/")
	maven("https://maven.terraformersmc.com/releases/")
	maven("https://dl.cloudsmith.io/public/geckolib3/geckolib/maven/")
	maven("https://maven.blamejared.com/")
}

minecraft.mavenizer(repositories)

println(
	"Java: ${providers.systemProperty("java.version").get()}, " +
		"JVM: ${providers.systemProperty("java.vm.version").get()} (${
			providers.systemProperty("java.vendor").get()
		}), " +
		"Arch: ${providers.systemProperty("os.arch").get()}"
)

minecraft {
	/*mappings(
		if (commonMod.propOrNull("parchment_mappings") != null) "parchment" else "official",
		if (commonMod.propOrNull("parchment_mappings") != null)
			"${commonMod.minecraft_version}-${commonMod.prop("parchment_mappings")}" else commonMod.minecraft_version
	)*/
	mappings("official", commonMod.minecraft_version)

	//setAccessTransformers(true)

	runs {
		configureEach {
			workingDir.convention(layout.projectDirectory.dir("run"))

			//systemProperty ("forge.logging.markers", "REGISTRIES")

			systemProperty("forge.logging.console.level", "debug")

			systemProperty("eventbus.api.strictRuntimeChecks", "true")

			//args ("-mixin.config=${commonMod.id}.mixins.json")

			//classpath(sourceSets.main.get())

			systemProperty("architectury.runtime.enduser", "false")//
			systemProperty("mixin.env.remapRefMap", "true")//
		}

		register("client") {
			systemProperty("forge.enabledGameTestNamespaces", commonMod.id)
		}

		register("server") {
			systemProperty("forge.enabledGameTestNamespaces", commonMod.id)
			args("--nogui")
		}

		register("gameTestServer") {
			systemProperty("forge.enabledGameTestNamespaces", commonMod.id)
		}

		register("data") {
			workingDir = layout.projectDirectory.dir("run-data")

			args(
				"--mod",
				commonMod.id,
				"--all",
				"--output",
				layout.projectDirectory.dir("src/generated/resources"),
				"--existing",
				layout.projectDirectory.dir("src/main/resources")
			)
		}
	}
}

// Include resources generated by data generators.
sourceSets.main {
	resources.srcDir(layout.projectDirectory.dir("src/generated/resources"))
}

// This methods registers jarJar for the default jar task.
// The closure allows you to configure the task, instead of needing to do this:
jarJar.register() {
	archiveClassifier = null
}

dependencies {
	// Specify the version of Minecraft to use.
	// Any artifact can be supplied so long as it has a "userdev" classifier artifact and is a compatible patcher artifact.
	// The "userdev" classifier will be requested and setup by ForgeGradle.
	// If the group id is "net.minecraft" and the artifact id is one of ["client", "server", "joined"],
	// then special handling is done to allow a setup of a vanilla dependency without the use of an external repository.
	implementation(minecraft.dependency("net.minecraftforge:forge:${commonMod.minecraft_version}-${commonMod.prop("minecraftforge_version")}"))

	// Forge 1.21.6+ uses EventBus 7, which shifts most of its runtime validation to compile-time via an annotation processor
	// to improve performance in production environments. This line is required to enable said compile-time validation
	// in your development environment, helping you catch issues early.
	if (stonecutter.eval(stonecutter.current.version, ">=1.21.6"))
		annotationProcessor("net.minecraftforge:eventbus-validator:${commonMod.prop("minecraftforge_eventbus_validator_version")}")

	// Example mod dependency with JEI
	// The JEI API is declared for compile time use, while the full JEI artifact is used at runtime
	//compileOnly "mezz.jei:jei-${mc_version}-common-api:${jei_version}"
	//compileOnly "mezz.jei:jei-${mc_version}-forge-api:${jei_version}"
	//runtimeOnly "mezz.jei:jei-${mc_version}-forge:${jei_version}"

	// Example mod dependency using a mod jar from ./libs with a flat dir repository
	// This maps to ./libs/coolmod-${mc_version}-${coolmod_version}.jar
	// The group id is ignored when searching -- in this case, it is "blank"
	// NOTE: Support for deobfuscated dependencies has not yet been added in ForgeGradle 7.
	//implementation "blank:coolmod-${mc_version}:${coolmod_version}"

	// For more info:
	// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
	// http://www.gradle.org/docs/current/userguide/dependency_management.html

	// Architectury & GeckoLib
	implementation("dev.architectury:architectury-forge:${commonMod.prop("architectury_version")}")

	// Explicitly add the transformer runtime JAR to the dependencies for the development environment.
	implementation("dev.architectury:architectury-transformer:5.2.88:runtime")

	implementation("software.bernie.geckolib:geckolib-forge-${commonMod.minecraft_version}:${commonMod.prop("geckolib_version")}")

	// JEI (Forge)
	// Compile against the API
	compileOnly("mezz.jei:jei-${commonMod.minecraft_version}-common-api:${commonMod.prop("jei_version")}")
	compileOnly("mezz.jei:jei-${commonMod.minecraft_version}-forge-api:${commonMod.prop("jei_version")}")
	// Run with the full mod
	runtimeOnly("mezz.jei:jei-${commonMod.minecraft_version}-forge:${commonMod.prop("jei_version")}")
}

tasks.withType(JavaCompile::class).configureEach {
	options.encoding = "UTF-8" // Use the UTF-8 charset for Java compilation
}

tasks {
	processResources {
		exclude("${mod.id}.accesswidener")
	}

	register<Copy>("copyAT") {
		val atFile =
			project(":common").file("src/main/resources/accesstransformers/accesstransformer-${commonMod.minecraft_version}.cfg")
		from(atFile) {
			rename("accesstransformer-${commonMod.minecraft_version}.cfg", "accesstransformer.cfg")
		}
		setDuplicatesStrategy(DuplicatesStrategy.INHERIT)
		into("src/main/resources/META-INF")
		mustRunAfter(common.project.tasks.getByName("stonecutterMerge"))
	}
}

tasks.named("stonecutterPrepare") {
	finalizedBy(tasks.named("copyAT"))
}

sourceSets.forEach {
	val dir = layout.buildDirectory.dir("sourcesSets/$it.name")
	it.output.setResourcesDir(dir)
	it.java.destinationDirectory = dir
}