diff options
author | Bryson Steck <brysonsteck@protonmail.com> | 2023-05-14 13:44:35 -0600 |
---|---|---|
committer | Bryson Steck <brysonsteck@protonmail.com> | 2023-05-14 13:44:35 -0600 |
commit | 13c4c1cfc0f2ddcf53a9b2e4a1a516b795adb4f6 (patch) | |
tree | a2123bcd4e09107e7e1a79428e16c54474940a97 /app/src/main/kotlin/xyz/brysonsteck/servercraft/server | |
parent | 8fbd006b494ce4c6ab284d9eeab8692195984151 (diff) | |
download | ServerCraft-13c4c1cfc0f2ddcf53a9b2e4a1a516b795adb4f6.tar ServerCraft-13c4c1cfc0f2ddcf53a9b2e4a1a516b795adb4f6.tar.gz ServerCraft-13c4c1cfc0f2ddcf53a9b2e4a1a516b795adb4f6.tar.bz2 |
complete rename to ServerCraft
Diffstat (limited to 'app/src/main/kotlin/xyz/brysonsteck/servercraft/server')
-rw-r--r-- | app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Download.kt | 119 | ||||
-rw-r--r-- | app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Server.kt | 35 |
2 files changed, 154 insertions, 0 deletions
diff --git a/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Download.kt b/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Download.kt new file mode 100644 index 0000000..a10ff69 --- /dev/null +++ b/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Download.kt @@ -0,0 +1,119 @@ +package xyz.brysonsteck.servercraft.server + +import java.io.*; +import java.net.*; +import java.util.*; + +class Download: Runnable { + public enum class Status { + DOWNLOADING, PAUSED, COMPLETE, CANCELLED, ERROR + } + public var size: Int + public var downloaded: Int + public var contentLength: Int + public var status: Status + + private final val MAX_BUFFER_SIZE: Number = 1024 + + private var url: URL + private var dir: String + + constructor (url: URL, dir: String) { + this.url = url + this.dir = dir + size = -1 + downloaded = 0 + status = Status.DOWNLOADING + contentLength = 1 + } + + public fun start() { + val thread = Thread(this) + thread.start() + } + + private fun getFilename(url: URL): String { + val filename = url.getFile() + return filename.substring(filename.lastIndexOf('/') + 1) + } + + override fun run() { + var stream: InputStream? = null + var file: RandomAccessFile? = null + + try { + // Open connection to URL. + var connection = url.openConnection() as HttpURLConnection; + + // Specify what portion of file to download. + connection.setRequestProperty("Range", "bytes=" + downloaded + "-"); + + // Connect to server. + connection.connect(); + + // Make sure response code is in the 200 range. + if (connection.responseCode / 100 != 2) { + status = Status.ERROR + } + + // Check for valid content length. + contentLength = connection.getContentLength(); + if (contentLength < 1) { + status = Status.ERROR + } + + /* Set the size for this download if it + hasn't been already set. */ + if (size == -1) { + size = contentLength; + } + + // Open file and seek to the end of it. + file = RandomAccessFile(dir + getFilename(url), "rw"); + file.seek(downloaded.toLong()); + + stream = connection.getInputStream(); + while (status == Status.DOWNLOADING) { + /* Size buffer according to how much of the + file is left to download. */ + val buffer: ByteArray; + if (size - downloaded > MAX_BUFFER_SIZE as Int) { + buffer = ByteArray(MAX_BUFFER_SIZE) + } else { + buffer = ByteArray(size - downloaded); + } + + // Read from server into buffer. + val read = stream.read(buffer); + if (read == -1) + break; + + // Write buffer to file. + file.write(buffer, 0, read); + downloaded += read; + } + + /* Change status to complete if this point was + reached because downloading has finished. */ + if (status == Status.DOWNLOADING) { + status = Status.COMPLETE; + } + } catch (e: Exception) { + status = Status.ERROR + } finally { + // Close file. + if (file != null) { + try { + file.close(); + } catch (e: Exception) {} + } + + // Close connection to server. + if (stream != null) { + try { + stream.close(); + } catch (e: Exception) {} + } + } + } +} diff --git a/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Server.kt b/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Server.kt new file mode 100644 index 0000000..38e79a1 --- /dev/null +++ b/app/src/main/kotlin/xyz/brysonsteck/servercraft/server/Server.kt @@ -0,0 +1,35 @@ +package xyz.brysonsteck.servercraft.server + +import java.io.File +import java.util.Properties + +public class Server { + public var jar = "" + + private val props = Properties() + + constructor() { + props.setProperty("allow-flight", false.toString()) + props.setProperty("allow-nether", true.toString()) + props.setProperty("generate-structures", true.toString()) + props.setProperty("hardcore", false.toString()) + props.setProperty("pvp", true.toString()) + props.setProperty("white-list", false.toString()) + props.setProperty("enable-command-block", false.toString()) + props.setProperty("hide-online-players", false.toString()) + props.setProperty("max-players", 20.toString()) + props.setProperty("max-world-size", 29999984.toString()) + props.setProperty("server-port", 25565.toString()) + props.setProperty("view-distance", 10.toString()) + props.setProperty("jvm-ram", 1024.toString()) + props.setProperty("spawn-protection", 16.toString()) + props.setProperty("simulation-distance", 10.toString()) + props.setProperty("max-tick-time", 60000.toString()) + props.setProperty("difficulty", "normal") + props.setProperty("gamemode", "survival") + props.setProperty("level-name", "world") + props.setProperty("level-seed", "") + props.setProperty("level-type", "minecraft:normal") + props.setProperty("motd", "A server for a dummy") + } +}
\ No newline at end of file |