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