mirror of
https://github.com/2006-Scape/apollo.git
synced 2026-07-03 00:38:21 +00:00
Support lookup_x inputs with the id at the end
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import org.apollo.cache.def.*
|
||||
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)
|
||||
@@ -12,13 +14,22 @@ 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('_', ' ')
|
||||
val definitions = definitionsProvider.invoke();
|
||||
val matcher: (T) -> Boolean = { it.nameSupplier().equals(normalizedName, true) }
|
||||
|
||||
return definitions.filter(matcher).firstOrNull()
|
||||
return definitions.firstOrNull { it.nameSupplier().equals(normalizedName, true) }
|
||||
}
|
||||
Reference in New Issue
Block a user