diff --git a/1.10.2/build.gradle b/1.10.2/build.gradle index e6b3e79..6f27176 100644 --- a/1.10.2/build.gradle +++ b/1.10.2/build.gradle @@ -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' diff --git a/1.11.2/build.gradle b/1.11.2/build.gradle index 2a2eb74..8004d6f 100644 --- a/1.11.2/build.gradle +++ b/1.11.2/build.gradle @@ -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' diff --git a/1.12.2/build.gradle b/1.12.2/build.gradle index 47e7b57..457d737 100644 --- a/1.12.2/build.gradle +++ b/1.12.2/build.gradle @@ -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' diff --git a/1.7.10/build.gradle b/1.7.10/build.gradle index e62ca49..2f71ff0 100644 --- a/1.7.10/build.gradle +++ b/1.7.10/build.gradle @@ -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/**" } } } diff --git a/core/build.gradle b/core/build.gradle index 90ece1f..41bf146 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -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' diff --git a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt index 0780acf..b41b946 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/BridgeCommandRegistry.kt @@ -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 + } } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt index 88197e4..94039b6 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt @@ -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> = 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> = mapOf( + "{uptime}" to instance::getUptimeAsString, + "{user}" to { user }, + "{userid}" to { userId }, + "{server}" to { server }, + "{args}" to { args } + ) + } } enum class CommandType { diff --git a/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt index 5c11a7f..0aa5d58 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/HelpCommand.kt @@ -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 " 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))}" diff --git a/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt index 80e28ca..9c533e1 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/IBridgeCommand.kt @@ -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 diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index 2f3f523..2dee27e 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -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 + 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 = 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::class.java) + commands = gson.fromJson(configFile.readText(), object : TypeToken() {}.type) } catch (e: JsonSyntaxException) { + instance.fatal(e.stackTraceString) instance.fatal("failed to parse $configFile using last good values as fallback") + return false } return true diff --git a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt index bcf25de..11a19b3 100644 --- a/core/src/main/kotlin/matterlink/config/PermissionConfig.kt +++ b/core/src/main/kotlin/matterlink/config/PermissionConfig.kt @@ -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> +typealias PermissionMap = Map> 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() {}.type) + perms =gson.fromJson(configFile.readText(), object : TypeToken() {}.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 } } \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index a0b56f7..20bd86b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -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 \ No newline at end of file