From 9a16ada0f604eb409258efc1d8f2881d09634f4f Mon Sep 17 00:00:00 2001 From: Bryson Steck Date: Wed, 31 May 2023 22:05:25 -0600 Subject: dialogs now separate, creating dirs and files for later work --- .../controllers/DisclaimerController.kt | 0 .../ServerCraft/controllers/PrimaryController.kt | 96 +++------------------- .../ServerCraft/controllers/UpdateController.kt | 0 .../ServerCraft/dialogs/CommonDialog.kt | 34 ++++++++ .../xyz/brysonsteck/ServerCraft/dialogs/Dialog.kt | 77 +++++++---------- .../brysonsteck/ServerCraft/dialogs/EulaDialog.kt | 53 ++++++++++-- .../brysonsteck/ServerCraft/dialogs/KillDialog.kt | 34 ++++++++ .../xyz/brysonsteck/ServerCraft/misc/Updater.kt | 0 .../ServerCraft/preferences/Preferences.kt | 0 9 files changed, 156 insertions(+), 138 deletions(-) create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/DisclaimerController.kt create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/UpdateController.kt create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/CommonDialog.kt create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/KillDialog.kt create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/misc/Updater.kt create mode 100644 src/main/kotlin/xyz/brysonsteck/ServerCraft/preferences/Preferences.kt diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/DisclaimerController.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/DisclaimerController.kt new file mode 100644 index 0000000..e69de29 diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/PrimaryController.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/PrimaryController.kt index 6eb0af4..1d8af46 100644 --- a/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/PrimaryController.kt +++ b/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/PrimaryController.kt @@ -58,6 +58,9 @@ import xyz.brysonsteck.ServerCraft.server.Server import xyz.brysonsteck.ServerCraft.server.Download import xyz.brysonsteck.ServerCraft.App import xyz.brysonsteck.ServerCraft.dialogs.Dialog +import xyz.brysonsteck.ServerCraft.dialogs.CommonDialog +import xyz.brysonsteck.ServerCraft.dialogs.KillDialog +import xyz.brysonsteck.ServerCraft.dialogs.EulaDialog class PrimaryController { @FXML @@ -130,9 +133,9 @@ class PrimaryController { lateinit private var scrollPane: ScrollPane lateinit private var server: Server + lateinit private var killDialog: Dialog private var building = false private var directory = "" - private var asyncResult = false private var started = false private var loading = false private var showingConsole = false @@ -235,6 +238,7 @@ class PrimaryController { onPropChange("level-seed", new) } } + killDialog = KillDialog() } @FXML @@ -369,7 +373,7 @@ class PrimaryController { @FXML private fun onDefaults() { - val res = createDialog("info", "Reset settings to defaults?\nThere is NO GOING BACK!") + val res = CommonDialog("info", "Reset all settings?", "Reset settings to defaults?\nThere is NO GOING BACK!").show() if (res) { server.loadProps() applyProps() @@ -546,11 +550,11 @@ 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?", hold=false) + killDialog.show() return; } if (!File(directory + "eula.txt").exists()) { - val res = eulaDialog() + val res = EulaDialog().show() if (res) { File(directory + "eula.txt").writeText("eula=true") } else { @@ -575,7 +579,7 @@ class PrimaryController { var cbuf = CharArray(1000) reader.read(cbuf, 0, 1000) while (proc.isAlive) { - if (asyncResult) { + if (killDialog.result) { withContext(Dispatchers.JavaFx) { statusBar.text = "Killing Minecraft server..." startButton.isDisable = true @@ -593,8 +597,8 @@ class PrimaryController { withContext(Dispatchers.JavaFx) {log("Stream Closed\n")} } withContext(Dispatchers.JavaFx) { - statusBar.text = if (asyncResult) { - asyncResult = false + statusBar.text = if (killDialog.result) { + killDialog.result = false "Server killed." } else { "Server stopped." @@ -611,80 +615,6 @@ class PrimaryController { } } - private fun eulaDialog(): Boolean { - var result = false - val resources = App().javaClass.getResource("icons/warning.png") - val dialog = Stage() - dialog.icons.add(Image(App().javaClass.getResourceAsStream("app.png"))) - dialog.setResizable(false) - dialog.initModality(Modality.APPLICATION_MODAL); - dialog.title = directory - val scenePane = Pane() - val dialogScene = Scene(scenePane, 400.0, 150.0); - val imagePane = Pane() - val icon = Image("$resources") - imagePane.layoutX = 14.0 - imagePane.layoutY = 14.0 - imagePane.scaleX = 0.7 - imagePane.scaleY = 0.7 - imagePane.children.add(ImageView(icon)) - val label = Label("Do you agree to the terms of the Minecraft End User License Agreement?") - label.isWrapText = true - label.maxWidth = 250.0 - label.layoutX = 115.0 - label.layoutY = 40.0 - val buttonBar = ButtonBar() - buttonBar.buttonOrder = "L+R" - 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("I Disagree") - noButton.onAction = EventHandler() { - result = false - dialog.hide() - } - noButton.isDefaultButton = true - val yesButton = Button("I Agree") - yesButton.onAction = EventHandler() { - result = true - dialog.hide() - } - val eula = Button("View EULA") - eula.onAction = EventHandler() { - val desktop = Desktop.getDesktop() - if (desktop.isSupported(Desktop.Action.BROWSE)) { - // most likely running on Windows or macOS - try { - desktop.browse(URI("https://account.mojang.com/documents/minecraft_eula")) - } catch (e: Exception) { - println(e) - } - } else { - // assume running on linux - try { - Runtime.getRuntime().exec("xdg-open https://account.mojang.com/documents/minecraft_eula"); - } catch (e: Exception) { - println(e) - } - } - } - ButtonBar.setButtonData(eula, ButtonBar.ButtonData.LEFT) - ButtonBar.setButtonData(noButton, ButtonBar.ButtonData.RIGHT) - ButtonBar.setButtonData(yesButton, ButtonBar.ButtonData.RIGHT) - buttonBar.buttons.add(eula) - buttonBar.buttons.add(noButton) - buttonBar.buttons.add(yesButton) - scenePane.children.addAll(imagePane, label, buttonBar) - dialog.setScene(dialogScene); - dialog.showAndWait(); - return result - } - - private fun createDialog(type: String, msg: String, yes: String = "Yes", no: String = "No", hold: Boolean = true): Boolean { - return Dialog(type, directory, msg, yes, no, null, hold).show() - } - private fun loadServerDir(dir: String): Boolean { directory = dir // exit if doesn't exist for whatever reason @@ -711,7 +641,7 @@ class PrimaryController { statusBar.text = "Server needs to be built before starting." } else if (!hasDummy && hasServer) { // server created externally - val result = createDialog("warning", "This server directory was not created by \nServerCraft. Errors may occur; copying\nthe world directories to a new folder may be\nsafer. Proceed anyway?") + val result = CommonDialog("warning", directory, "This server directory was not created by \nServerCraft. Errors may occur; copying\nthe world directories to a new folder may be\nsafer. Proceed anyway?").show() statusBar.text = "Ready." if (result) { startButton.isDisable = true @@ -723,7 +653,7 @@ class PrimaryController { return result } else { // assume clean directory - val result = createDialog("info", "There is no server in this directory.\nCreate one?") + val result = CommonDialog("info", directory, "There is no server in this directory.\nCreate one?").show() if (result) { File(directory + "ServerCraft").mkdir() startButton.isDisable = true diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/UpdateController.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/controllers/UpdateController.kt new file mode 100644 index 0000000..e69de29 diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/CommonDialog.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/CommonDialog.kt new file mode 100644 index 0000000..6a8f14d --- /dev/null +++ b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/CommonDialog.kt @@ -0,0 +1,34 @@ +package xyz.brysonsteck.ServerCraft.dialogs + +import javafx.scene.control.Button +import javafx.event.EventHandler +import javafx.event.ActionEvent + +class CommonDialog: Dialog { + override var type: String + override var title: String + override var msg: String + override val neutralButton: Button? + override val yesButton: Button? + override val noButton: Button? + + constructor(type: String, title: String, msg: String) { + this.type = type + this.title = title + this.msg = msg + + noButton = Button("No") + noButton.onAction = EventHandler() { + result = false + dialog.hide() + } + yesButton = Button("Yes") + yesButton.onAction = EventHandler() { + result = true + dialog.hide() + } + yesButton.isDefaultButton = true + neutralButton = null + } + +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/Dialog.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/Dialog.kt index 0984188..1c1c60a 100644 --- a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/Dialog.kt +++ b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/Dialog.kt @@ -1,57 +1,41 @@ package xyz.brysonsteck.ServerCraft.dialogs import javafx.scene.control.Button -import javafx.scene.control.Label import javafx.scene.control.ButtonBar +import javafx.scene.control.Label import javafx.scene.image.Image import javafx.scene.image.ImageView import javafx.scene.layout.Pane import javafx.scene.Scene -import javafx.stage.Stage import javafx.stage.Modality +import javafx.stage.Stage import javafx.geometry.Insets import javafx.event.EventHandler import javafx.event.ActionEvent import xyz.brysonsteck.ServerCraft.App -open class Dialog { - private var result = false +abstract class Dialog { + public var result = false - private val type: String - private val title: String - private val msg: String - private val yes: String - private val no: String - private val neutral: Button? - private val hold: Boolean + abstract val type: String + abstract val title: String + abstract val msg: String + abstract val neutralButton: Button? + abstract val yesButton: Button? + abstract val noButton: Button? - constructor(type: String, - title: String, - msg: String, - yes: String = "Yes", - no: String = "No", - neutral: Button?, - hold: Boolean = true) { - this.type = type - this.title = title - this.msg = msg - this.yes = yes - this.no = no - this.neutral = neutral - this.hold = hold - } + protected var hold: Boolean = true + protected var labelOffset: Double = 40.0 + protected val dialog = Stage() public fun show(): Boolean { val resources = App().javaClass.getResource("icons/$type.png") - val dialog = Stage() + val icon = Image("$resources") dialog.icons.add(Image(App().javaClass.getResourceAsStream("app.png"))) dialog.setResizable(false) dialog.initModality(Modality.APPLICATION_MODAL); dialog.title = title - val scenePane = Pane() - val dialogScene = Scene(scenePane, 400.0, 150.0); val imagePane = Pane() - val icon = Image("$resources") imagePane.layoutX = 14.0 imagePane.layoutY = 14.0 imagePane.scaleX = 0.7 @@ -60,30 +44,27 @@ open class Dialog { val label = Label(msg) label.isWrapText = true label.layoutX = 115.0 - label.layoutY = if (type == "warning") {10.0} else {40.0} + label.layoutY = labelOffset val buttonBar = ButtonBar() - buttonBar.padding = Insets(10.0, 10.0, 10.0, 10.0) + buttonBar.padding = Insets(10.0) buttonBar.layoutX = 0.0 buttonBar.layoutY = 107.0 buttonBar.prefWidth = 400.0 - val noButton = Button(no) - noButton.onAction = EventHandler() { - result = false - dialog.hide() + val scenePane = Pane() + scenePane.children.addAll(imagePane, label, buttonBar) + if (noButton != null) { + ButtonBar.setButtonData(noButton, ButtonBar.ButtonData.RIGHT) + buttonBar.buttons.add(noButton) } - val yesButton = Button(yes) - yesButton.onAction = EventHandler() { - result = true - dialog.hide() + if (yesButton != null) { + ButtonBar.setButtonData(yesButton, ButtonBar.ButtonData.RIGHT) + buttonBar.buttons.add(yesButton) } - yesButton.isDefaultButton = true - buttonBar.buttons.add(noButton) - buttonBar.buttons.add(yesButton) - scenePane.children.addAll(imagePane, label, buttonBar) - ButtonBar.setButtonData(noButton, ButtonBar.ButtonData.RIGHT) - ButtonBar.setButtonData(yesButton, ButtonBar.ButtonData.RIGHT) - buttonBar.buttons.add(noButton) - buttonBar.buttons.add(yesButton) + if (neutralButton != null) { + ButtonBar.setButtonData(neutralButton, ButtonBar.ButtonData.LEFT) + buttonBar.buttons.add(neutralButton) + } + val dialogScene = Scene(scenePane, 400.0, 150.0); dialog.setScene(dialogScene); if (hold) { dialog.showAndWait(); diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/EulaDialog.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/EulaDialog.kt index a05b3a9..4084b08 100644 --- a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/EulaDialog.kt +++ b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/EulaDialog.kt @@ -2,15 +2,54 @@ package xyz.brysonsteck.ServerCraft.dialogs import xyz.brysonsteck.ServerCraft.dialogs.Dialog import javafx.scene.control.Button +import javafx.event.EventHandler +import javafx.event.ActionEvent +import java.awt.Desktop +import java.net.URI class EulaDialog: Dialog { + override val type: String + override val title: String + override val msg: String + override val neutralButton: Button? + override val yesButton: Button? + override val noButton: Button? - constructor(type: String, - title: String, - msg: String, - yes: String = "Yes", - no: String = "No", - neutral: Button?, - hold: Boolean = true) : super(type, title, msg, yes, no, neutral, hold) + constructor() { + type = "warning" + title = "Minecraft End User License Agreement (EULA)" + msg = "Do you agree to the terms of the Minecraft End User License Agreement?" + + noButton = Button("I Disagree") + noButton.onAction = EventHandler() { + result = false + dialog.hide() + } + noButton.isDefaultButton = true + yesButton = Button("I Agree") + yesButton.onAction = EventHandler() { + result = true + dialog.hide() + } + neutralButton = Button("View EULA") + neutralButton.onAction = EventHandler() { + val desktop = Desktop.getDesktop() + if (desktop.isSupported(Desktop.Action.BROWSE)) { + // most likely running on Windows or macOS + try { + desktop.browse(URI("https://account.mojang.com/documents/minecraft_eula")) + } catch (e: Exception) { + println(e) + } + } else { + // assume running on linux + try { + Runtime.getRuntime().exec("xdg-open https://account.mojang.com/documents/minecraft_eula"); + } catch (e: Exception) { + println(e) + } + } + } + } } \ No newline at end of file diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/KillDialog.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/KillDialog.kt new file mode 100644 index 0000000..5ef0bbc --- /dev/null +++ b/src/main/kotlin/xyz/brysonsteck/ServerCraft/dialogs/KillDialog.kt @@ -0,0 +1,34 @@ +package xyz.brysonsteck.ServerCraft.dialogs + +import javafx.scene.control.Button +import javafx.event.EventHandler +import javafx.event.ActionEvent + +class KillDialog: Dialog { + override val type: String + override val title: String + override val msg: String + override val neutralButton: Button? + override val yesButton: Button? + override val noButton: Button? + + constructor() { + type = "warning" + title = "Server is still running!" + msg = "You should only kill the server if absolutely necessary, i.e. not being responsive after long periods of time. Data loss may occur. Continue anyway?" + hold = false + + noButton = Button("No") + noButton.onAction = EventHandler() { + result = false + dialog.hide() + } + yesButton = Button("Yes") + yesButton.onAction = EventHandler() { + result = true + dialog.hide() + } + yesButton.isDefaultButton = true + neutralButton = null + } +} \ No newline at end of file diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/misc/Updater.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/misc/Updater.kt new file mode 100644 index 0000000..e69de29 diff --git a/src/main/kotlin/xyz/brysonsteck/ServerCraft/preferences/Preferences.kt b/src/main/kotlin/xyz/brysonsteck/ServerCraft/preferences/Preferences.kt new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3