add custom command: uptime to the defaults

This commit is contained in:
Nikky Ai 2018-02-21 05:23:01 +01:00
parent 172305482d
commit f6b48085b0
3 changed files with 31 additions and 15 deletions

View File

@ -5,7 +5,6 @@ import matterlink.bridge.command.BridgeCommandRegistry
import matterlink.bridge.command.IMinecraftCommandSender import matterlink.bridge.command.IMinecraftCommandSender
import matterlink.config.cfg import matterlink.config.cfg
import matterlink.update.UpdateChecker import matterlink.update.UpdateChecker
import java.time.Duration
lateinit var instance: IMatterLink lateinit var instance: IMatterLink
@ -52,20 +51,29 @@ abstract class IMatterLink {
*/ */
var serverStartTime: Long = 0 var serverStartTime: Long = 0
fun getUptimeInSeconds(): Int { fun getUptimeInSeconds(): Long {
return ((System.currentTimeMillis() - serverStartTime) / 1000).toInt() return (System.currentTimeMillis() - serverStartTime) / 1000
} }
fun getUptimeAsString(): String { fun getUptimeAsString(): String {
val duration = Duration.ofSeconds((System.currentTimeMillis() - serverStartTime) / 1000) val total = this.getUptimeInSeconds()
return duration.toString() val s = total % 60
// val total = this.getUptimeInSeconds() val m = (total / 60) % 60
// val sec = total % 60 val h = (total / 3600) % 24
// val min = (total / 60) % 60 val d = total / 86400
// val hr = (total / 3600) % 24
// val day = total / 86400 fun timeFormat(unit: Long, name: String) = when {
// unit > 1L -> "$unit ${name}s "
// return "${day}d ${hr}hr ${min}m ${sec}s" unit == 1L -> "$unit $name "
else -> ""
}
var result = ""
result += timeFormat(d, "Day")
result += timeFormat(h, "Hour")
result += timeFormat(m, "Minute")
result += timeFormat(s, "Second")
return result
} }
fun registerBridgeCommands() { fun registerBridgeCommands() {

View File

@ -49,9 +49,9 @@ data class CustomCommand(
} }
fun getReplacements(user: String, args: String): Map<String, () -> String> = mapOf( fun getReplacements(user: String, args: String): Map<String, () -> String> = mapOf(
"{UPTIME}" to instance::getUptimeAsString, "{uptime}" to instance::getUptimeAsString,
"{USER}" to { user }, "{users}" to { user },
"{ARGS}" to { args } "{args}" to { args }
) )
} }

View File

@ -32,6 +32,14 @@ object CommandConfig {
execute = "seed", execute = "seed",
help = "Print server world seed", help = "Print server world seed",
allowArgs = false allowArgs = false
),
CustomCommand(
alias = "uptime",
type = CommandType.PASSTHROUGH,
permLevel = 1,
response = "{uptime}",
help = "Print server world seed",
allowArgs = false
) )
) )
var commands: Array<CustomCommand> = default var commands: Array<CustomCommand> = default