From f9f22a5f61d129a17073a795537dcb222907964f Mon Sep 17 00:00:00 2001 From: hannibal2 <24389977+hannibal00212@users.noreply.github.com> Date: Sat, 17 Sep 2022 19:30:57 +0200 Subject: added player chat filter --- .../at/hannibal2/skyhanni/utils/MultiFilter.kt | 40 ++++++++++++++++------ 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src/main/java/at/hannibal2/skyhanni/utils') diff --git a/src/main/java/at/hannibal2/skyhanni/utils/MultiFilter.kt b/src/main/java/at/hannibal2/skyhanni/utils/MultiFilter.kt index 5786cb9e7..947019aa6 100644 --- a/src/main/java/at/hannibal2/skyhanni/utils/MultiFilter.kt +++ b/src/main/java/at/hannibal2/skyhanni/utils/MultiFilter.kt @@ -4,21 +4,24 @@ import com.google.gson.JsonObject class MultiFilter { - val equals = mutableListOf() - val startsWith = mutableListOf() - val endsWith = mutableListOf() - val contains = mutableListOf() + private val equals = mutableListOf() + private val startsWith = mutableListOf() + private val endsWith = mutableListOf() + private val contains = mutableListOf() + private val containsWord = mutableListOf() fun load(hideNpcSell: JsonObject) { equals.clear() startsWith.clear() endsWith.clear() contains.clear() + containsWord.clear() fill(hideNpcSell, "equals", equals) fill(hideNpcSell, "startsWith", startsWith) fill(hideNpcSell, "endsWith", endsWith) fill(hideNpcSell, "contains", contains) + fill(hideNpcSell, "containsWord", containsWord) } private fun fill(jsonObject: JsonObject, key: String, list: MutableList) { @@ -27,12 +30,29 @@ class MultiFilter { } } - fun match(name: String): Boolean { - if (equals.contains(name)) return true - if (startsWith.any { name.startsWith(it) }) return true - if (endsWith.any { name.endsWith(it) }) return true - if (contains.any { name.contains(it) }) return true + fun match(string: String): Boolean { + return matchResult(string) != null + } + + fun matchResult(string: String): String? { + var result = equals.find { it == string } + if (result != null) return result + result = startsWith.find { string.startsWith(it) } + if (result != null) return result + result = endsWith.find { string.endsWith(it) } + if (result != null) return result + result = contains.find { string.contains(it) } + if (result != null) return result + result = containsWord.find { containsWord(string, it) } + if (result != null) return result + + return null + } + + private fun containsWord(message: String, word: String): Boolean = + message.startsWith("$word ") || message.endsWith(" $word") || message.contains(" $word ") - return false + fun count(): Int { + return equals.size + startsWith.size + endsWith.size + contains.size + containsWord.size } } \ No newline at end of file -- cgit