From 30fc810d61341ca27cd20351317165491c619c04 Mon Sep 17 00:00:00 2001 From: Cube Date: Sat, 3 Jun 2017 02:22:37 +0300 Subject: [PATCH] Add base for Kotlin commands --- .../game/plugin/kotlin/KotlinPluginScript.kt | 28 +++++++++++++++++-- game/src/main/kotlin/stub.kt | 9 ++++-- game/src/plugins/cmd/meta.toml | 7 +++++ game/src/plugins/cmd/src/bank-cmd.plugin.kts | 5 ++++ game/src/plugins/stub/stub.kt | 4 +++ 5 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 game/src/plugins/cmd/meta.toml create mode 100644 game/src/plugins/cmd/src/bank-cmd.plugin.kts diff --git a/game/src/main/kotlin/org/apollo/game/plugin/kotlin/KotlinPluginScript.kt b/game/src/main/kotlin/org/apollo/game/plugin/kotlin/KotlinPluginScript.kt index 1a9541a3..710b3ac1 100644 --- a/game/src/main/kotlin/org/apollo/game/plugin/kotlin/KotlinPluginScript.kt +++ b/game/src/main/kotlin/org/apollo/game/plugin/kotlin/KotlinPluginScript.kt @@ -1,10 +1,12 @@ package org.apollo.game.plugin.kotlin +import org.apollo.game.command.Command +import org.apollo.game.command.CommandListener import org.apollo.game.message.handler.MessageHandler import org.apollo.game.model.World import org.apollo.game.model.entity.Player +import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.plugin.PluginContext -import org.apollo.game.scheduling.ScheduledTask import org.apollo.net.message.Message import kotlin.reflect.KClass import kotlin.script.templates.ScriptTemplateDefinition @@ -20,6 +22,10 @@ abstract class KotlinPluginScript(private var world: World, val context: PluginC return KotlinMessageHandler(world, context, type.invoke()) } + protected fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler { + return KotlinCommandHandler(world, command, privileges) + } + protected fun start(callback: (World) -> Unit) { this.startListener = callback } @@ -37,7 +43,6 @@ abstract class KotlinPluginScript(private var world: World, val context: PluginC } } - class KotlinMessageHandler(val world: World, val context: PluginContext, val type: KClass) : MessageHandler(world) { override fun handle(player: Player, message: T) { @@ -61,3 +66,22 @@ class KotlinMessageHandler(val world: World, val context: PluginCon } } + +class KotlinCommandListener(val level: PrivilegeLevel, val function: (Player, Command) -> Unit) : CommandListener(level) { + + override fun execute(player: Player, command: Command) { + function.invoke(player, command) + } + +} + +class KotlinCommandHandler(val world : World, val command: String, val privileges: PrivilegeLevel) { + + var function: (Player, Command) -> Unit = { _, _ -> } + + fun then(function: (Player, Command) -> Unit) { + this.function = function + world.commandDispatcher.register(command, KotlinCommandListener(privileges, function)) + } + +} diff --git a/game/src/main/kotlin/stub.kt b/game/src/main/kotlin/stub.kt index 8c583704..a7e212c7 100644 --- a/game/src/main/kotlin/stub.kt +++ b/game/src/main/kotlin/stub.kt @@ -7,8 +7,9 @@ */ import org.apollo.game.model.World -import org.apollo.game.plugin.PluginContext -import org.apollo.game.plugin.kotlin.* +import org.apollo.game.model.entity.setting.PrivilegeLevel +import org.apollo.game.plugin.kotlin.KotlinCommandHandler +import org.apollo.game.plugin.kotlin.KotlinMessageHandler import org.apollo.net.message.Message import kotlin.reflect.KClass @@ -16,6 +17,10 @@ fun on(type: () -> KClass): KotlinMessageHandler { null!! } +fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler { + null!! +} + fun start(callback: (World) -> Unit) { } diff --git a/game/src/plugins/cmd/meta.toml b/game/src/plugins/cmd/meta.toml new file mode 100644 index 00000000..141c3a7b --- /dev/null +++ b/game/src/plugins/cmd/meta.toml @@ -0,0 +1,7 @@ +name = "Chat commands" +package = "org.apollo.game.plugin.cmd" +authors = [ "Graham", "cubeee" ] + +[config] +srcDir = "src/" +testDir = "test/" \ No newline at end of file diff --git a/game/src/plugins/cmd/src/bank-cmd.plugin.kts b/game/src/plugins/cmd/src/bank-cmd.plugin.kts new file mode 100644 index 00000000..84de8f0d --- /dev/null +++ b/game/src/plugins/cmd/src/bank-cmd.plugin.kts @@ -0,0 +1,5 @@ +import org.apollo.game.model.entity.setting.PrivilegeLevel + +// Opens the player's bank if they are an administrator. +on_command("bank", PrivilegeLevel.ADMINISTRATOR) + .then { player, _ -> player.openBank() } \ No newline at end of file diff --git a/game/src/plugins/stub/stub.kt b/game/src/plugins/stub/stub.kt index 8c583704..f82da254 100644 --- a/game/src/plugins/stub/stub.kt +++ b/game/src/plugins/stub/stub.kt @@ -16,6 +16,10 @@ fun on(type: () -> KClass): KotlinMessageHandler { null!! } +fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler { + null!! +} + fun start(callback: (World) -> Unit) { }