add timeous to commands

This commit is contained in:
Nikky Ai 2018-02-25 00:51:56 +01:00
parent 53e84afc6d
commit c5fd53a00c
4 changed files with 30 additions and 15 deletions

View File

@ -17,11 +17,12 @@ repositories {
} }
dependencies { dependencies {
compile 'org.apache.httpcomponents:httpclient:4.3.3' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'
compile 'commons-logging:commons-logging:1.1.3' compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.3.3'
// compile group: "org.apache.logging.log4j", name: "log4j-api", version: '2.8.1' compile group: 'commons-logging', name: 'commons-logging', version: '1.1.3'
compile 'com.google.code.gson:gson:2.8.0' compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
compile group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib-jdk8', version: project.kotlin_version
} }
compileKotlin { compileKotlin {

View File

@ -2,6 +2,7 @@ package matterlink.bridge.command
import matterlink.bridge.ApiMessage import matterlink.bridge.ApiMessage
import matterlink.bridge.MessageHandler import matterlink.bridge.MessageHandler
import matterlink.handlers.TickHandler
import matterlink.instance import matterlink.instance
import matterlink.lazyFormat import matterlink.lazyFormat
@ -12,17 +13,29 @@ data class CustomCommand(
val response: String = "", val response: String = "",
override val permLevel: Int = 0, override val permLevel: Int = 0,
override val help: String = "", override val help: String = "",
val allowArgs: Boolean = true val allowArgs: Boolean = true,
val timeout: Int = 20
) : IBridgeCommand { ) : IBridgeCommand {
var lastUsed: Int = 0
override fun execute(user: String, userId: String, server: String, args: String): Boolean { override fun execute(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)
{
instance.debug("dropped command $alias")
return true //eat command silently
}
if (!canExecute(userId, server)) { if (!canExecute(userId, server)) {
MessageHandler.transmit(ApiMessage(text = "$user is not permitted to perform command: $alias")) MessageHandler.transmit(ApiMessage(text = "$user is not permitted to perform command: $alias"))
return false return false
} }
lastUsed = TickHandler.tickCounter
return when (type) { return when (type) {
CommandType.PASSTHROUGH -> { CommandType.EXECUTE -> {
//uses a new commandsender for each user //uses a new commandsender for each user
// TODO: cache CommandSenders // TODO: cache CommandSenders
val commandSender = instance.commandSenderFor(user, userId, server) val commandSender = instance.commandSenderFor(user, userId, server)
@ -40,7 +53,7 @@ data class CustomCommand(
*/ */
override fun validate(): Boolean { override fun validate(): Boolean {
val typeCheck = when (type) { val typeCheck = when (type) {
CommandType.PASSTHROUGH -> execute.isNotBlank() CommandType.EXECUTE -> execute.isNotBlank()
CommandType.RESPONSE -> response.isNotBlank() CommandType.RESPONSE -> response.isNotBlank()
} }
if (!typeCheck) return false if (!typeCheck) return false
@ -58,5 +71,5 @@ data class CustomCommand(
} }
enum class CommandType { enum class CommandType {
PASSTHROUGH, RESPONSE EXECUTE, RESPONSE
} }

View File

@ -14,21 +14,21 @@ object CommandConfig {
private val default = arrayOf( private val default = arrayOf(
CustomCommand( CustomCommand(
alias = "tps", alias = "tps",
type = CommandType.PASSTHROUGH, type = CommandType.EXECUTE,
execute = "forge tps", execute = "forge tps",
help = "Print server tps", help = "Print server tps",
allowArgs = false allowArgs = false
), ),
CustomCommand( CustomCommand(
alias = "list", alias = "list",
type = CommandType.PASSTHROUGH, type = CommandType.EXECUTE,
execute = "list", execute = "list",
help = "List online players", help = "List online players",
allowArgs = false allowArgs = false
), ),
CustomCommand( CustomCommand(
alias = "seed", alias = "seed",
type = CommandType.PASSTHROUGH, type = CommandType.EXECUTE,
execute = "seed", execute = "seed",
help = "Print server world seed", help = "Print server world seed",
allowArgs = false allowArgs = false

View File

@ -10,12 +10,13 @@ import matterlink.update.UpdateChecker
* @version 1.0 * @version 1.0
*/ */
object TickHandler { object TickHandler {
private var totalTicks = 0 var tickCounter = 0
private set
private var accumulator = 0 private var accumulator = 0
private const val updateInterval = 12 * 60 * 60 * 20 private const val updateInterval = 12 * 60 * 60 * 20
fun handleTick() { fun handleTick() {
totalTicks++ tickCounter++
if (totalTicks % 100 == 0) { if (tickCounter % 100 == 0) {
MessageHandler.checkConnection() MessageHandler.checkConnection()
} }