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

val is120 = commonMod.minecraft_version.startsWith("1.20")

// Define repositories first so they are available for dependencies and mavenizer
repositories {
	maven("https://api.modrinth.com/maven") { name = "Modrinth" }
	maven {
        name = "CurseMaven"
		url = uri("https://www.cursemaven.com")
		content {
			includeGroup("curse.maven")
		}
	}
	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
	)
    // REMOVED: mappings("official", commonMod.minecraft_version) -- this was overriding the block above

	//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())
            // This ensures the implementation dependencies are on the runtime classpath
            lazy {
                val jadeVersion = if (is120) commonMod.prop("jade_version_1_20_1") else commonMod.prop("jade_version_1_21_1")
                property("forge.enabledGameTestNamespaces", commonMod.id)
            }
        }

		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")}"))

    // Jade - Use fg.deobf to ensure the obfuscated mod jar is remapped for the dev environment
    if (is120) {
        val jadeDep = fg.deobf("curse.maven:jade-324717:6855440")
        implementation(jadeDep)
        compileOnly(jadeDep)
    } else {
        val jadeVersion = commonMod.prop("jade_version_1_21_1")
        val jadeDep = fg.deobf("maven.modrinth:jade:$jadeVersion+forge")
        implementation(jadeDep)
        compileOnly(jadeDep)
    }

    // 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}"
}

    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
}