implemented opening directories and starting servers

This commit is contained in:
Bryson Steck 2023-05-12 23:20:16 -06:00
parent 0522f0aae1
commit bbf11561cf

View file

@ -6,6 +6,8 @@ import org.rauschig.jarchivelib.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
import java.io.BufferedReader
import java.io.InputStreamReader
import java.awt.Checkbox import java.awt.Checkbox
import java.util.Properties import java.util.Properties
import java.net.URL import java.net.URL
@ -108,6 +110,9 @@ class PrimaryController {
lateinit private var buildButton: Button lateinit private var buildButton: Button
private var building = false private var building = false
private var directory = ""
private var asyncResult = false
private var started = false
@FXML @FXML
private fun onDirectoryButtonClick() { private fun onDirectoryButtonClick() {
@ -117,10 +122,17 @@ class PrimaryController {
val result = dirChooser.showDialog(null) val result = dirChooser.showDialog(null)
if (result != null) { if (result != null) {
currentDirectoryLabel.text = result.absolutePath currentDirectoryLabel.text = result.absolutePath
val real = loadServerDir(result.absolutePath) val res = loadServerDir(result.absolutePath)
if (res) {
parentPane.isDisable = false parentPane.isDisable = false
worldSettingsPane.isDisable = false worldSettingsPane.isDisable = false
buttonBar.isDisable = false buttonBar.isDisable = false
} else {
currentDirectoryLabel.text = "<NONE>"
parentPane.isDisable = true
worldSettingsPane.isDisable = true
buttonBar.isDisable = true
}
} }
} }
@ -198,17 +210,59 @@ class PrimaryController {
@FXML @FXML
private fun onStart() { 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." 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") @Suppress("OPT_IN_USAGE")
GlobalScope.launch(Dispatchers.Default) { 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 resources = this.javaClass.getResource("icons/$type.png")
val dialog = Stage(); val dialog = Stage();
dialog.initModality(Modality.APPLICATION_MODAL); dialog.initModality(Modality.APPLICATION_MODAL);
dialog.title = directory
val scenePane = Pane() val scenePane = Pane()
val dialogScene = Scene(scenePane, 400.0, 150.0); val dialogScene = Scene(scenePane, 400.0, 150.0);
val imagePane = Pane() val imagePane = Pane()
@ -220,25 +274,45 @@ class PrimaryController {
imagePane.children.add(ImageView(icon)) imagePane.children.add(ImageView(icon))
val label = Label(msg) val label = Label(msg)
label.layoutX = 115.0 label.layoutX = 115.0
label.layoutY = 40.0 label.layoutY = if (type == "warning") {10.0} else {40.0}
val buttonBar = ButtonBar() val buttonBar = ButtonBar()
buttonBar.padding = Insets(10.0, 10.0, 10.0, 10.0) buttonBar.padding = Insets(10.0, 10.0, 10.0, 10.0)
buttonBar.layoutX = 0.0 buttonBar.layoutX = 0.0
buttonBar.layoutY = 107.0 buttonBar.layoutY = 107.0
buttonBar.prefWidth = 400.0 buttonBar.prefWidth = 400.0
val noButton = Button(no) val noButton = Button(no)
noButton.onMouseClicked = EventHandler<MouseEvent>() {
if (hold) {
result = false
} else {
asyncResult = false
}
dialog.hide()
}
val yesButton = Button(yes) val yesButton = Button(yes)
yesButton.onMouseClicked = EventHandler<MouseEvent>() {
if (hold) {
result = true
} else {
asyncResult = true
}
dialog.hide()
}
yesButton.isDefaultButton = true yesButton.isDefaultButton = true
buttonBar.buttons.add(noButton) buttonBar.buttons.add(noButton)
buttonBar.buttons.add(yesButton) buttonBar.buttons.add(yesButton)
scenePane.children.addAll(imagePane, label, buttonBar) scenePane.children.addAll(imagePane, label, buttonBar)
dialog.setScene(dialogScene); dialog.setScene(dialogScene);
if (hold) {
dialog.showAndWait();
} else {
dialog.show(); dialog.show();
} }
return result
}
private fun loadServerDir(dir: String): Boolean { private fun loadServerDir(dir: String): Boolean {
createDialog("info", "There is no server in this directory.\nCreate one?", "Yes", "No") directory = dir
var directory = dir
var hasServer = false var hasServer = false
if (!File(directory).isDirectory) { if (!File(directory).isDirectory) {
return false; return false;
@ -249,7 +323,9 @@ class PrimaryController {
val hasDummy = File(directory + "ServerForDummies").isDirectory 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) { for (j in 15 downTo 0) {
var spigotFile: String = "" var spigotFile: String = ""
if (j == 0) if (j == 0)
@ -266,17 +342,26 @@ class PrimaryController {
val hasProperties = File(directory + File.separator + "server.properties").isFile val hasProperties = File(directory + File.separator + "server.properties").isFile
if (hasDummy && hasServer) { if (hasDummy && hasServer) {
// read jproperties // server complete, just read jproperties
println("read jproperties") statusBar.text = "Server found!"
startButton.isDisable = false
} else if (hasDummy && !hasServer && hasProperties) { } else if (hasDummy && !hasServer && hasProperties) {
// just needs to be built // just needs to be built
println("build") startButton.isDisable = true
statusBar.text = "Server needs to be built before starting."
} else if (!hasDummy && hasServer) { } else if (!hasDummy && hasServer) {
// server created externally // 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 { } else {
// assume clean directory // 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; return true;