From 8454db6785365f95d4031da22f88b608315c3bcf Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 29 Mar 2025 12:07:32 +0700 Subject: [PATCH] Some fixes for item filters --- .../ru/dbotthepony/mc/otm/container/ItemFilterSet.kt | 8 +++----- .../dbotthepony/mc/otm/menu/input/ItemFilterInput.kt | 10 ++++++++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ItemFilterSet.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ItemFilterSet.kt index 8319020b8..18c43bafe 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/container/ItemFilterSet.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/container/ItemFilterSet.kt @@ -22,9 +22,7 @@ data class ItemFilterSet(val filter: ImmutableSet, val isWhitelist: } fun replace(index: Int, value: ItemFilter): ItemFilterSet { - if (index !in filter.indices) - throw IndexOutOfBoundsException("No such filter at index $index") - else if (value in filter) + if (index !in filter.indices || value in filter) return this val values = ObjectArrayList(filter) @@ -50,7 +48,7 @@ data class ItemFilterSet(val filter: ImmutableSet, val isWhitelist: fun removeAt(index: Int): ItemFilterSet { if (index !in filter.indices) - throw IndexOutOfBoundsException("No such filter at index $index") + return this if (filter.size == 1) return copy(filter = ImmutableSet.of()) @@ -65,7 +63,7 @@ data class ItemFilterSet(val filter: ImmutableSet, val isWhitelist: } operator fun get(index: Int): ItemFilter { - return filter.asList()[index] + return filter.asList().getOrElse(index) { ItemFilter.EMPTY } } fun clear(): ItemFilterSet { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/ItemFilterInput.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/ItemFilterInput.kt index 43c96518c..e944af6a6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/ItemFilterInput.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/input/ItemFilterInput.kt @@ -16,8 +16,14 @@ class ItemFilterInput(menu: MatteryMenu, maxSlots: Int, var filter: Delegate menu.PlayerInput(StreamCodecs.ITEM_FILTER, handler = { - if (allowRecursive || it.depth <= 1) - filter?.get()?.addOrReplace(i, it) + if (allowRecursive || it.depth <= 1) { + val filter = filter ?: return@PlayerInput + + if (it.hasRules) + filter.accept(filter.get().addOrReplace(i, it)) + else + filter.accept(filter.get().removeAt(i)) + } }) }