From 6d202abbc2ef012a02adbfd7870abb3d52e6ae3f Mon Sep 17 00:00:00 2001 From: Major Date: Sat, 23 Sep 2017 04:11:44 +0100 Subject: [PATCH] Support lookup_x inputs with the id at the end --- game/plugin/util/lookup/src/lookup.kt | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/game/plugin/util/lookup/src/lookup.kt b/game/plugin/util/lookup/src/lookup.kt index 637f4fab..a789b17d 100644 --- a/game/plugin/util/lookup/src/lookup.kt +++ b/game/plugin/util/lookup/src/lookup.kt @@ -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 find_entity(definitionsProvider: () -> Array, 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) } } \ No newline at end of file