From 48e1726bc09fc030264562d05cea4115308248d7 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 28 May 2017 23:07:05 +0100 Subject: [PATCH] Remove 'world' object from global script scope --- .../game/plugin/KotlinPluginEnvironment.java | 2 +- .../game/plugin/kotlin/KotlinPluginScript.kt | 18 +++++++++--------- game/src/plugins/bank/bank.plugin.kts | 2 +- .../chat/private-messaging/friends.plugin.kts | 18 +++++++++--------- .../chat/private-messaging/ignores.plugin.kts | 4 ++-- game/src/plugins/entity/spawn/spawn.plugin.kts | 5 +---- game/src/plugins/stub.kt | 7 ++----- 7 files changed, 25 insertions(+), 31 deletions(-) diff --git a/game/src/main/java/org/apollo/game/plugin/KotlinPluginEnvironment.java b/game/src/main/java/org/apollo/game/plugin/KotlinPluginEnvironment.java index d433db17..b2e74222 100644 --- a/game/src/main/java/org/apollo/game/plugin/KotlinPluginEnvironment.java +++ b/game/src/main/java/org/apollo/game/plugin/KotlinPluginEnvironment.java @@ -55,7 +55,7 @@ public class KotlinPluginEnvironment implements PluginEnvironment { throw new RuntimeException(e); } - pluginScripts.forEach(KotlinPluginScript::doStart); + pluginScripts.forEach(script -> script.doStart(world)); } @Override 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 c26febcc..1a9541a3 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 @@ -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 on(type: () -> KClass): KotlinMessageHandler { 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) } } diff --git a/game/src/plugins/bank/bank.plugin.kts b/game/src/plugins/bank/bank.plugin.kts index 96082328..10d424e3 100644 --- a/game/src/plugins/bank/bank.plugin.kts +++ b/game/src/plugins/bank/bank.plugin.kts @@ -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) diff --git a/game/src/plugins/chat/private-messaging/friends.plugin.kts b/game/src/plugins/chat/private-messaging/friends.plugin.kts index e7fef1bb..ea7ced88 100644 --- a/game/src/plugins/chat/private-messaging/friends.plugin.kts +++ b/game/src/plugins/chat/private-messaging/friends.plugin.kts @@ -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)) } } - } \ No newline at end of file + } diff --git a/game/src/plugins/chat/private-messaging/ignores.plugin.kts b/game/src/plugins/chat/private-messaging/ignores.plugin.kts index a42816da..47b0f29f 100644 --- a/game/src/plugins/chat/private-messaging/ignores.plugin.kts +++ b/game/src/plugins/chat/private-messaging/ignores.plugin.kts @@ -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) } \ No newline at end of file + .then { it.removeIgnore(username) } \ No newline at end of file diff --git a/game/src/plugins/entity/spawn/spawn.plugin.kts b/game/src/plugins/entity/spawn/spawn.plugin.kts index 878ab4e3..cb65aace 100644 --- a/game/src/plugins/entity/spawn/spawn.plugin.kts +++ b/game/src/plugins/entity/spawn/spawn.plugin.kts @@ -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 { -} diff --git a/game/src/plugins/stub.kt b/game/src/plugins/stub.kt index 1f0fe80d..8c583704 100644 --- a/game/src/plugins/stub.kt +++ b/game/src/plugins/stub.kt @@ -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 on(type: () -> KClass): KotlinMessageHandler { null!! } -fun start(callback: () -> Unit) { +fun start(callback: (World) -> Unit) { } -fun stop(callback: () -> Unit) { +fun stop(callback: (World) -> Unit) { }