Add base for Kotlin commands

This commit is contained in:
Cube
2017-06-03 02:22:37 +03:00
parent ce3150082d
commit 30fc810d61
5 changed files with 49 additions and 4 deletions
@@ -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<T : Message>(val world: World, val context: PluginContext, val type: KClass<T>) : MessageHandler<T>(world) {
override fun handle(player: Player, message: T) {
@@ -61,3 +66,22 @@ class KotlinMessageHandler<T : Message>(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))
}
}
+7 -2
View File
@@ -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 <T : Message> on(type: () -> KClass<T>): KotlinMessageHandler<T> {
null!!
}
fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler {
null!!
}
fun start(callback: (World) -> Unit) {
}
+7
View File
@@ -0,0 +1,7 @@
name = "Chat commands"
package = "org.apollo.game.plugin.cmd"
authors = [ "Graham", "cubeee" ]
[config]
srcDir = "src/"
testDir = "test/"
@@ -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() }
+4
View File
@@ -16,6 +16,10 @@ fun <T : Message> on(type: () -> KClass<T>): KotlinMessageHandler<T> {
null!!
}
fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler {
null!!
}
fun start(callback: (World) -> Unit) {
}