keep track of default commands for updating

use nullable variables because gson is stupid
This commit is contained in:
nikky 2018-05-21 12:00:55 +02:00
parent fd7fd69306
commit 1400627187
3 changed files with 27 additions and 14 deletions

View File

@ -8,17 +8,18 @@ import matterlink.lazyFormat
data class CustomCommand( data class CustomCommand(
val type: CommandType = CommandType.RESPONSE, val type: CommandType = CommandType.RESPONSE,
val execute: String = "", val execute: String? = null,
val response: String = "", val response: String? = null,
override val permLevel: Double = 0.0, override val permLevel: Double = 0.0,
override val help: String = "", override val help: String = "",
val allowArgs: Boolean = true, val allowArgs: Boolean = true,
val timeout: Int = 20 val timeout: Int = 20,
val defaultCommand: Boolean? = null
) : IBridgeCommand { ) : IBridgeCommand {
val alias: String val alias: String
get() = BridgeCommandRegistry.getName(this)!! get() = BridgeCommandRegistry.getName(this)!!
var lastUsed: Int = 0 @Transient private var lastUsed: Int = 0
override fun execute(alias: String, user: String, userId: String, server: String, args: String): Boolean { override fun execute(alias: String, user: String, userId: String, server: String, args: String): Boolean {
if (!allowArgs && args.isNotBlank()) return false if (!allowArgs && args.isNotBlank()) return false
@ -45,7 +46,7 @@ data class CustomCommand(
} }
CommandType.RESPONSE -> { CommandType.RESPONSE -> {
MessageHandlerInst.transmit(ApiMessage() MessageHandlerInst.transmit(ApiMessage()
.setText(response.lazyFormat(getReplacements(user, userId, server, args))) .setText((response ?: "").lazyFormat(getReplacements(user, userId, server, args)))
) )
true true
} }
@ -57,8 +58,8 @@ data class CustomCommand(
*/ */
override fun validate(): Boolean { override fun validate(): Boolean {
val typeCheck = when (type) { val typeCheck = when (type) {
CommandType.EXECUTE -> execute.isNotBlank() CommandType.EXECUTE -> execute?.isNotBlank() ?: false
CommandType.RESPONSE -> response.isNotBlank() CommandType.RESPONSE -> response?.isNotBlank() ?: false
} }
if (!typeCheck) return false if (!typeCheck) return false

View File

@ -10,7 +10,7 @@ import matterlink.instance
import matterlink.stackTraceString import matterlink.stackTraceString
import java.io.File import java.io.File
typealias CommandMap = Map<String, CustomCommand> typealias CommandMap = MutableMap<String, CustomCommand>
object CommandConfig { object CommandConfig {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create() private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
@ -22,33 +22,38 @@ object CommandConfig {
execute = "forge tps", execute = "forge tps",
help = "Print server tps", help = "Print server tps",
allowArgs = false, allowArgs = false,
timeout = 200 timeout = 200,
defaultCommand = true
), ),
"list" to CustomCommand( "list" to CustomCommand(
type = CommandType.EXECUTE, type = CommandType.EXECUTE,
execute = "list", execute = "list",
help = "List online players", help = "List online players",
allowArgs = false allowArgs = false,
defaultCommand = true
), ),
"seed" to CustomCommand( "seed" to CustomCommand(
type = CommandType.EXECUTE, type = CommandType.EXECUTE,
execute = "seed", execute = "seed",
help = "Print server world seed", help = "Print server world seed",
allowArgs = false allowArgs = false,
defaultCommand = true
), ),
"uptime" to CustomCommand( "uptime" to CustomCommand(
type = CommandType.RESPONSE, type = CommandType.RESPONSE,
permLevel = 1.0, permLevel = 1.0,
response = "{uptime}", response = "{uptime}",
help = "Print server uptime", help = "Print server uptime",
allowArgs = false allowArgs = false,
defaultCommand = true
), ),
"whoami" to CustomCommand( "whoami" to CustomCommand(
type = CommandType.RESPONSE, type = CommandType.RESPONSE,
response = "server: `{server}` userid: `{userid}` user: `{user}`", response = "server: `{server}` userid: `{userid}` user: `{user}`",
help = "Print debug user data", help = "Print debug user data",
allowArgs = false, allowArgs = false,
timeout = 200 timeout = 200,
defaultCommand = true
) )
) )
@ -64,6 +69,13 @@ object CommandConfig {
try { try {
commands = gson.fromJson(configFile.readText(), object : TypeToken<CommandMap>() {}.type) commands = gson.fromJson(configFile.readText(), object : TypeToken<CommandMap>() {}.type)
commands.filterValues { it.defaultCommand ?: false }.forEach { commands.remove(it.key) }
default.forEach { k, v ->
if(!commands.containsKey(k)){
commands[k] = v
}
}
configFile.writeText(gson.toJson(commands))
} catch (e: JsonSyntaxException) { } catch (e: JsonSyntaxException) {
instance.fatal(e.stackTraceString) instance.fatal(e.stackTraceString)
instance.fatal("failed to parse $configFile using last good values as fallback") instance.fatal("failed to parse $configFile using last good values as fallback")

View File

@ -1,5 +1,5 @@
mod_name = MatterLink mod_name = MatterLink
mod_version = 1.5.7 mod_version = 1.5.8
forgelin_version = 1.6.0 forgelin_version = 1.6.0
curse_id = 287323 curse_id = 287323
curse_release_type = beta curse_release_type = beta