Gradle: Implement --update option for setupPaperServer, and add runPaperServer

This commit is contained in:
Kenneth Endfinger
2022-01-13 23:19:07 -05:00
parent 01999eadd7
commit 8f34209aff
5 changed files with 73 additions and 19 deletions

View File

@ -135,7 +135,7 @@ subprojects {
}
}
tasks.setupPaperServer {
minecraftServerPath = "server"
paperVersionGroup = "1.18"
foundation {
minecraftServerPath.set("server")
paperVersionGroup.set("1.18")
}

View File

@ -0,0 +1,8 @@
package cloud.kubelet.foundation.gradle
import org.gradle.api.provider.Property
interface FoundationExtension {
val paperVersionGroup: Property<String>
val minecraftServerPath: Property<String>
}

View File

@ -2,9 +2,13 @@ package cloud.kubelet.foundation.gradle
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
class FoundationGradlePlugin : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.create("setupPaperServer", SetupPaperServer::class.java)
project.extensions.create<FoundationExtension>("foundation")
val setupPaperServer = project.tasks.create<SetupPaperServer>("setupPaperServer")
val runPaperServer = project.tasks.create<RunPaperServer>("runPaperServer")
runPaperServer.dependsOn(setupPaperServer)
}
}

View File

@ -0,0 +1,25 @@
package cloud.kubelet.foundation.gradle
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.getByType
open class RunPaperServer : DefaultTask() {
init {
outputs.upToDateWhen { false }
}
@TaskAction
fun runPaperServer() {
val foundation = project.extensions.getByType<FoundationExtension>()
val minecraftServerDirectory = project.file(foundation.minecraftServerPath.get())
val paperJarFile = minecraftServerDirectory.resolve("paper.jar")
project.javaexec {
classpath(paperJarFile.absolutePath)
workingDir(minecraftServerDirectory)
args("nogui")
mainClass.set("io.papermc.paperclip.Main")
}
}
}

View File

@ -3,34 +3,35 @@ package cloud.kubelet.foundation.gradle
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.TaskAction
import org.gradle.api.tasks.options.Option
import org.gradle.kotlin.dsl.getByType
import java.io.File
import java.nio.file.Files
open class SetupPaperServer : DefaultTask() {
@get:Input
lateinit var paperVersionGroup: String
init {
outputs.upToDateWhen { false }
}
@get:Input
lateinit var minecraftServerPath: String
@set:Option(option = "update", description = "Update Paper Server")
var shouldUpdatePaperServer = false
private val paperVersionClient = PaperVersionClient()
@TaskAction
fun downloadPaperTask() {
val minecraftServerDirectory = project.file(minecraftServerPath)
val client = PaperVersionClient()
val builds = client.getVersionBuilds(paperVersionGroup)
val build = builds.last()
val download = build.downloads["application"]!!
val url = client.resolveDownloadUrl(build, download)
val foundation = project.extensions.getByType<FoundationExtension>()
val minecraftServerDirectory = project.file(foundation.minecraftServerPath.get())
if (!minecraftServerDirectory.exists()) {
minecraftServerDirectory.mkdirs()
}
ant.invokeMethod(
"get", mapOf(
"src" to url.toString(),
"dest" to project.file("${minecraftServerPath}/paper.jar").absolutePath
)
)
val paperJarFile = project.file("${foundation.minecraftServerPath.get()}/paper.jar")
if (!paperJarFile.exists() || shouldUpdatePaperServer) {
downloadLatestBuild(foundation.paperVersionGroup.get(), paperJarFile)
}
val paperPluginsDirectory = minecraftServerDirectory.resolve("plugins")
@ -52,4 +53,20 @@ open class SetupPaperServer : DefaultTask() {
Files.createSymbolicLink(pluginLinkFile.toPath(), pluginJarFile.toPath())
}
}
private fun downloadLatestBuild(paperVersionGroup: String, paperJarFile: File) {
val builds = paperVersionClient.getVersionBuilds(paperVersionGroup)
val build = builds.last()
val download = build.downloads["application"]!!
val url = paperVersionClient.resolveDownloadUrl(build, download)
ant.invokeMethod(
"get", mapOf(
"src" to url.toString(),
"dest" to paperJarFile.absolutePath
)
)
logger.lifecycle("Installed Paper Server ${build.version} build ${build.build}")
}
}