Skip to content
This repository has been archived by the owner on Jan 5, 2020. It is now read-only.

Commit

Permalink
keep track of default commands for updating
Browse files Browse the repository at this point in the history
use nullable variables because gson is stupid
  • Loading branch information
NikkyAI committed May 21, 2018
1 parent fd7fd69 commit 1400627
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
15 changes: 8 additions & 7 deletions core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ import matterlink.lazyFormat

data class CustomCommand(
val type: CommandType = CommandType.RESPONSE,
val execute: String = "",
val response: String = "",
val execute: String? = null,
val response: String? = null,
override val permLevel: Double = 0.0,
override val help: String = "",
val allowArgs: Boolean = true,
val timeout: Int = 20
val timeout: Int = 20,
val defaultCommand: Boolean? = null
) : IBridgeCommand {
val alias: String
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 {
if (!allowArgs && args.isNotBlank()) return false

Expand All @@ -45,7 +46,7 @@ data class CustomCommand(
}
CommandType.RESPONSE -> {
MessageHandlerInst.transmit(ApiMessage()
.setText(response.lazyFormat(getReplacements(user, userId, server, args)))
.setText((response ?: "").lazyFormat(getReplacements(user, userId, server, args)))
)
true
}
Expand All @@ -57,8 +58,8 @@ data class CustomCommand(
*/
override fun validate(): Boolean {
val typeCheck = when (type) {
CommandType.EXECUTE -> execute.isNotBlank()
CommandType.RESPONSE -> response.isNotBlank()
CommandType.EXECUTE -> execute?.isNotBlank() ?: false
CommandType.RESPONSE -> response?.isNotBlank() ?: false
}
if (!typeCheck) return false

Expand Down
24 changes: 18 additions & 6 deletions core/src/main/kotlin/matterlink/config/CommandConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import matterlink.instance
import matterlink.stackTraceString
import java.io.File

typealias CommandMap = Map<String, CustomCommand>
typealias CommandMap = MutableMap<String, CustomCommand>

object CommandConfig {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
Expand All @@ -22,33 +22,38 @@ object CommandConfig {
execute = "forge tps",
help = "Print server tps",
allowArgs = false,
timeout = 200
timeout = 200,
defaultCommand = true
),
"list" to CustomCommand(
type = CommandType.EXECUTE,
execute = "list",
help = "List online players",
allowArgs = false
allowArgs = false,
defaultCommand = true
),
"seed" to CustomCommand(
type = CommandType.EXECUTE,
execute = "seed",
help = "Print server world seed",
allowArgs = false
allowArgs = false,
defaultCommand = true
),
"uptime" to CustomCommand(
type = CommandType.RESPONSE,
permLevel = 1.0,
response = "{uptime}",
help = "Print server uptime",
allowArgs = false
allowArgs = false,
defaultCommand = true
),
"whoami" to CustomCommand(
type = CommandType.RESPONSE,
response = "server: `{server}` userid: `{userid}` user: `{user}`",
help = "Print debug user data",
allowArgs = false,
timeout = 200
timeout = 200,
defaultCommand = true
)
)

Expand All @@ -64,6 +69,13 @@ object CommandConfig {

try {
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) {
instance.fatal(e.stackTraceString)
instance.fatal("failed to parse $configFile using last good values as fallback")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod_name = MatterLink
mod_version = 1.5.7
mod_version = 1.5.8
forgelin_version = 1.6.0
curse_id = 287323
curse_release_type = beta

0 comments on commit 1400627

Please sign in to comment.