plugins {
    id 'architectury-plugin' version '3.4-SNAPSHOT'
    id 'dev.architectury.loom' version '1.10-SNAPSHOT' apply false
}

architectury {
    minecraft = project.minecraft_version
}

subprojects {
    apply plugin: 'dev.architectury.loom'

    loom {
        silentMojangMappingsLicense()
    }

    dependencies {
        minecraft "com.mojang:minecraft:${project.minecraft_version}"
        mappings loom.officialMojangMappings()
    }
}

allprojects {
    apply plugin: 'java'
    apply plugin: 'checkstyle'
    apply plugin: 'maven-publish'
    apply plugin: 'architectury-plugin'

    group = rootProject.maven_group
    version = rootProject.version
    archivesBaseName = rootProject.archives_base_name

    sourceCompatibility = targetCompatibility = JavaVersion.VERSION_21

    // Perform tests using the JUnit test suite
    test {
        useJUnitPlatform()
    }

    // Produce additional distributions
    java {
        withJavadocJar()
        withSourcesJar()
    }

    // Add the licence to all distributions
    tasks.withType(Jar).configureEach {
        it.from rootProject.file('LICENCE.txt')
        it.duplicatesStrategy = 'EXCLUDE'
    }

    // Perform linting using Checkstyle
    checkstyle {
        configFile rootProject.file('.checkstyle.xml')
        toolVersion project.checkstyle_version
    }

    // Add any additional repositories
    repositories {
        mavenCentral()
        maven { name = 'Architectury'; url = 'https://maven.architectury.dev/' }
        maven { name = 'Fabric'; url = 'https://maven.fabricmc.net/' }
        maven { name = 'Forge'; url = 'https://maven.minecraftforge.net/' }
        maven { name = 'Modrinth'; url = 'https://api.modrinth.com/maven' }
        maven { name = 'NeoForge'; url = 'https://maven.neoforged.net/releases/' }
        maven { name = 'Sponge'; url = 'https://repo.spongepowered.org/repository/maven-public' }
        maven { name = 'Team Resourceful'; url = 'https://maven.teamresourceful.com/repository/maven-public/' }
        maven { name = 'TerraformersMC'; url = 'https://maven.terraformersmc.com/' }
    }

    // Define how packages are published
    publishing {
        // Declare all publications
        if (project != rootProject) {
            publications {
                mavenJava(MavenPublication) {
                    from components.java
                    artifactId "${rootProject.archives_base_name}-${project.name}"
                    pom {
                        name = project.mod_name
                        description = project.mod_description
                        url = "https://github.com/${project.github_repo}"
                        licenses { license { name = 'MIT'; url = 'https://opensource.org/licenses/MIT' } }
                        developers {
                            developer {
                                name = project.mod_author
                                email = project.mod_author_email
                                url = "https://github.com/${project.mod_author}"
                            }
                        }
                        scm {
                            connection = "scm:git:git://github.com/${project.github_repo}.git"
                            developerConnection = "scm:git:ssh://git@github.com:${project.github_repo}.git"
                            url = "https://github.com/${project.github_repo}"
                        }
                        issueManagement { system = 'GitHub'; url = "https://github.com/${project.github_repo}/issues" }
                        ciManagement { system = 'GitHub'; url = "https://github.com/${project.github_repo}/actions" }
                        properties = [
                            'mod_id'           : project.mod_id,
                            'minecraft_version': project.minecraft_version,
                        ]
                    }
                }
            }
        }

        // Add repositories to publish to
        repositories {
            // GitHub Packages (https://pkg.github.com)
            maven {
                name 'GitHub'
                url "https://maven.pkg.github.com/${project.github_repo}"
                credentials {
                    username System.getenv('GITHUB_ACTOR')
                    password System.getenv('GITHUB_TOKEN')
                }
            }
        }
    }
}
