Some fixes for item filters

This commit is contained in:
DBotThePony 2025-03-29 12:07:32 +07:00
parent 78fad5d3cc
commit 8454db6785
Signed by: DBot
GPG Key ID: DCC23B5715498507
2 changed files with 11 additions and 7 deletions

View File

@ -22,9 +22,7 @@ data class ItemFilterSet(val filter: ImmutableSet<ItemFilter>, 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<ItemFilter>, 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<ItemFilter>, val isWhitelist:
}
operator fun get(index: Int): ItemFilter {
return filter.asList()[index]
return filter.asList().getOrElse(index) { ItemFilter.EMPTY }
}
fun clear(): ItemFilterSet {

View File

@ -16,8 +16,14 @@ class ItemFilterInput(menu: MatteryMenu, maxSlots: Int, var filter: Delegate<Ite
val inputs = immutableList(maxSlots) { i ->
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))
}
})
}