From d0fec15a84cc97c625930c4adbc09b400516aeb0 Mon Sep 17 00:00:00 2001 From: Gary Tierney Date: Sun, 18 Jun 2017 21:45:52 +0100 Subject: [PATCH] Add missing name normalization to lookup plugin --- game/src/plugins/util/lookup/src/lookup.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/game/src/plugins/util/lookup/src/lookup.kt b/game/src/plugins/util/lookup/src/lookup.kt index c7395e10..637f4fab 100644 --- a/game/src/plugins/util/lookup/src/lookup.kt +++ b/game/src/plugins/util/lookup/src/lookup.kt @@ -1,17 +1,24 @@ import org.apollo.cache.def.* fun lookup_object(name: String): ObjectDefinition? { - val definitions = ObjectDefinition.getDefinitions() - return definitions.filter { name.equals(it.name, true) }.firstOrNull() + return find_entity(ObjectDefinition::getDefinitions, ObjectDefinition::getName, name) } fun lookup_npc(name: String): NpcDefinition? { - val definitions = NpcDefinition.getDefinitions() - return definitions.filter { name.equals(it.name, true) }.firstOrNull() + return find_entity(NpcDefinition::getDefinitions, NpcDefinition::getName, name) } fun lookup_item(name: String): ItemDefinition? { - val definitions = ItemDefinition.getDefinitions() - return definitions.filter { name.equals(it.name, true) }.firstOrNull() + return find_entity(ItemDefinition::getDefinitions, ItemDefinition::getName, name) } +private fun find_entity(definitionsProvider: () -> Array, + nameSupplier: T.() -> String, + name: String): T? { + + val normalizedName = name.replace('_', ' ') + val definitions = definitionsProvider.invoke(); + val matcher: (T) -> Boolean = { it.nameSupplier().equals(normalizedName, true) } + + return definitions.filter(matcher).firstOrNull() +} \ No newline at end of file