aboutsummaryrefslogtreecommitdiff
path: root/installMinecraft.init.gradle.kts
blob: bf28e9e537f66123596a9d826764b76faba84ef3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
 * Install a mod into your minecraft mod folder using a gradle task from any mod you are editing.
 * This requires you to set the global property `minecraft.modfolder` in your global gradle.properties.
 * Afterwards you can run `./gradlew installToMinecraft` (or ./gradlew :subproject:installToMinecraft)
 * to install a mod into your minecraft mod folder while deleting old versions of that mod. This isn't
 * based on mod ids, but instead on archiveBaseName, so you will need to have a jar task that behaves
 * normally.
 * Will only work if it detects architectury loom, fabric loom, essentials loom, forge gradle or unimined.
 * Will prefer a remapJar task, or otherwise use the shadowJar or jar tasks (for forge gradle).
 */

allprojects {
    this.afterEvaluate {
        if (!listOf(
                "gg.essential.loom",
                "cc.polyfrost.loom",
                "dev.architectury.loom",
                "fabric-loom",
                "net.minecraftforge.gradle.tweaker-client",
                "net.minecraftforge.gradle.forge",
                "xyz.wagyourtail.unimined",
            ).any { plugins.hasPlugin(it) }
        ) {
            return@afterEvaluate
        }
        val toInstall =
            listOf("remapJar", "shadowJar", "jar")
                .mapNotNull {
                    tasks.findByName(it)
                }.filterIsInstance<org.gradle.jvm.tasks.Jar>()
                .firstOrNull()
        val targetDirectory = File(project.findProperty("minecraft.modfolder") as? String ?: return@afterEvaluate)
        if (toInstall != null) {
            tasks.create("installToMinecraft") {
                dependsOn(toInstall)
                doLast {
                    val modJar = toInstall.archiveFile.get().asFile
                    targetDirectory
                        .listFiles()
                        .filter {
                            it.name.startsWith(toInstall.archiveBaseName.get())
                        }.forEach {
                            it.delete()
                        }
                    modJar.copyTo(targetDirectory.resolve(modJar.name))
                    println("Installed $modJar to $targetDirectory")
                }
            }
        }
    }
}