Remove 'world' object from global script scope

This commit is contained in:
Gary Tierney
2017-05-28 23:07:05 +01:00
parent 05e20d9d51
commit 48e1726bc0
7 changed files with 25 additions and 31 deletions
@@ -55,7 +55,7 @@ public class KotlinPluginEnvironment implements PluginEnvironment {
throw new RuntimeException(e);
}
pluginScripts.forEach(KotlinPluginScript::doStart);
pluginScripts.forEach(script -> script.doStart(world));
}
@Override
@@ -12,28 +12,28 @@ import kotlin.script.templates.ScriptTemplateDefinition
@ScriptTemplateDefinition(
scriptFilePattern = ".*\\.plugin\\.kts"
)
abstract class KotlinPluginScript(val world: World, val context: PluginContext) {
var startListener: () -> Unit = {};
var stopListener: () -> Unit = {};
abstract class KotlinPluginScript(private var world: World, val context: PluginContext) {
var startListener: (World) -> Unit = { _ -> };
var stopListener: (World) -> Unit = { _ -> };
protected fun <T : Message> on(type: () -> KClass<T>): KotlinMessageHandler<T> {
return KotlinMessageHandler(world, context, type.invoke())
}
protected fun start(callback: () -> Unit) {
protected fun start(callback: (World) -> Unit) {
this.startListener = callback
}
protected fun stop(callback: () -> Unit) {
protected fun stop(callback: (World) -> Unit) {
this.stopListener = callback
}
fun doStart() {
this.startListener.invoke()
fun doStart(world: World) {
this.startListener.invoke(world)
}
fun doStop() {
this.stopListener.invoke()
fun doStop(world: World) {
this.stopListener.invoke(world)
}
}
+1 -1
View File
@@ -23,7 +23,7 @@ on { ObjectActionMessage::class }
on { NpcActionMessage::class }
.where { option == 2 }
.then {
val npc = world.npcRepository[index]
val npc = it.world.npcRepository[index]
if (npc.id in BANKER_NPCS) {
BankAction.start(this, it, npc.position)
@@ -3,21 +3,21 @@ import org.apollo.game.message.impl.SendFriendMessage
import org.apollo.game.model.entity.setting.PrivacyState
on { AddFriendMessage::class }
.then { player ->
player.addFriend(username)
.then {
it.addFriend(username)
val playerUsername = player.username
val friend = world.getPlayer(username)
val playerUsername = it.username
val friend = it.world.getPlayer(username)
if (friend == null) {
player.send(SendFriendMessage(username, 0))
it.send(SendFriendMessage(username, 0))
} else if (friend.friendsWith(playerUsername) || friend.friendPrivacy == PrivacyState.ON) {
if (player.friendPrivacy != PrivacyState.OFF) {
friend.send(SendFriendMessage(playerUsername, player.worldId))
if (it.friendPrivacy != PrivacyState.OFF) {
friend.send(SendFriendMessage(playerUsername, it.worldId))
}
if (friend.friendPrivacy != PrivacyState.OFF) {
player.send(SendFriendMessage(username, friend.worldId))
it.send(SendFriendMessage(username, friend.worldId))
}
}
}
}
@@ -2,7 +2,7 @@ import org.apollo.game.message.impl.AddIgnoreMessage
import org.apollo.game.message.impl.RemoveIgnoreMessage
on { AddIgnoreMessage::class }
.then { player -> player.addIgnore(username) }
.then { it.addIgnore(username) }
on { RemoveIgnoreMessage::class }
.then { player -> player.removeIgnore(username) }
.then { it.removeIgnore(username) }
@@ -1,7 +1,7 @@
import org.apollo.cache.def.NpcDefinition
import org.apollo.game.model.entity.Npc
start {
start { world ->
Spawns.list.forEach {
val definition = if (it.id != null) NpcDefinition.lookup(it.id) else lookup_npc(it.name)
if (definition == null) {
@@ -22,6 +22,3 @@ start {
world.register(npc)
}
}
stop {
}
+2 -5
View File
@@ -12,17 +12,14 @@ import org.apollo.game.plugin.kotlin.*
import org.apollo.net.message.Message
import kotlin.reflect.KClass
val world: World = null!!
var context: PluginContext = null!!
fun <T : Message> on(type: () -> KClass<T>): KotlinMessageHandler<T> {
null!!
}
fun start(callback: () -> Unit) {
fun start(callback: (World) -> Unit) {
}
fun stop(callback: () -> Unit) {
fun stop(callback: (World) -> Unit) {
}