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 {
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 {

View File

@ -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
}

View File

@ -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

View File

@ -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()
}