Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/DockyardMC/Dockyard
Browse files Browse the repository at this point in the history
  • Loading branch information
LukynkaCZE committed Dec 29, 2024
2 parents a7dfcec + 60473e1 commit 8630e39
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/main/kotlin/io/github/dockyardmc/motd/ServerStatus.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.dockyardmc.motd

import cz.lukynka.Bindable
import io.github.dockyardmc.DockyardServer
import io.github.dockyardmc.config.ConfigManager
import io.github.dockyardmc.player.PlayerManager
Expand All @@ -10,14 +11,59 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import java.io.File
import java.net.URL
import java.util.*

object ServerStatusManager {

private lateinit var cache: ServerStatus

private val iconFile = File("./icon.png")
val base64EncodedIcon = if(iconFile.exists()) Base64.getEncoder().encode(iconFile.readBytes()).decodeToString() else ""

private val base64EncodedIcon = Bindable<String>(
if(iconFile.exists()) Base64.getEncoder().encode(iconFile.readBytes()).decodeToString()
else ""
)

val description = Bindable<String>("${Branding.logo} <gray>Custom Kotlin Server Implementation")

init {
description.valueChanged { updateCache() }
base64EncodedIcon.valueChanged { updateCache() }
}

/**
* Set the server icon from a file. If the file does not exist, the icon
* will be cleared.
*
* @param file The icon file
*/
fun setIconFromFile(file: File) {
base64EncodedIcon.value =
if (file.exists()) Base64.getEncoder().encode(file.readBytes()).decodeToString()
else ""
}

/**
* Set the server icon from a classpath resource
*
* @param resource The resource URL
*
* @see Class.getResource
*/
fun setIconFromResource(resource: URL) {
base64EncodedIcon.value =
Base64.getEncoder().encode(resource.readBytes()).decodeToString()
}

/**
* Set the server description (MOTD string)
*
* @param description The description
*/
fun setDescription(description: String) {
this.description.value = description
}

fun getCache(): ServerStatus {
if(!this::cache.isInitialized) updateCache()
Expand All @@ -40,10 +86,10 @@ object ServerStatusManager {
online = PlayerManager.players.size,
sample = playersOnline,
),
description = "${Branding.logo} <gray>Custom Kotlin Server Implementation".toComponent(),
description = description.value.toComponent(),
enforceSecureChat = false,
previewsChat = false,
favicon = "data:image/png;base64,$base64EncodedIcon"
favicon = "data:image/png;base64,${base64EncodedIcon.value}"
)
}
private val json = getCache().toJson()
Expand Down

0 comments on commit 8630e39

Please sign in to comment.