@@ -51,7 +51,7 @@ class StackSelection {
5151 /* *
5252 * Filters the given [stacks], sorts them with the [comparator] and returns the first value
5353 */
54- fun bestItemMatch (stacks : List <ItemStack >): ItemStack ? = filterStacks( stacks).firstOrNull( )
54+ fun bestItemMatch (stacks : List <ItemStack >): ItemStack ? = stacks.minWithOrNull(comparator )
5555
5656 fun filterStack (stack : ItemStack ) =
5757 if (inShulkerBox) stack.shulkerBoxContents.any { selector(it) }
@@ -65,6 +65,38 @@ class StackSelection {
6565 fun filterSlots (slots : List <Slot >): List <Slot > =
6666 slots.filter(::filterSlot).sortedWith { slot, slot2 -> comparator.compare(slot.stack, slot2.stack) }
6767
68+ @StackSelectionDsl
69+ fun <R : Comparable <R >> sortBy (selector : (ItemStack ) -> R ? ): StackSelection = apply {
70+ comparator = compareBy(selector)
71+ }
72+
73+ @StackSelectionDsl
74+ fun <R : Comparable <R >> sortByDescending (selector : (ItemStack ) -> R ? ): StackSelection = apply {
75+ comparator = compareByDescending(selector)
76+ }
77+
78+ @StackSelectionDsl
79+ fun <R : Comparable <R >> thenBy (selector : (ItemStack ) -> R ? ): StackSelection = apply {
80+ check(comparator != NO_COMPARE ) { " No comparator specified" }
81+ comparator = comparator.thenBy(selector)
82+ }
83+
84+ @StackSelectionDsl
85+ fun <R : Comparable <R >> thenByDescending (selector : (ItemStack ) -> R ? ): StackSelection = apply {
86+ check(comparator != NO_COMPARE ) { " No comparator specified" }
87+ comparator = comparator.thenByDescending(selector)
88+ }
89+
90+ @StackSelectionDsl
91+ fun sortWith (custom : Comparator <ItemStack >): StackSelection = apply {
92+ comparator = custom
93+ }
94+
95+ @StackSelectionDsl
96+ fun reversed (): StackSelection = apply {
97+ comparator = comparator.reversed()
98+ }
99+
68100 /* *
69101 * returns a function that finds a shulker box to push matching items into.
70102 */
@@ -237,8 +269,7 @@ class StackSelection {
237269 val EMPTY_SHULKERS : (ItemStack ) -> Boolean = { stack -> stack.shulkerBoxContents.all { it.isEmpty } }
238270 val EVERYTHING : (ItemStack ) -> Boolean = { true }
239271 val NOTHING : (ItemStack ) -> Boolean = { false }
240-
241- val NO_COMPARE : Comparator <ItemStack > = compareBy { 69 - 420 }
272+ val NO_COMPARE : Comparator <ItemStack > = Comparator { _, _ -> 0 }
242273
243274 @StackSelectionDsl
244275 fun Item.select () = selectStack { isItem(this @select) }
@@ -257,23 +288,6 @@ class StackSelection {
257288 @StackSelectionDsl
258289 fun ((ItemStack ) -> Boolean ).select() = selectStack { this @select }
259290
260- /* *
261- * Builds a [StackSelection] with the given parameters.
262- * @param count The count of items to be selected.
263- * @param block The predicate to be used to select the items.
264- * @return A [StackSelection] with the given parameters.
265- */
266- @StackSelectionDsl
267- fun selectStack (
268- count : Int = DEFAULT_AMOUNT ,
269- inShulkerBox : Boolean = false,
270- block : StackSelection .() -> (ItemStack ) -> Boolean = { EVERYTHING },
271- ) = StackSelection ().apply {
272- selector = block()
273- this .count = count
274- this .inShulkerBox = inShulkerBox
275- }
276-
277291 @StackSelectionDsl
278292 fun selectStack (
279293 count : Int = DEFAULT_AMOUNT ,
0 commit comments