From bbf11561cf6598c9dc8ca412dc1f2bb3cc760244 Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Fri, 12 May 2023 23:20:16 -0600 Subject: implemented opening directories and starting servers --- .../serverfordummies/PrimaryController.kt | 117 ++++++++++++++++++--- 1 file changed, 101 insertions(+), 16 deletions(-) (limited to 'app/src/main/kotlin') diff --git a/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/PrimaryController.kt b/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/PrimaryController.kt index 5bc578e..a73745f 100644 --- a/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/PrimaryController.kt +++ b/app/src/main/kotlin/xyz/brysonsteck/serverfordummies/PrimaryController.kt @@ -6,6 +6,8 @@ import org.rauschig.jarchivelib.* import java.io.File import java.io.IOException +import java.io.BufferedReader +import java.io.InputStreamReader import java.awt.Checkbox import java.util.Properties import java.net.URL @@ -108,6 +110,9 @@ class PrimaryController { lateinit private var buildButton: Button private var building = false + private var directory = "" + private var asyncResult = false + private var started = false @FXML private fun onDirectoryButtonClick() { @@ -117,10 +122,17 @@ class PrimaryController { val result = dirChooser.showDialog(null) if (result != null) { currentDirectoryLabel.text = result.absolutePath - val real = loadServerDir(result.absolutePath) - parentPane.isDisable = false - worldSettingsPane.isDisable = false - buttonBar.isDisable = false + val res = loadServerDir(result.absolutePath) + if (res) { + parentPane.isDisable = false + worldSettingsPane.isDisable = false + buttonBar.isDisable = false + } else { + currentDirectoryLabel.text = "" + parentPane.isDisable = true + worldSettingsPane.isDisable = true + buttonBar.isDisable = true + } } } @@ -198,17 +210,59 @@ class PrimaryController { @FXML private fun onStart() { + if (started) { + createDialog("warning", "You should only kill the server if\nabsolutely necessary. Data loss may occur.\nContinue anyway?", "Yes", "No", false) + return; + } + started = true statusBar.text = "The Minecraft Server is now running. Shutdown the server to unlock the settings." + worldSettingsPane.isDisable = true + directoryPane.isDisable = true + parentPane.isDisable = true + buildButton.isDisable = true + startButton.text = "Kill Server" @Suppress("OPT_IN_USAGE") GlobalScope.launch(Dispatchers.Default) { - val proc = Runtime.getRuntime().exec("java -jar /home/bryson/test/testserver/server.jar"); + val builder = ProcessBuilder("java", "-jar", "${directory}server.jar") + builder.directory(File(directory)) + val proc = builder.start() + val reader = InputStreamReader(proc.inputStream) + val br = BufferedReader(reader) + try { + var line = br.readLine() + while (line != null) { + if (asyncResult) { + proc.destroy() + } + println(line); + line = br.readLine() + } + } catch (e: IOException) { + println("Stream closed") + } + withContext(Dispatchers.JavaFx) { + statusBar.text = if (asyncResult) { + asyncResult = false + "Server killed." + } else { + "Server stopped." + } + worldSettingsPane.isDisable = false + directoryPane.isDisable = false + parentPane.isDisable = false + buildButton.isDisable = false + startButton.text = "Start Server" + started = false + } } } - private fun createDialog(type: String, msg: String, yes: String, no: String) { + private fun createDialog(type: String, msg: String, yes: String, no: String, hold: Boolean): Boolean { + var result = false val resources = this.javaClass.getResource("icons/$type.png") val dialog = Stage(); dialog.initModality(Modality.APPLICATION_MODAL); + dialog.title = directory val scenePane = Pane() val dialogScene = Scene(scenePane, 400.0, 150.0); val imagePane = Pane() @@ -220,25 +274,45 @@ class PrimaryController { imagePane.children.add(ImageView(icon)) val label = Label(msg) label.layoutX = 115.0 - label.layoutY = 40.0 + label.layoutY = if (type == "warning") {10.0} else {40.0} val buttonBar = ButtonBar() buttonBar.padding = Insets(10.0, 10.0, 10.0, 10.0) buttonBar.layoutX = 0.0 buttonBar.layoutY = 107.0 buttonBar.prefWidth = 400.0 val noButton = Button(no) + noButton.onMouseClicked = EventHandler() { + if (hold) { + result = false + } else { + asyncResult = false + } + dialog.hide() + } val yesButton = Button(yes) + yesButton.onMouseClicked = EventHandler() { + if (hold) { + result = true + } else { + asyncResult = true + } + dialog.hide() + } yesButton.isDefaultButton = true buttonBar.buttons.add(noButton) buttonBar.buttons.add(yesButton) scenePane.children.addAll(imagePane, label, buttonBar) dialog.setScene(dialogScene); - dialog.show(); + if (hold) { + dialog.showAndWait(); + } else { + dialog.show(); + } + return result } private fun loadServerDir(dir: String): Boolean { - createDialog("info", "There is no server in this directory.\nCreate one?", "Yes", "No") - var directory = dir + directory = dir var hasServer = false if (!File(directory).isDirectory) { return false; @@ -249,7 +323,9 @@ class PrimaryController { val hasDummy = File(directory + "ServerForDummies").isDirectory - for (i in 20 downTo 8) { + // major version + for (i in 25 downTo 8) { + // patch number for (j in 15 downTo 0) { var spigotFile: String = "" if (j == 0) @@ -266,17 +342,26 @@ class PrimaryController { val hasProperties = File(directory + File.separator + "server.properties").isFile if (hasDummy && hasServer) { - // read jproperties - println("read jproperties") + // server complete, just read jproperties + statusBar.text = "Server found!" + startButton.isDisable = false } else if (hasDummy && !hasServer && hasProperties) { // just needs to be built - println("build") + startButton.isDisable = true + statusBar.text = "Server needs to be built before starting." } else if (!hasDummy && hasServer) { // server created externally - println("server made externally") + val result = createDialog("warning", "This server directory was not created by \nServerForDummies. Errors may occur; copying\nthe world directories to a new folder may be\nsafer. Proceed anyway?", "Yes", "No", true) + statusBar.text = "Ready." + if (result) { + startButton.isDisable = false + } + return result } else { // assume clean directory - println("none") + val result = createDialog("info", "There is no server in this directory.\nCreate one?", "Yes", "No", true) + statusBar.text = "Ready." + return result } return true; -- cgit v1.2.3