plugins {
    id 'base'
}

group = 'com.xfestudio.xfeservermanager'
version = providers.gradleProperty('modVersion').getOrElse('0.1.0-SNAPSHOT')

subprojects {
    apply plugin: 'java-library'
    apply plugin: 'jacoco'

    group = rootProject.group
    version = rootProject.version

    java {
        toolchain {
            languageVersion = JavaLanguageVersion.of(17)
        }
        withSourcesJar()
        withJavadocJar()
    }

    tasks.withType(JavaCompile).configureEach {
        options.encoding = 'UTF-8'
        options.release = 17
        options.compilerArgs += ['-Xlint:all', '-parameters']
    }

    tasks.withType(Test).configureEach {
        useJUnitPlatform()
        testLogging {
            events 'failed', 'skipped'
            exceptionFormat 'full'
        }
    }

    tasks.withType(Jar).configureEach {
        reproducibleFileOrder = true
        preserveFileTimestamps = false
    }

    dependencyLocking {
        lockAllConfigurations()
    }
}

// Keep each source tree as an explicit input root. Using `common/` as the root makes Gradle's
// task validation conservatively treat sibling `build/` directories as consumed outputs even
// when the include pattern only selects Java sources.
def commonJavaSources = files(
        fileTree('common/api/src') { include '**/*.java' },
        fileTree('common/core/src') { include '**/*.java' },
        fileTree('common/infra/src') { include '**/*.java' },
        fileTree('common/testkit/src') { include '**/*.java' }
)

tasks.register('verifyNoPlatformImports') {
    group = 'verification'
    description = 'Fails when common Java sources reference Minecraft or Forge classes.'
    inputs.files(commonJavaSources).withPathSensitivity(PathSensitivity.RELATIVE)
    doLast {
        def offenders = inputs.files.files.findAll { file ->
            def source = file.getText('UTF-8')
            source.contains('net.minecraft.') || source.contains('net.minecraftforge.')
        }
        if (!offenders.empty) {
            throw new GradleException("Platform imports found in common sources: ${offenders}")
        }
    }
}

tasks.named('check') {
    dependsOn 'verifyNoPlatformImports'
    dependsOn subprojects.collect { project -> "${project.path}:check" }
}
