Initial commit

This commit is contained in:
2025-04-28 23:25:29 +08:00
commit ca938a603e
22 changed files with 787 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
/*
* Copyright © 2025 RTAkland
* Date: 2025/4/28 22:44
* Open Source Under Apache-2.0 License
* https://www.apache.org/licenses/LICENSE-2.0
*/
package cn.rtast.runbroker.subserver
import cn.rtast.runbroker.subserver.bukkit.RunBrokerSubServerTask
import cn.rtast.runbroker.subserver.bungeecord.RunBrokerBungeeCordTask
import org.gradle.api.Plugin
import org.gradle.api.Project
class RunBrokerSubserver : Plugin<Project> {
override fun apply(target: Project) {
target.extensions.create("runBrokerSubserver", RunBrokerSubserverExtension::class.java)
val settings = target.extensions.getByType(RunBrokerSubserverExtension::class.java)
val serverCount = settings.subServerCount.get()
repeat(serverCount) {
target.tasks.register("runServer$it", RunBrokerSubServerTask::class.java) { task ->
task.group = "runbroker-subserver"
}
}
target.tasks.register("runBungeecord", RunBrokerBungeeCordTask::class.java) { task ->
task.group = "runbroker-subserver"
}
}
}

View File

@@ -0,0 +1,37 @@
/*
* Copyright © 2025 RTAkland
* Date: 2025/4/28 22:45
* Open Source Under Apache-2.0 License
* https://www.apache.org/licenses/LICENSE-2.0
*/
package cn.rtast.runbroker.subserver
import cn.rtast.runbroker.common.JavaExecPath
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
abstract class RunBrokerSubserverExtension {
@get:Input
abstract val minecraftServerPath: Property<String>
@get:Input
abstract val subServerCount: Property<Int>
@get:Input
abstract val brokerBukkitPluginPath: Property<String>
@get:Input
abstract val brokerBungeeCordPluginPath: Property<String>
@get:Input
abstract val javaExecPath: Property<JavaExecPath>
init {
subServerCount.convention(2)
minecraftServerPath.convention("./server.jar")
brokerBukkitPluginPath.convention("./plugin.jar")
brokerBungeeCordPluginPath.convention("./plugin.jar")
javaExecPath.convention(JavaExecPath.DEFAULT)
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright © 2025 RTAkland
* Date: 2025/4/28 23:16
* Open Source Under Apache-2.0 License
* https://www.apache.org/licenses/LICENSE-2.0
*/
package cn.rtast.runbroker.subserver.bukkit
import cn.rtast.runbroker.common.util.readBytesFromFile
import cn.rtast.runbroker.subserver.RunBrokerSubserverExtension
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
abstract class RunBrokerSubServerTask : DefaultTask() {
init {
dependsOn(project.tasks.named("shadowJar"))
}
@TaskAction
fun runSubserver() {
val shadowJarTask = project.tasks.named("shadowJar").get()
val settings = project.extensions.getByType(RunBrokerSubserverExtension::class.java)
val currentTaskName = this.name
val runDir = project.layout.projectDirectory.dir("run/server/$currentTaskName").asFile
.apply { mkdirs() }
val serverCore = File(runDir, "server.jar")
val pluginsDir = File(runDir, "plugins").apply { mkdirs() }
val brokerPluginFile = File(pluginsDir, "broker-bukkit.jar")
if (!brokerPluginFile.exists()) brokerPluginFile.writeBytes(
settings.brokerBukkitPluginPath.get().readBytesFromFile()
)
if (!serverCore.exists()) serverCore.writeBytes(settings.minecraftServerPath.get().readBytesFromFile())
val previousPluginJar = File(pluginsDir, "_broker-plugin.jar")
if (previousPluginJar.exists()) previousPluginJar.delete()
val builtJar = shadowJarTask.outputs.files.singleFile
builtJar.copyTo(previousPluginJar, overwrite = true)
val executablePath = settings.javaExecPath.get().path
project.exec {
it.executable = if (executablePath == "-") "java" else executablePath
it.workingDir = project.layout.projectDirectory.dir("run/server/$currentTaskName").asFile
it.args = listOf("-jar", serverCore.absolutePath)
}
println("Server $currentTaskName started...")
}
}

View File

@@ -0,0 +1,44 @@
/*
* Copyright © 2025 RTAkland
* Date: 2025/4/28 23:16
* Open Source Under Apache-2.0 License
* https://www.apache.org/licenses/LICENSE-2.0
*/
package cn.rtast.runbroker.subserver.bungeecord
import cn.rtast.runbroker.common.util.readBytesFromFile
import cn.rtast.runbroker.subserver.RunBrokerSubserverExtension
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
import java.io.File
import java.net.URI
private const val BUNGEECORD_DOWNLOAD_URL =
"https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar"
class RunBrokerBungeeCordTask : DefaultTask() {
@TaskAction
fun runSubserver() {
val settings = project.extensions.getByType(RunBrokerSubserverExtension::class.java)
val runDir = project.layout.projectDirectory.dir("run/bungee/").asFile.apply { mkdirs() }
val bungeeCord = File(runDir, "bungee.jar")
val pluginsDir = File(runDir, "plugins").apply { mkdirs() }
val brokerBungeePluginFile = File(pluginsDir, "broker-bungee.jar")
if (!brokerBungeePluginFile.exists()) brokerBungeePluginFile.writeBytes(
settings.brokerBungeeCordPluginPath.get().readBytesFromFile()
)
println("Downloading Bungeecord...")
val bungeeCordBytes = URI(BUNGEECORD_DOWNLOAD_URL).toURL().readBytes()
println("BungeeCord downloaded...")
if (!bungeeCord.exists()) bungeeCord.writeBytes(bungeeCordBytes)
val executablePath = settings.javaExecPath.get().path
project.exec {
it.executable = if (executablePath == "-") "java" else executablePath
it.workingDir = project.layout.projectDirectory.dir("run/bungee/").asFile
it.args = listOf("-jar", bungeeCord.absolutePath)
}
println("BungeeCord started...")
}
}