From 8fe09880b7d6cdd45ee4874bdf47d300d4712f69 Mon Sep 17 00:00:00 2001 From: Major Date: Sun, 8 Apr 2018 16:19:02 +0100 Subject: [PATCH] Remove util:lookup plugin Behaviour moved into the api plugin. --- game/plugin/api/src/definitions.kt | 34 +++++++++++++++++ .../test/NamedLookupTests.kt} | 6 +-- game/plugin/entity/spawn/build.gradle | 2 +- game/plugin/entity/spawn/src/spawn.plugin.kts | 18 ++++----- game/plugin/shops/build.gradle | 8 ++-- game/plugin/shops/src/dsl.kt | 7 ++-- game/plugin/skills/fishing/build.gradle | 11 +++--- game/plugin/skills/prayer/build.gradle | 2 +- game/plugin/skills/runecrafting/build.gradle | 10 +++-- game/plugin/util/lookup/build.gradle | 3 -- game/plugin/util/lookup/src/lookup.kt | 37 ------------------- 11 files changed, 66 insertions(+), 72 deletions(-) rename game/plugin/{util/lookup/test/LookupTests.kt => api/test/NamedLookupTests.kt} (67%) delete mode 100644 game/plugin/util/lookup/build.gradle delete mode 100644 game/plugin/util/lookup/src/lookup.kt diff --git a/game/plugin/api/src/definitions.kt b/game/plugin/api/src/definitions.kt index 69966d43..b2f826e5 100644 --- a/game/plugin/api/src/definitions.kt +++ b/game/plugin/api/src/definitions.kt @@ -9,11 +9,45 @@ object Definitions { return ItemDefinition.lookup(id) } + fun item(name: String): ItemDefinition? { + return findEntity(ItemDefinition::getDefinitions, ItemDefinition::getName, name) + } + fun obj(id: Int): ObjectDefinition? { return ObjectDefinition.lookup(id) } + fun obj(name: String): ObjectDefinition? { + return findEntity(ObjectDefinition::getDefinitions, ObjectDefinition::getName, name) + } + fun npc(id: Int): NpcDefinition? { return NpcDefinition.lookup(id) } + + fun npc(name: String): NpcDefinition? { + return findEntity(NpcDefinition::getDefinitions, NpcDefinition::getName, name) + } + + /** + * The [Regex] used to match 'names' that have an id attached to the end. + */ + private val ID_REGEX = Regex(".+_[0-9]+$") + + private fun findEntity( + definitionsProvider: () -> Array, + nameSupplier: T.() -> String, + name: String + ): T? { + val definitions = definitionsProvider.invoke() + + if (ID_REGEX matches name) { + val id = name.substring(name.lastIndexOf('_') + 1, name.length).toInt() + return definitions.getOrNull(id) + } + + val normalizedName = name.replace('_', ' ') + return definitions.firstOrNull { it.nameSupplier().equals(normalizedName, ignoreCase = true) } + } + } \ No newline at end of file diff --git a/game/plugin/util/lookup/test/LookupTests.kt b/game/plugin/api/test/NamedLookupTests.kt similarity index 67% rename from game/plugin/util/lookup/test/LookupTests.kt rename to game/plugin/api/test/NamedLookupTests.kt index 680c9c15..4f361db6 100644 --- a/game/plugin/util/lookup/test/LookupTests.kt +++ b/game/plugin/api/test/NamedLookupTests.kt @@ -1,10 +1,10 @@ import org.apollo.cache.def.ItemDefinition +import org.apollo.game.plugin.api.Definitions import org.apollo.game.plugin.testing.KotlinPluginTest -import org.apollo.game.plugin.util.lookup.lookup_item import org.assertj.core.api.Assertions.assertThat import org.junit.Test -class LookupTests : KotlinPluginTest() { +class NamedLookupTests : KotlinPluginTest() { @Test fun itemLookup() { val testItem = ItemDefinition(0) @@ -12,6 +12,6 @@ class LookupTests : KotlinPluginTest() { ItemDefinition.init(arrayOf(testItem)) - assertThat(lookup_item("sword")).isEqualTo(testItem) + assertThat(Definitions.item("sword")).isEqualTo(testItem) } } \ No newline at end of file diff --git a/game/plugin/entity/spawn/build.gradle b/game/plugin/entity/spawn/build.gradle index adc0d466..e0bb2828 100644 --- a/game/plugin/entity/spawn/build.gradle +++ b/game/plugin/entity/spawn/build.gradle @@ -4,6 +4,6 @@ plugin { "Gary Tierney", ] dependencies = [ - "util:lookup", + "api", ] } \ No newline at end of file diff --git a/game/plugin/entity/spawn/src/spawn.plugin.kts b/game/plugin/entity/spawn/src/spawn.plugin.kts index 0f172491..87e3bb51 100644 --- a/game/plugin/entity/spawn/src/spawn.plugin.kts +++ b/game/plugin/entity/spawn/src/spawn.plugin.kts @@ -1,18 +1,18 @@ -import org.apollo.cache.def.NpcDefinition + import org.apollo.game.model.entity.Npc +import org.apollo.game.plugin.api.Definitions import org.apollo.game.plugin.entity.spawn.Spawns -import org.apollo.game.plugin.util.lookup.lookup_npc start { world -> - Spawns.list.forEach { - val definition = it.id?.let { NpcDefinition.lookup(it) } ?: lookup_npc(it.name) ?: - throw IllegalArgumentException("Invalid NPC name or ID ${it.name}, ${it.id}") + Spawns.list.forEach { spawn -> + val definition = spawn.id?.let(Definitions::npc) ?: Definitions.npc(spawn.name) + ?: throw IllegalArgumentException("Invalid NPC name or ID ${spawn.name}, ${spawn.id}") - val npc = Npc(world, definition.id, it.position) - npc.turnTo(it.position.step(1, it.facing)) + val npc = Npc(world, definition.id, spawn.position) + npc.turnTo(spawn.position.step(1, spawn.facing)) - it.spawnAnimation?.let(npc::playAnimation) - it.spawnGraphic?.let(npc::playGraphic) + spawn.spawnAnimation?.let(npc::playAnimation) + spawn.spawnGraphic?.let(npc::playGraphic) world.register(npc) } diff --git a/game/plugin/shops/build.gradle b/game/plugin/shops/build.gradle index 2532af02..369bf235 100644 --- a/game/plugin/shops/build.gradle +++ b/game/plugin/shops/build.gradle @@ -1,11 +1,11 @@ plugin { name = "shops" authors = [ - "Stuart", - "Major", - "tlf30" + "Stuart", + "Major", + "tlf30" ] dependencies = [ - "util:lookup", + "api", ] } diff --git a/game/plugin/shops/src/dsl.kt b/game/plugin/shops/src/dsl.kt index 639ccb16..b5f5da39 100644 --- a/game/plugin/shops/src/dsl.kt +++ b/game/plugin/shops/src/dsl.kt @@ -1,9 +1,8 @@ package org.apollo.game.plugin.shops import org.apollo.cache.def.NpcDefinition +import org.apollo.game.plugin.api.Definitions import org.apollo.game.plugin.shops.CategoryWrapper.Affix -import org.apollo.game.plugin.util.lookup.lookup_item -import org.apollo.game.plugin.util.lookup.lookup_npc /** * Creates a [Shop]. @@ -109,7 +108,7 @@ class ShopBuilder(val name: String) { * Converts this builder into a [Shop]. */ internal fun build(): Shop { - val items = sold.associateBy({ (first) -> lookup_item(first)!!.id }, Pair::second) + val items = sold.associateBy({ (first) -> Definitions.item(first)!!.id }, Pair::second) val npc = NpcDefinition.lookup(operators().first()) return Shop(name, action.action(npc), items, trades.currency, buys.policy) @@ -174,7 +173,7 @@ class OperatorBuilder internal constructor() { * Adds a shop operator, using the specified [name] to resolve the npc id. */ infix fun by(name: String): OperatorBuilder { - operators.add(lookup_npc(name)!!.id) + operators.add(Definitions.npc(name)!!.id) return this } diff --git a/game/plugin/skills/fishing/build.gradle b/game/plugin/skills/fishing/build.gradle index a7c3c5df..435281cb 100644 --- a/game/plugin/skills/fishing/build.gradle +++ b/game/plugin/skills/fishing/build.gradle @@ -1,13 +1,12 @@ plugin { name = "fishing_skill" authors = [ - "Linux", - "Major", - "tlf30" + "Linux", + "Major", + "tlf30" ] dependencies = [ - "api", - "entity:spawn", - "util:lookup" + "api", + "entity:spawn", ] } diff --git a/game/plugin/skills/prayer/build.gradle b/game/plugin/skills/prayer/build.gradle index fa5c27ce..5ea5c0b2 100644 --- a/game/plugin/skills/prayer/build.gradle +++ b/game/plugin/skills/prayer/build.gradle @@ -6,6 +6,6 @@ plugin { "tlf30" ] dependencies = [ - "util:lookup", "api" + "api" ] } diff --git a/game/plugin/skills/runecrafting/build.gradle b/game/plugin/skills/runecrafting/build.gradle index 123631a8..74ddffb6 100644 --- a/game/plugin/skills/runecrafting/build.gradle +++ b/game/plugin/skills/runecrafting/build.gradle @@ -1,10 +1,12 @@ plugin { name = "runecrafting-skill" authors = [ - "Major", - "BugCrusher", - "tlf30" + "Major", + "BugCrusher", + "tlf30" ] - dependencies = ["util:lookup", "api"] + dependencies = [ + "api" + ] } \ No newline at end of file diff --git a/game/plugin/util/lookup/build.gradle b/game/plugin/util/lookup/build.gradle deleted file mode 100644 index 6c225ddf..00000000 --- a/game/plugin/util/lookup/build.gradle +++ /dev/null @@ -1,3 +0,0 @@ -plugin { - name = "entity_lookup" - } \ No newline at end of file diff --git a/game/plugin/util/lookup/src/lookup.kt b/game/plugin/util/lookup/src/lookup.kt deleted file mode 100644 index 895d63c3..00000000 --- a/game/plugin/util/lookup/src/lookup.kt +++ /dev/null @@ -1,37 +0,0 @@ -package org.apollo.game.plugin.util.lookup - -import org.apollo.cache.def.ItemDefinition -import org.apollo.cache.def.NpcDefinition -import org.apollo.cache.def.ObjectDefinition - -fun lookup_object(name: String): ObjectDefinition? { - return find_entity(ObjectDefinition::getDefinitions, ObjectDefinition::getName, name) -} - -fun lookup_npc(name: String): NpcDefinition? { - return find_entity(NpcDefinition::getDefinitions, NpcDefinition::getName, name) -} - -fun lookup_item(name: String): ItemDefinition? { - return find_entity(ItemDefinition::getDefinitions, ItemDefinition::getName, name) -} - -/** - * The [Regex] used to match 'names' that have an id attached to the end. - */ -private val ID_REGEX = Regex(".+_[0-9]+$") - -private fun find_entity(definitionsProvider: () -> Array, - nameSupplier: T.() -> String, - name: String): T? { - val definitions = definitionsProvider.invoke() - - if (ID_REGEX matches name) { - val id = name.substring(name.lastIndexOf('_') + 1, name.length).toInt() - return definitions.getOrNull(id) - } - - val normalizedName = name.replace('_', ' ') - - return definitions.firstOrNull { it.nameSupplier().equals(normalizedName, true) } -} \ No newline at end of file