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"
dependencies {
include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2"))
include(dependency('commons-logging:commons-logging:1.1.3'))
include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.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'

View File

@ -34,9 +34,10 @@ shadowJar {
relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging"
dependencies {
include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2"))
include(dependency('commons-logging:commons-logging:1.1.3'))
include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.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'

View File

@ -34,9 +34,10 @@ shadowJar {
relocate "org.apache.commons.logging", "matterlink.repack.org.apache.commons.logging"
dependencies {
include(project(":core"))
include(dependency("org.apache.httpcomponents:httpclient:4.3.3"))
include(dependency("org.apache.httpcomponents:httpcore:4.3.2"))
include(dependency('commons-logging:commons-logging:1.1.3'))
include(dependency(group: 'org.apache.httpcomponents', name: 'httpcore', version: '4.3.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'

View File

@ -38,9 +38,9 @@ minecraft {
srgExtra "PK: kotlin matterlink/repack/kotlin"
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/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 {
@ -86,6 +86,7 @@ jar {
configurations.shade.each { dep ->
from(project.zipTree(dep)) {
exclude "META-INF", "META-INF/**"
exclude "com/google/gson", "com/google/gson/**"
}
}
}

View File

@ -17,7 +17,7 @@ repositories {
}
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: 'commons-logging', name: 'commons-logging', version: '1.1.3'
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 args = if (cmd.size == 2) cmd[1] else ""
return if (commandMap.containsKey(cmd[0]))
(commandMap[cmd[0]]!!.execute(input.username, input.userid, input.account, args))
else false
return commandMap[cmd[0]]?.execute(cmd[0], input.username, input.userid, input.account, args) ?: false
}
fun register(cmd: IBridgeCommand): Boolean {
if (cmd.alias.isBlank() || commandMap.containsKey(cmd.alias)) {
instance.error("Failed to register command: '${cmd.alias}'")
fun register(alias: String, cmd: IBridgeCommand): Boolean {
if (alias.isBlank() || commandMap.containsKey(alias)) {
instance.error("Failed to register command: '${alias}'")
return false
}
if (!cmd.validate()) {
instance.error("Failed to validate command: '${cmd.alias}'")
instance.error("Failed to validate command: '${alias}'")
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
}
fun registerAll(vararg commands: IBridgeCommand) {
commands.forEach { register(it) }
}
fun getHelpString(cmd: String): String {
if (!commandMap.containsKey(cmd)) return "No such command."
@ -60,10 +56,19 @@ object BridgeCommandRegistry {
fun reloadCommands() {
commandMap.clear()
val permStatus = PermissionConfig.loadPermFile()
register(HelpCommand)
register("help", HelpCommand)
val cmdStatus = CommandConfig.readConfig()
registerAll(*CommandConfig.commands)
CommandConfig.commands.forEach { (alias, command) ->
register(alias, 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
data class CustomCommand(
override val alias: String,
val type: CommandType = CommandType.RESPONSE,
val execute: String = "",
val response: String = "",
@ -16,9 +15,11 @@ data class CustomCommand(
val allowArgs: Boolean = true,
val timeout: Int = 20
) : IBridgeCommand {
val alias: String
get() = BridgeCommandRegistry.getName(this)!!
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 (TickHandler.tickCounter - lastUsed < timeout)
@ -61,13 +62,16 @@ data class CustomCommand(
return true
}
fun getReplacements(user: String, userId: String, server: String, args: String): Map<String, () -> String> = mapOf(
"{uptime}" to instance::getUptimeAsString,
"{user}" to { user },
"{userid}" to { userId },
"{server}" to { server },
"{args}" to { args }
)
companion object {
fun getReplacements(user: String, userId: String, server: String, args: String): Map<String, () -> String> = mapOf(
"{uptime}" to instance::getUptimeAsString,
"{user}" to { user },
"{userid}" to { userId },
"{server}" to { server },
"{args}" to { args }
)
}
}
enum class CommandType {

View File

@ -5,10 +5,9 @@ import matterlink.bridge.MessageHandler
import matterlink.config.cfg
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 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 {
args.isEmpty() ->
"Available commands: ${BridgeCommandRegistry.getCommandList(IBridgeCommand.getPermLevel(userId, server))}"

View File

@ -3,11 +3,10 @@ package matterlink.bridge.command
import matterlink.config.PermissionConfig
interface IBridgeCommand {
val alias: String
val help: String
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 {
return getPermLevel(userId, server) >= permLevel

View File

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

View File

@ -5,38 +5,42 @@ import com.google.gson.GsonBuilder
import com.google.gson.JsonSyntaxException
import com.google.gson.reflect.TypeToken
import matterlink.instance
import matterlink.stackTraceString
import java.io.File
typealias PermissionMap = HashMap<String, HashMap<String, Int>>
typealias PermissionMap = Map<String, Map<String, Int>>
object PermissionConfig {
private val gson: Gson = GsonBuilder().setPrettyPrinting().create()
private val configFile: File = cfg.cfgDirectory.resolve("permissions.json")
private val default: PermissionMap = hashMapOf(
"irc.esper" to hashMapOf(
private val default = mapOf(
"irc.esper" to mapOf(
"~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
fun loadPermFile(): Boolean {
if (!configFile.exists()) {
if (!configFile.exists() || configFile.readText().isBlank()) {
configFile.createNewFile()
perms = default
configFile.writeText(gson.toJson(default))
return true
}
val text = configFile.readText()
try {
perms = gson.fromJson(text, object : TypeToken<PermissionMap>() {}.type)
perms =gson.fromJson(configFile.readText(), object : TypeToken<PermissionMap>() {}.type)
} catch (e: JsonSyntaxException) {
instance.fatal(e.stackTraceString)
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_version = 1.5.1
mod_version = 1.5.2
forgelin_version = 1.6.0
curse_id = 287323
curse_release_type = beta