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)
}
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 <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.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)
}
}
+1 -1
View File
@@ -4,6 +4,6 @@ plugin {
"Gary Tierney",
]
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.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)
}
+4 -4
View File
@@ -1,11 +1,11 @@
plugin {
name = "shops"
authors = [
"Stuart",
"Major",
"tlf30"
"Stuart",
"Major",
"tlf30"
]
dependencies = [
"util:lookup",
"api",
]
}
+3 -4
View File
@@ -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<String, Int>::second)
val items = sold.associateBy({ (first) -> Definitions.item(first)!!.id }, Pair<String, Int>::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
}
+5 -6
View File
@@ -1,13 +1,12 @@
plugin {
name = "fishing_skill"
authors = [
"Linux",
"Major",
"tlf30"
"Linux",
"Major",
"tlf30"
]
dependencies = [
"api",
"entity:spawn",
"util:lookup"
"api",
"entity:spawn",
]
}
+1 -1
View File
@@ -6,6 +6,6 @@ plugin {
"tlf30"
]
dependencies = [
"util:lookup", "api"
"api"
]
}
+6 -4
View File
@@ -1,10 +1,12 @@
plugin {
name = "runecrafting-skill"
authors = [
"Major",
"BugCrusher",
"tlf30"
"Major",
"BugCrusher",
"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) }
}