From c5fd53a00c9553cc707c8b90c91667f8330bed96 Mon Sep 17 00:00:00 2001 From: Nikky Ai Date: Sun, 25 Feb 2018 00:51:56 +0100 Subject: [PATCH] add timeous to commands --- core/build.gradle | 11 +++++----- .../bridge/command/CustomCommand.kt | 21 +++++++++++++++---- .../kotlin/matterlink/config/CommandConfig.kt | 6 +++--- .../kotlin/matterlink/handlers/TickHandler.kt | 7 ++++--- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index 8d32838..90ece1f 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -17,11 +17,12 @@ repositories { } dependencies { - compile 'org.apache.httpcomponents:httpclient:4.3.3' - compile 'commons-logging:commons-logging:1.1.3' -// compile group: "org.apache.logging.log4j", name: "log4j-api", version: '2.8.1' - compile 'com.google.code.gson:gson:2.8.0' - compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + 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: 'com.google.code.gson', name: 'gson', version: '2.8.0' + + compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: project.kotlin_version } compileKotlin { diff --git a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt index bbc9b5b..88197e4 100644 --- a/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt +++ b/core/src/main/kotlin/matterlink/bridge/command/CustomCommand.kt @@ -2,6 +2,7 @@ package matterlink.bridge.command import matterlink.bridge.ApiMessage import matterlink.bridge.MessageHandler +import matterlink.handlers.TickHandler import matterlink.instance import matterlink.lazyFormat @@ -12,17 +13,29 @@ data class CustomCommand( val response: String = "", override val permLevel: Int = 0, override val help: String = "", - val allowArgs: Boolean = true + val allowArgs: Boolean = true, + val timeout: Int = 20 ) : IBridgeCommand { + + var lastUsed: Int = 0 override fun execute(user: String, userId: String, server: String, args: String): Boolean { if (!allowArgs && args.isNotBlank()) return false + + if (TickHandler.tickCounter - lastUsed < timeout) + { + instance.debug("dropped command $alias") + return true //eat command silently + } + if (!canExecute(userId, server)) { MessageHandler.transmit(ApiMessage(text = "$user is not permitted to perform command: $alias")) return false } + lastUsed = TickHandler.tickCounter + return when (type) { - CommandType.PASSTHROUGH -> { + CommandType.EXECUTE -> { //uses a new commandsender for each user // TODO: cache CommandSenders val commandSender = instance.commandSenderFor(user, userId, server) @@ -40,7 +53,7 @@ data class CustomCommand( */ override fun validate(): Boolean { val typeCheck = when (type) { - CommandType.PASSTHROUGH -> execute.isNotBlank() + CommandType.EXECUTE -> execute.isNotBlank() CommandType.RESPONSE -> response.isNotBlank() } if (!typeCheck) return false @@ -58,5 +71,5 @@ data class CustomCommand( } enum class CommandType { - PASSTHROUGH, RESPONSE + EXECUTE, RESPONSE } \ No newline at end of file diff --git a/core/src/main/kotlin/matterlink/config/CommandConfig.kt b/core/src/main/kotlin/matterlink/config/CommandConfig.kt index af2a22e..2f3f523 100644 --- a/core/src/main/kotlin/matterlink/config/CommandConfig.kt +++ b/core/src/main/kotlin/matterlink/config/CommandConfig.kt @@ -14,21 +14,21 @@ object CommandConfig { private val default = arrayOf( CustomCommand( alias = "tps", - type = CommandType.PASSTHROUGH, + type = CommandType.EXECUTE, execute = "forge tps", help = "Print server tps", allowArgs = false ), CustomCommand( alias = "list", - type = CommandType.PASSTHROUGH, + type = CommandType.EXECUTE, execute = "list", help = "List online players", allowArgs = false ), CustomCommand( alias = "seed", - type = CommandType.PASSTHROUGH, + type = CommandType.EXECUTE, execute = "seed", help = "Print server world seed", allowArgs = false diff --git a/core/src/main/kotlin/matterlink/handlers/TickHandler.kt b/core/src/main/kotlin/matterlink/handlers/TickHandler.kt index 83642b9..554d34f 100644 --- a/core/src/main/kotlin/matterlink/handlers/TickHandler.kt +++ b/core/src/main/kotlin/matterlink/handlers/TickHandler.kt @@ -10,12 +10,13 @@ import matterlink.update.UpdateChecker * @version 1.0 */ object TickHandler { - private var totalTicks = 0 + var tickCounter = 0 + private set private var accumulator = 0 private const val updateInterval = 12 * 60 * 60 * 20 fun handleTick() { - totalTicks++ - if (totalTicks % 100 == 0) { + tickCounter++ + if (tickCounter % 100 == 0) { MessageHandler.checkConnection() }