mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-05 00:38:14 +00:00
Remove util:lookup plugin
Behaviour moved into the api plugin.
This commit is contained in:
@@ -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) }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import org.apollo.cache.def.ItemDefinition
|
||||
import org.apollo.game.plugin.api.Definitions
|
||||
import org.apollo.game.plugin.testing.KotlinPluginTest
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.Test
|
||||
|
||||
class NamedLookupTests : KotlinPluginTest() {
|
||||
@Test
|
||||
fun itemLookup() {
|
||||
val testItem = ItemDefinition(0)
|
||||
testItem.name = "sword"
|
||||
|
||||
ItemDefinition.init(arrayOf(testItem))
|
||||
|
||||
assertThat(Definitions.item("sword")).isEqualTo(testItem)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user