summaryrefslogtreecommitdiff
path: root/plugin/src/main/kotlin/moe/nea/archenemy/mojang/ArchenemyMojangExtension.kt
blob: 81259007869c9caf299108d86846c9fe72ccb5f3 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package moe.nea.archenemy.mojang

import moe.nea.archenemy.MCSide
import net.minecraftforge.artifactural.gradle.GradleRepositoryAdapter
import org.gradle.api.Project
import org.gradle.api.artifacts.Dependency
import org.gradle.api.artifacts.ModuleDependency
import java.io.File
import java.net.URI

abstract class ArchenemyMojangExtension(val project: Project) {
    val sharedExtension = project.rootProject.extensions.getByType(ArchenemySharedExtension::class.java)

    private val _registerMinecraftProvider by lazy {
        GradleRepositoryAdapter.add(
            project.repositories,
            "Minecraft Provider",
            sharedExtension.getLocalCacheDirectory().resolve("minecraft-provider"),
            sharedExtension.minecraftProvider
        )
        GradleRepositoryAdapter.add(
            project.repositories,
            "Minecraft Mapped Provider",
            getLocalCacheDirectory().resolve("minecraft-transformation-provider"),
            mappedRepositoryProvider
        )
        project.repositories.maven {
            it.name = "Minecraft Libraries"
            it.url = URI("https://libraries.minecraft.net/")
        }
    }

    private val mappedRepositoryProvider = MappedRepositoryProvider(this)

    fun yarnMappings(dependency: Dependency): MappingDependency {
        dependency as ModuleDependency
        return YarnMappingDependency(this, dependency)
    }

    fun officialMappings(version: String, side: MCSide): MappingDependency {
        _registerMinecraftProvider
        val dependency by lazy {
            project.dependencies.create(
                sharedExtension.minecraftProvider.getMappingsDependencyCoordinate(
                    MinecraftProvider.MinecraftCoordinate(
                        version,
                        side
                    )
                )
            )
        }
        return OfficialMappingDependency(side, version, project.providers.provider { dependency })
    }

    fun intermediaryMappings(version: String): MappingDependency {
        return yarnMappings(project.dependencies.create("net.fabricmc:intermediary:$version:v2"))
    }

    fun mapJar(
        dependency: Dependency,
        mappings: MappingDependency,
        sourceNamespace: String,
        destinationNamespace: String
    ): Dependency {
        dependency as ModuleDependency
        _registerMinecraftProvider
        return project.dependencies.create(
            mappedRepositoryProvider.getDependencyCoordiante(
                MappedRepositoryProvider.MappedCoordinates(
                    dependency, mappings, sourceNamespace, destinationNamespace
                )
            )
        )
    }

    fun minecraft(version: String, side: MCSide): Dependency {
        _registerMinecraftProvider
        return project.dependencies.create(
            sharedExtension.minecraftProvider.getDependencyCoordinate(
                MinecraftProvider.MinecraftCoordinate(
                    version,
                    side,
                )
            )
        )
    }

    fun getLocalCacheDirectory(): File {
        return sharedExtension.getLocalCacheDirectory().resolve("projectspecific")
            .resolve(if (project == project.rootProject) "__root" else project.path.replace(":", "_"))
    }

}