1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
package xyz.brysonsteck.serverfordummies
import kotlinx.coroutines.*
import kotlinx.coroutines.javafx.JavaFx
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
import javafx.beans.value.ChangeListener
import javafx.beans.value.ObservableValue
import javafx.concurrent.Task
import javafx.beans.property.BooleanProperty
import javafx.collections.FXCollections
import javafx.fxml.FXML
import javafx.geometry.Insets
import javafx.scene.control.Button
import javafx.scene.control.ChoiceBox
import javafx.scene.control.Label
import javafx.scene.control.TextField
import javafx.scene.control.Spinner
import javafx.scene.control.TitledPane
import javafx.scene.control.ButtonBar
import javafx.scene.control.CheckBox
import javafx.scene.control.ProgressBar
import javafx.scene.layout.Border
import javafx.scene.layout.BorderStroke
import javafx.scene.layout.GridPane
import javafx.scene.layout.Pane
import javafx.scene.layout.HBox
import javafx.scene.layout.VBox
import javafx.scene.text.TextAlignment
import javafx.scene.text.Text
import javafx.scene.Scene
import javafx.scene.input.MouseEvent
import javafx.scene.image.Image
import javafx.scene.image.ImageView
import javafx.stage.FileChooser
import javafx.stage.FileChooser.ExtensionFilter
import javafx.stage.DirectoryChooser
import javafx.stage.Modality
import javafx.stage.Stage
import javafx.event.EventHandler
import Download
class PrimaryController {
@FXML
lateinit private var currentDirectoryLabel: Label
@FXML
lateinit private var worldNameField: TextField
@FXML
lateinit private var seedField: TextField
@FXML
lateinit private var portSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var difficultyBox: ChoiceBox<String>
@FXML
lateinit private var gamemodeBox: ChoiceBox<String>
@FXML
lateinit private var worldTypeBox: ChoiceBox<String>
@FXML
lateinit private var worldSettingsPane: HBox
@FXML
lateinit private var parentPane: Pane
@FXML
lateinit private var directoryPane: Pane
@FXML
lateinit private var buttonBar: ButtonBar
@FXML
lateinit private var flightCheckbox: CheckBox
@FXML
lateinit private var netherCheckbox: CheckBox
@FXML
lateinit private var structuresCheckbox: CheckBox
@FXML
lateinit private var pvpCheckbox: CheckBox
@FXML
lateinit private var whitelistCheckbox: CheckBox
@FXML
lateinit private var cmdBlocksCheckbox: CheckBox
@FXML
lateinit private var playerCountCheckbox: CheckBox
@FXML
lateinit private var maxPlayersSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var maxSizeSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var memorySpinner: Spinner<kotlin.Int>
@FXML
lateinit private var spawnSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var simulationSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var renderSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var maxTickSpinner: Spinner<kotlin.Int>
@FXML
lateinit private var statusBar: Label
@FXML
lateinit private var progressBar: ProgressBar
@FXML
lateinit private var startButton: Button
@FXML
lateinit private var buildButton: Button
private var building = false
private var directory = ""
private var asyncResult = false
private var started = false
@FXML
private fun onDirectoryButtonClick() {
val dirChooser = DirectoryChooser()
dirChooser.title = "Open a server directory"
dirChooser.initialDirectory = File(System.getProperty("user.home"))
val result = dirChooser.showDialog(null)
if (result != null) {
currentDirectoryLabel.text = result.absolutePath
val res = loadServerDir(result.absolutePath)
if (res) {
parentPane.isDisable = false
worldSettingsPane.isDisable = false
buttonBar.isDisable = false
} else {
currentDirectoryLabel.text = "<NONE>"
parentPane.isDisable = true
worldSettingsPane.isDisable = true
buttonBar.isDisable = true
}
}
}
@FXML
private fun onWorldNameChange() {
}
@FXML
private fun onSeedChange() {
}
@FXML
private fun onPortChange() {
}
@FXML
private fun onCheckboxClick() {
}
@FXML
private fun onSpinnerChange() {
}
@FXML
private fun onBuild() {
if (building) {
building = false
return;
}
building = true
worldSettingsPane.isDisable = true
directoryPane.isDisable = true
parentPane.isDisable = true
startButton.isDisable = true
buildButton.text = "Cancel Build"
@Suppress("OPT_IN_USAGE")
GlobalScope.launch(Dispatchers.Default) {
progressBar.isVisible = true
var downloads = mapOf(
"BuildTools" to "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar",
)
downloads.forEach {
withContext(Dispatchers.JavaFx){statusBar.text = "Downloading ${it.key}..."}
val download = Download(URL(it.value))
download.start()
while (download.status == Download.Status.DOWNLOADING) {
var prog = (download.downloaded.toDouble() / download.contentLength.toDouble())
// for whatever reason I need to print something to the screen in order for it to update the progress bar
print("")
if (prog >= 0.01) {
withContext(Dispatchers.JavaFx) {progressBar.progress = prog}
}
if (!building) download.status = Download.Status.CANCELLED
Thread.sleep(300)
}
}
progressBar.isVisible = false
withContext(Dispatchers.JavaFx){
worldSettingsPane.isDisable = false
directoryPane.isDisable = false
parentPane.isDisable = false
startButton.isDisable = false
buildButton.text = "Build Server"
statusBar.text = if (building) {"Ready."} else {"Server build cancelled."}
building = false;
}
}
}
@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 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, 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()
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(msg)
label.layoutX = 115.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<MouseEvent>() {
if (hold) {
result = false
} else {
asyncResult = false
}
dialog.hide()
}
val yesButton = Button(yes)
yesButton.onMouseClicked = EventHandler<MouseEvent>() {
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);
if (hold) {
dialog.showAndWait();
} else {
dialog.show();
}
return result
}
private fun loadServerDir(dir: String): Boolean {
directory = dir
var hasServer = false
if (!File(directory).isDirectory) {
return false;
}
if (directory[directory.length-1] != File.separatorChar)
directory += File.separatorChar
val hasDummy = File(directory + "ServerForDummies").isDirectory
// major version
for (i in 25 downTo 8) {
// patch number
for (j in 15 downTo 0) {
var spigotFile: String = ""
if (j == 0)
spigotFile += "spigot-1.$i.jar"
else
spigotFile += "spigot-1.$i.$j.jar";
hasServer = File(directory + spigotFile).isFile || File(directory + "server.jar").isFile
if (hasServer)
break;
}
}
val hasProperties = File(directory + File.separator + "server.properties").isFile
if (hasDummy && hasServer) {
// server complete, just read jproperties
statusBar.text = "Server found!"
startButton.isDisable = false
} else if (hasDummy && !hasServer && hasProperties) {
// just needs to be built
startButton.isDisable = true
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 \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
val result = createDialog("info", "There is no server in this directory.\nCreate one?", "Yes", "No", true)
statusBar.text = "Ready."
return result
}
return true;
}
}
|