Remove util:lookup plugin

Behaviour moved into the api plugin.
This commit is contained in:
Major
2018-04-08 16:19:02 +01:00
parent fdabea8162
commit 8fe09880b7
11 changed files with 66 additions and 72 deletions
+34
View File
@@ -9,11 +9,45 @@ object Definitions {
return ItemDefinition.lookup(id) return ItemDefinition.lookup(id)
} }
fun item(name: String): ItemDefinition? {
return findEntity(ItemDefinition::getDefinitions, ItemDefinition::getName, name)
}
fun obj(id: Int): ObjectDefinition? { fun obj(id: Int): ObjectDefinition? {
return ObjectDefinition.lookup(id) return ObjectDefinition.lookup(id)
} }
fun obj(name: String): ObjectDefinition? {
return findEntity(ObjectDefinition::getDefinitions, ObjectDefinition::getName, name)
}
fun npc(id: Int): NpcDefinition? { fun npc(id: Int): NpcDefinition? {
return NpcDefinition.lookup(id) 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 <T : Any> findEntity(
definitionsProvider: () -> Array<T>,
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) }
}
} }
@@ -1,10 +1,10 @@
import org.apollo.cache.def.ItemDefinition 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.testing.KotlinPluginTest
import org.apollo.game.plugin.util.lookup.lookup_item
import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThat
import org.junit.Test import org.junit.Test
class LookupTests : KotlinPluginTest() { class NamedLookupTests : KotlinPluginTest() {
@Test @Test
fun itemLookup() { fun itemLookup() {
val testItem = ItemDefinition(0) val testItem = ItemDefinition(0)
@@ -12,6 +12,6 @@ class LookupTests : KotlinPluginTest() {
ItemDefinition.init(arrayOf(testItem)) ItemDefinition.init(arrayOf(testItem))
assertThat(lookup_item("sword")).isEqualTo(testItem) assertThat(Definitions.item("sword")).isEqualTo(testItem)
} }
} }
+1 -1
View File
@@ -4,6 +4,6 @@ plugin {
"Gary Tierney", "Gary Tierney",
] ]
dependencies = [ dependencies = [
"util:lookup", "api",
] ]
} }
@@ -1,18 +1,18 @@
import org.apollo.cache.def.NpcDefinition
import org.apollo.game.model.entity.Npc 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.entity.spawn.Spawns
import org.apollo.game.plugin.util.lookup.lookup_npc
start { world -> start { world ->
Spawns.list.forEach { Spawns.list.forEach { spawn ->
val definition = it.id?.let { NpcDefinition.lookup(it) } ?: lookup_npc(it.name) ?: val definition = spawn.id?.let(Definitions::npc) ?: Definitions.npc(spawn.name)
throw IllegalArgumentException("Invalid NPC name or ID ${it.name}, ${it.id}") ?: throw IllegalArgumentException("Invalid NPC name or ID ${spawn.name}, ${spawn.id}")
val npc = Npc(world, definition.id, it.position) val npc = Npc(world, definition.id, spawn.position)
npc.turnTo(it.position.step(1, it.facing)) npc.turnTo(spawn.position.step(1, spawn.facing))
it.spawnAnimation?.let(npc::playAnimation) spawn.spawnAnimation?.let(npc::playAnimation)
it.spawnGraphic?.let(npc::playGraphic) spawn.spawnGraphic?.let(npc::playGraphic)
world.register(npc) world.register(npc)
} }
+4 -4
View File
@@ -1,11 +1,11 @@
plugin { plugin {
name = "shops" name = "shops"
authors = [ authors = [
"Stuart", "Stuart",
"Major", "Major",
"tlf30" "tlf30"
] ]
dependencies = [ dependencies = [
"util:lookup", "api",
] ]
} }
+3 -4
View File
@@ -1,9 +1,8 @@
package org.apollo.game.plugin.shops package org.apollo.game.plugin.shops
import org.apollo.cache.def.NpcDefinition 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.shops.CategoryWrapper.Affix
import org.apollo.game.plugin.util.lookup.lookup_item
import org.apollo.game.plugin.util.lookup.lookup_npc
/** /**
* Creates a [Shop]. * Creates a [Shop].
@@ -109,7 +108,7 @@ class ShopBuilder(val name: String) {
* Converts this builder into a [Shop]. * Converts this builder into a [Shop].
*/ */
internal fun build(): Shop { internal fun build(): Shop {
val items = sold.associateBy({ (first) -> lookup_item(first)!!.id }, Pair<String, Int>::second) val items = sold.associateBy({ (first) -> Definitions.item(first)!!.id }, Pair<String, Int>::second)
val npc = NpcDefinition.lookup(operators().first()) val npc = NpcDefinition.lookup(operators().first())
return Shop(name, action.action(npc), items, trades.currency, buys.policy) 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. * Adds a shop operator, using the specified [name] to resolve the npc id.
*/ */
infix fun by(name: String): OperatorBuilder { infix fun by(name: String): OperatorBuilder {
operators.add(lookup_npc(name)!!.id) operators.add(Definitions.npc(name)!!.id)
return this return this
} }
+5 -6
View File
@@ -1,13 +1,12 @@
plugin { plugin {
name = "fishing_skill" name = "fishing_skill"
authors = [ authors = [
"Linux", "Linux",
"Major", "Major",
"tlf30" "tlf30"
] ]
dependencies = [ dependencies = [
"api", "api",
"entity:spawn", "entity:spawn",
"util:lookup"
] ]
} }
+1 -1
View File
@@ -6,6 +6,6 @@ plugin {
"tlf30" "tlf30"
] ]
dependencies = [ dependencies = [
"util:lookup", "api" "api"
] ]
} }
+6 -4
View File
@@ -1,10 +1,12 @@
plugin { plugin {
name = "runecrafting-skill" name = "runecrafting-skill"
authors = [ authors = [
"Major", "Major",
"BugCrusher", "BugCrusher",
"tlf30" "tlf30"
] ]
dependencies = ["util:lookup", "api"] dependencies = [
"api"
]
} }
-3
View File
@@ -1,3 +0,0 @@
plugin {
name = "entity_lookup"
}
-37
View File
@@ -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 <T : Any> find_entity(definitionsProvider: () -> Array<T>,
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) }
}