Add on_button method to plugin scripts

This commit is contained in:
Gary Tierney
2017-06-25 02:38:56 +01:00
parent 9a8ed0d7a9
commit e605241893
2 changed files with 72 additions and 65 deletions
@@ -3,6 +3,7 @@ package org.apollo.game.plugin.kotlin
import org.apollo.game.command.Command import org.apollo.game.command.Command
import org.apollo.game.command.CommandListener import org.apollo.game.command.CommandListener
import org.apollo.game.message.handler.MessageHandler 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.World
import org.apollo.game.model.entity.Player import org.apollo.game.model.entity.Player
import org.apollo.game.model.entity.setting.PrivilegeLevel import org.apollo.game.model.entity.setting.PrivilegeLevel
@@ -20,12 +21,26 @@ abstract class KotlinPluginScript(private var world: World, val context: PluginC
var startListener: (World) -> Unit = { _ -> }; var startListener: (World) -> Unit = { _ -> };
var stopListener: (World) -> Unit = { _ -> }; var stopListener: (World) -> Unit = { _ -> };
/**
* Create a new [MessageHandler].
*/
fun <T : Message> on(type: () -> KClass<T>) = KotlinMessageHandler<T>(world, context, type.invoke()) fun <T : Message> on(type: () -> KClass<T>) = KotlinMessageHandler<T>(world, context, type.invoke())
/**
* Create a new [EventListener] for a type of [PlayerEvent].
*/
fun <T : PlayerEvent> on_player_event(type: () -> KClass<T>) = KotlinPlayerEventHandler(world, type.invoke()) fun <T : PlayerEvent> on_player_event(type: () -> KClass<T>) = KotlinPlayerEventHandler(world, type.invoke())
/**
* 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) 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) { fun start(callback: (World) -> Unit) {
this.startListener = callback this.startListener = callback
+8 -16
View File
@@ -6,7 +6,9 @@
* required to resolve references within plugin code. * 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.handler.MessageHandlerChainSet
import org.apollo.game.message.impl.ButtonMessage
import org.apollo.game.model.World import org.apollo.game.model.World
import org.apollo.game.model.area.RegionRepository import org.apollo.game.model.area.RegionRepository
import org.apollo.game.model.entity.* import org.apollo.game.model.entity.*
@@ -16,21 +18,11 @@ import org.apollo.game.plugin.kotlin.*
import org.apollo.net.message.Message import org.apollo.net.message.Message
import kotlin.reflect.KClass import kotlin.reflect.KClass
fun <T : Message> on(type: () -> KClass<T>): KotlinMessageHandler<T> { fun <T : Message> on(type: () -> KClass<T>): KotlinPlayerHandlerProxyTrait<T> = null!!
null!! fun <T : PlayerEvent> on_player_event(type: () -> KClass<T>): KotlinPlayerHandlerProxyTrait<T> = null!!
} fun on_command(command: String, privileges: PrivilegeLevel): KotlinPlayerHandlerProxyTrait<Command> = null!!
fun <T : PlayerEvent> on_player_event(type: () -> KClass<T>): KotlinPlayerEventHandler<T> { fun on_button(button: Int): KotlinPlayerHandlerProxyTrait<ButtonMessage> = null!!
null!!
}
fun on_command(command: String, privileges: PrivilegeLevel): KotlinCommandHandler { fun start(callback: (World) -> Unit) = {}
null!! fun stop(callback: (World) -> Unit) = {}
}
fun start(callback: (World) -> Unit) {
}
fun stop(callback: (World) -> Unit) {
}