improve command aliases (#4)

* redo alias in commands and use hocon for commands and permission

* fix packaging issue on 1.1x.2 builds

* revert attempts at hocon
This commit is contained in:
NikkyAI 2018-02-26 05:15:43 +01:00 committed by GitHub
parent c5fd53a00c
commit 4e8fba5725
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 81 additions and 63 deletions

View File

@ -35,9 +35,10 @@ shadowJar {
relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging" relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging"
dependencies { dependencies {
include(project(":core")) include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2")) include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'))
include(dependency('commons-logging:commons-logging:1.1.3')) include(dependency(group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'))
include(dependency(group: 'commons-logging', name: 'commons-logging', version: '1.1.3'))
} }
exclude 'dummyThing' exclude 'dummyThing'

View File

@ -34,9 +34,10 @@ shadowJar {
relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging" relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging"
dependencies { dependencies {
include(project(":core")) include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2")) include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'))
include(dependency('commons-logging:commons-logging:1.1.3')) include(dependency(group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'))
include(dependency(group: 'commons-logging', name: 'commons-logging', version: '1.1.3'))
} }
exclude 'dummyThing' exclude 'dummyThing'

View File

@ -34,9 +34,10 @@ shadowJar {
relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging" relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging"
dependencies { dependencies {
include(project(":core")) include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2")) include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'))
include(dependency('commons-logging:commons-logging:1.1.3')) include(dependency(group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'))
include(dependency(group: 'commons-logging', name: 'commons-logging', version: '1.1.3'))
} }
exclude 'dummyThing' exclude 'dummyThing'

View File

@ -38,9 +38,9 @@ minecraft {
srgExtra "PK: kotlin matterlink/repack/kotlin" srgExtra "PK: kotlin matterlink/repack/kotlin"
srgExtra "PK: org/jetbrains/annotations matterlink/repack/org/jetbrains/annotations" srgExtra "PK: org/jetbrains/annotations matterlink/repack/org/jetbrains/annotations"
srgExtra "PK: com/google/gson matterlink/repack/com/google/gson"
srgExtra "PK: org/apache/http matterlink/repack/org/apache/http" srgExtra "PK: org/apache/http matterlink/repack/org/apache/http"
srgExtra "PK: org/intelij matterlink/repack/org/intelij" //srgExtra "PK: org/apache/commons matterlink/repack/org/apache/commons"
srgExtra "PK: org/intellij matterlink/repack/org/intellij"
} }
compileKotlin.doFirst { compileKotlin.doFirst {
@ -86,6 +86,7 @@ jar {
configurations.shade.each { dep -> configurations.shade.each { dep ->
from(project.zipTree(dep)) { from(project.zipTree(dep)) {
exclude "META-INF", "META-INF/**" exclude "META-INF", "META-INF/**"
exclude "com/google/gson", "com/google/gson/**"
} }
} }
} }

View File

@ -17,7 +17,7 @@ repositories {
} }
dependencies { dependencies {
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3' compile group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.3'
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'
compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3' compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0' compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'

View File

@ -18,28 +18,24 @@ object BridgeCommandRegistry {
val cmd = input.text.substring(1).split(' ', ignoreCase = false, limit = 2) val cmd = input.text.substring(1).split(' ', ignoreCase = false, limit = 2)
val args = if (cmd.size == 2) cmd[1] else "" val args = if (cmd.size == 2) cmd[1] else ""
return if (commandMap.containsKey(cmd[0])) return commandMap[cmd[0]]?.execute(cmd[0], input.username, input.userid, input.account, args) ?: false
(commandMap[cmd[0]]!!.execute(input.username, input.userid, input.account, args))
else false
} }
fun register(cmd: IBridgeCommand): Boolean { fun register(alias: String, cmd: IBridgeCommand): Boolean {
if (cmd.alias.isBlank() || commandMap.containsKey(cmd.alias)) { if (alias.isBlank() || commandMap.containsKey(alias)) {
instance.error("Failed to register command: '${cmd.alias}'") instance.error("Failed to register command: '${alias}'")
return false return false
} }
if (!cmd.validate()) { if (!cmd.validate()) {
instance.error("Failed to validate command: '${cmd.alias}'") instance.error("Failed to validate command: '${alias}'")
return false return false
} }
commandMap[cmd.alias] = cmd //TODO: maybe write alias to command here ?
// could avoid searching for the command in the registry
commandMap[alias] = cmd
return true return true
} }
fun registerAll(vararg commands: IBridgeCommand) {
commands.forEach { register(it) }
}
fun getHelpString(cmd: String): String { fun getHelpString(cmd: String): String {
if (!commandMap.containsKey(cmd)) return "No such command." if (!commandMap.containsKey(cmd)) return "No such command."
@ -60,10 +56,19 @@ object BridgeCommandRegistry {
fun reloadCommands() { fun reloadCommands() {
commandMap.clear() commandMap.clear()
val permStatus = PermissionConfig.loadPermFile() val permStatus = PermissionConfig.loadPermFile()
register(HelpCommand) register("help", HelpCommand)
val cmdStatus = CommandConfig.readConfig() val cmdStatus = CommandConfig.readConfig()
registerAll(*CommandConfig.commands) CommandConfig.commands.forEach { (alias, command) ->
register(alias, command)
}
} }
operator fun get(command: String) = commandMap[command] operator fun get(command: String) = commandMap[command]
fun getName(command: IBridgeCommand): String? {
commandMap.forEach{(alias, cmd) ->
if(command == cmd) return alias
}
return null
}
} }

View File

@ -7,7 +7,6 @@ import matterlink.instance
import matterlink.lazyFormat import matterlink.lazyFormat
data class CustomCommand( data class CustomCommand(
override val alias: String,
val type: CommandType = CommandType.RESPONSE, val type: CommandType = CommandType.RESPONSE,
val execute: String = "", val execute: String = "",
val response: String = "", val response: String = "",
@ -16,9 +15,11 @@ data class CustomCommand(
val allowArgs: Boolean = true, val allowArgs: Boolean = true,
val timeout: Int = 20 val timeout: Int = 20
) : IBridgeCommand { ) : IBridgeCommand {
val alias: String
get() = BridgeCommandRegistry.getName(this)!!
var lastUsed: Int = 0 var lastUsed: Int = 0
override fun execute(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
if (TickHandler.tickCounter - lastUsed < timeout) if (TickHandler.tickCounter - lastUsed < timeout)
@ -61,13 +62,16 @@ data class CustomCommand(
return true return true
} }
fun getReplacements(user: String, userId: String, server: String, args: String): Map<String, () -> String> = mapOf( companion object {
"{uptime}" to instance::getUptimeAsString,
"{user}" to { user }, fun getReplacements(user: String, userId: String, server: String, args: String): Map<String, () -> String> = mapOf(
"{userid}" to { userId }, "{uptime}" to instance::getUptimeAsString,
"{server}" to { server }, "{user}" to { user },
"{args}" to { args } "{userid}" to { userId },
) "{server}" to { server },
"{args}" to { args }
)
}
} }
enum class CommandType { enum class CommandType {

View File

@ -5,10 +5,9 @@ import matterlink.bridge.MessageHandler
import matterlink.config.cfg import matterlink.config.cfg
object HelpCommand : IBridgeCommand { object HelpCommand : IBridgeCommand {
override val alias: String = "help"
override val help: String = "Returns the help string for the given command. Syntax: help <command>" override val help: String = "Returns the help string for the given command. Syntax: help <command>"
override val permLevel = 0 override val permLevel = 0
override fun execute(user: String, userId: String, server: String, args: String): Boolean { override fun execute(alias: String, user: String, userId: String, server: String, args: String): Boolean {
val msg: String = when { val msg: String = when {
args.isEmpty() -> args.isEmpty() ->
"Available commands: ${BridgeCommandRegistry.getCommandList(IBridgeCommand.getPermLevel(userId, server))}" "Available commands: ${BridgeCommandRegistry.getCommandList(IBridgeCommand.getPermLevel(userId, server))}"

View File

@ -3,11 +3,10 @@ package matterlink.bridge.command
import matterlink.config.PermissionConfig import matterlink.config.PermissionConfig
interface IBridgeCommand { interface IBridgeCommand {
val alias: String
val help: String val help: String
val permLevel: Int val permLevel: Int
fun execute(user: String, userId: String, server: String, args: String): Boolean fun execute(alias: String, user: String, userId: String, server: String, args: String): Boolean
fun canExecute(userId: String, server: String): Boolean { fun canExecute(userId: String, server: String): Boolean {
return getPermLevel(userId, server) >= permLevel return getPermLevel(userId, server) >= permLevel

View File

@ -3,38 +3,39 @@ package matterlink.config
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import matterlink.bridge.command.CommandType import matterlink.bridge.command.CommandType
import matterlink.bridge.command.CustomCommand import matterlink.bridge.command.CustomCommand
import matterlink.instance import matterlink.instance
import matterlink.stackTraceString
import java.io.File import java.io.File
typealias CommandMap = Map<String, CustomCommand>
object CommandConfig { object CommandConfig {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create() private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
private val configFile: File = cfg.cfgDirectory.resolve("commands.json") private val configFile: File = cfg.cfgDirectory.resolve("commands.json")
private val default = arrayOf(
CustomCommand( private val default = hashMapOf(
alias = "tps", "tps" to CustomCommand(
type = CommandType.EXECUTE, type = CommandType.EXECUTE,
execute = "forge tps", execute = "forge tps",
help = "Print server tps", help = "Print server tps",
allowArgs = false allowArgs = false
), ),
CustomCommand( "list" to CustomCommand(
alias = "list",
type = CommandType.EXECUTE, type = CommandType.EXECUTE,
execute = "list", execute = "list",
help = "List online players", help = "List online players",
allowArgs = false allowArgs = false
), ),
CustomCommand( "seed" to CustomCommand(
alias = "seed",
type = CommandType.EXECUTE, type = CommandType.EXECUTE,
execute = "seed", execute = "seed",
help = "Print server world seed", help = "Print server world seed",
allowArgs = false allowArgs = false
), ),
CustomCommand( "uptime" to CustomCommand(
alias = "uptime",
type = CommandType.RESPONSE, type = CommandType.RESPONSE,
permLevel = 1, permLevel = 1,
response = "{uptime}", response = "{uptime}",
@ -42,21 +43,23 @@ object CommandConfig {
allowArgs = false allowArgs = false
) )
) )
var commands: Array<CustomCommand> = default
var commands: CommandMap = default
private set private set
fun readConfig(): Boolean { fun readConfig(): Boolean {
if (!configFile.exists()) { if (!configFile.exists() || configFile.readText().isBlank()) {
configFile.createNewFile() configFile.createNewFile()
configFile.writeText(gson.toJson(default)) configFile.writeText(gson.toJson(default))
return true return true
} }
val text = configFile.readText()
try { try {
commands = gson.fromJson(text, Array<CustomCommand>::class.java) commands = gson.fromJson(configFile.readText(), object : TypeToken<CommandMap>() {}.type)
} catch (e: JsonSyntaxException) { } catch (e: JsonSyntaxException) {
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")
return false return false
} }
return true return true

View File

@ -5,38 +5,42 @@ import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import matterlink.instance import matterlink.instance
import matterlink.stackTraceString
import java.io.File import java.io.File
typealias PermissionMap = HashMap<String, HashMap<String, Int>> typealias PermissionMap = Map<String, Map<String, Int>>
object PermissionConfig { object PermissionConfig {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create() private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
private val configFile: File = cfg.cfgDirectory.resolve("permissions.json") private val configFile: File = cfg.cfgDirectory.resolve("permissions.json")
private val default: PermissionMap = hashMapOf( private val default = mapOf(
"irc.esper" to hashMapOf( "irc.esper" to mapOf(
"~nikky@nikky.moe" to 0, "~nikky@nikky.moe" to 0,
"user@example.com" to 0 "user@example." to 0
),
"discord.game" to mapOf(
"112228624366575616" to 0
) )
) )
var perms: PermissionMap = default var perms: PermissionMap = default
fun loadPermFile(): Boolean { fun loadPermFile(): Boolean {
if (!configFile.exists()) { if (!configFile.exists() || configFile.readText().isBlank()) {
configFile.createNewFile() configFile.createNewFile()
perms = default
configFile.writeText(gson.toJson(default)) configFile.writeText(gson.toJson(default))
return true return true
} }
val text = configFile.readText()
try { try {
perms = gson.fromJson(text, object : TypeToken<PermissionMap>() {}.type) perms =gson.fromJson(configFile.readText(), object : TypeToken<PermissionMap>() {}.type)
} catch (e: JsonSyntaxException) { } catch (e: JsonSyntaxException) {
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")
return false
return true
} }
return true return false
} }
} }

View File

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