From e6052418934b431c02692bb6de76684bb49398b8 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 25 Jun 2017 02:38:56 +0100 Subject: [PATCH] Add on_button method to plugin scripts --- .../game/plugin/kotlin/KotlinPluginScript.kt | 113 ++++++++++-------- game/src/main/kotlin/stub.kt | 24 ++-- 2 files changed, 72 insertions(+), 65 deletions(-) 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 f3b9cf3b..357b17ef 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 @@ -3,6 +3,7 @@ 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.message.impl.ButtonMessage import org.apollo.game.model.World import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.setting.PrivilegeLevel @@ -14,89 +15,103 @@ import kotlin.reflect.KClass import kotlin.script.templates.ScriptTemplateDefinition @ScriptTemplateDefinition( - scriptFilePattern = ".*\\.plugin\\.kts" + scriptFilePattern = ".*\\.plugin\\.kts" ) abstract class KotlinPluginScript(private var world: World, val context: PluginContext) { - var startListener: (World) -> Unit = { _ -> }; - var stopListener: (World) -> Unit = { _ -> }; + var startListener: (World) -> Unit = { _ -> }; + var stopListener: (World) -> Unit = { _ -> }; - fun on(type: () -> KClass) = KotlinMessageHandler(world, context, type.invoke()) + /** + * Create a new [MessageHandler]. + */ + fun on(type: () -> KClass) = KotlinMessageHandler(world, context, type.invoke()) - fun on_player_event(type: () -> KClass) = KotlinPlayerEventHandler(world, type.invoke()) + /** + * Create a new [EventListener] for a type of [PlayerEvent]. + */ + fun on_player_event(type: () -> KClass) = KotlinPlayerEventHandler(world, type.invoke()) - fun on_command(command: String, privileges: PrivilegeLevel) = KotlinCommandHandler(world, command, privileges) + /** + * Create a new [CommandHandler] for the given _command_ name, which only players with a [PrivelegeLevel] + * of _privileges_ and above can use. + */ + fun on_command(command: String, privileges: PrivilegeLevel) = KotlinCommandHandler(world, command, privileges) + /** + * Create a new [ButtonMessage] [MessageHandler] for the given _button_ id. + */ + fun on_button(button: Int) = on { ButtonMessage::class }.where { widgetId == button } - fun start(callback: (World) -> Unit) { - this.startListener = callback - } + fun start(callback: (World) -> Unit) { + this.startListener = callback + } - fun stop(callback: (World) -> Unit) { - this.stopListener = callback - } + fun stop(callback: (World) -> Unit) { + this.stopListener = callback + } - fun doStart(world: World) { - this.startListener.invoke(world) - } + fun doStart(world: World) { + this.startListener.invoke(world) + } - fun doStop(world: World) { - this.stopListener.invoke(world) - } + fun doStop(world: World) { + this.stopListener.invoke(world) + } } interface KotlinPlayerHandlerProxyTrait { - var callback: S.(Player) -> Unit - var predicate: S.() -> Boolean + var callback: S.(Player) -> Unit + var predicate: S.() -> Boolean - fun where(predicate: S.() -> Boolean): KotlinPlayerHandlerProxyTrait { - this.predicate = predicate - return this - } + fun where(predicate: S.() -> Boolean): KotlinPlayerHandlerProxyTrait { + this.predicate = predicate + return this + } - fun then(callback: S.(Player) -> Unit) { - this.callback = callback - this.register() - } + fun then(callback: S.(Player) -> Unit) { + this.callback = callback + this.register() + } - fun register() + fun register() - fun handleProxy(player: Player, subject: S) { - if (subject.predicate()) { - subject.callback(player) - } - } + fun handleProxy(player: Player, subject: S) { + if (subject.predicate()) { + subject.callback(player) + } + } } class KotlinPlayerEventHandler(val world: World, val type: KClass) : - KotlinPlayerHandlerProxyTrait, EventListener { + KotlinPlayerHandlerProxyTrait, EventListener { - override var callback: T.(Player) -> Unit = {} - override var predicate: T.() -> Boolean = { true } + override var callback: T.(Player) -> Unit = {} + override var predicate: T.() -> Boolean = { true } - override fun handle(event: T) = handleProxy(event.player, event) - override fun register() = world.listenFor(type.java, this) + override fun handle(event: T) = handleProxy(event.player, event) + override fun register() = world.listenFor(type.java, this) } class KotlinMessageHandler(val world: World, val context: PluginContext, val type: KClass) : - KotlinPlayerHandlerProxyTrait, MessageHandler(world) { + KotlinPlayerHandlerProxyTrait, MessageHandler(world) { - override var callback: T.(Player) -> Unit = {} - override var predicate: T.() -> Boolean = { true } + override var callback: T.(Player) -> Unit = {} + override var predicate: T.() -> Boolean = { true } - override fun handle(player: Player, message: T) = handleProxy(player, message) - override fun register() = context.addMessageHandler(type.java, this) + override fun handle(player: Player, message: T) = handleProxy(player, message) + override fun register() = context.addMessageHandler(type.java, this) } class KotlinCommandHandler(val world: World, val command: String, privileges: PrivilegeLevel) : - KotlinPlayerHandlerProxyTrait, CommandListener(privileges) { + KotlinPlayerHandlerProxyTrait, CommandListener(privileges) { - override var callback: Command.(Player) -> Unit = {} - override var predicate: Command.() -> Boolean = { true } + override var callback: Command.(Player) -> Unit = {} + override var predicate: Command.() -> Boolean = { true } - override fun execute(player: Player, command: Command) = handleProxy(player, command) - override fun register() = world.commandDispatcher.register(command, this) + override fun execute(player: Player, command: Command) = handleProxy(player, command) + override fun register() = world.commandDispatcher.register(command, this) } diff --git a/game/src/main/kotlin/stub.kt b/game/src/main/kotlin/stub.kt index 70dcb9db..48dd599a 100644 --- a/game/src/main/kotlin/stub.kt +++ b/game/src/main/kotlin/stub.kt @@ -6,7 +6,9 @@ * required to resolve references within plugin code. */ +import org.apollo.game.command.Command import org.apollo.game.message.handler.MessageHandlerChainSet +import org.apollo.game.message.impl.ButtonMessage import org.apollo.game.model.World import org.apollo.game.model.area.RegionRepository import org.apollo.game.model.entity.* @@ -16,21 +18,11 @@ import org.apollo.game.plugin.kotlin.* import org.apollo.net.message.Message import kotlin.reflect.KClass -fun on(type: () -> KClass): KotlinMessageHandler { - null!! -} -fun on_player_event(type: () -> KClass): KotlinPlayerEventHandler { - null!! -} +fun on(type: () -> KClass): KotlinPlayerHandlerProxyTrait = null!! +fun on_player_event(type: () -> KClass): KotlinPlayerHandlerProxyTrait = null!! +fun on_command(command: String, privileges: PrivilegeLevel): KotlinPlayerHandlerProxyTrait = null!! +fun on_button(button: Int): KotlinPlayerHandlerProxyTrait = null!! -fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler { - null!! -} +fun start(callback: (World) -> Unit) = {} +fun stop(callback: (World) -> Unit) = {} -fun start(callback: (World) -> Unit) { - -} - -fun stop(callback: (World) -> Unit) { - -}