Support lookup_x inputs with the id at the end

This commit is contained in:
Major
2017-09-23 04:11:44 +01:00
parent 1c8bc3a009
commit 6d202abbc2
+15 -4
View File
@@ -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) }
}