Skip to content

Commit b72fb91

Browse files
committed
Proper sorting builder for StackSelection
1 parent f531ad0 commit b72fb91

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

src/main/kotlin/com/lambda/interaction/material/StackSelection.kt

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

src/main/kotlin/com/lambda/module/modules/combat/KillAura.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ object KillAura : Module(
104104
listen<TickEvent.Pre> {
105105
target?.let { entity ->
106106
if (swap) {
107-
val selection =
108-
selectStack(sorter = compareByDescending { player.attackDamage(stack = it) })
107+
val selection = selectStack().sortByDescending { player.attackDamage(stack = it) }
109108

110109
if (!selection.bestItemMatch(player.hotbarAndStorage).equal(player.mainHandStack))
111110
selection.transfer(MainHandContainer)?.run()
@@ -128,8 +127,7 @@ object KillAura : Module(
128127
}
129128

130129
// Rotation check
131-
run {
132-
if (!rotate) return@run
130+
if (rotate) {
133131
val angle = RotationManager.activeRotation
134132

135133
if (interactionSettings.strictRayCast) {

0 commit comments

Comments
 (0)