Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
[fix] AutoOffhand item switching spam
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama committed Mar 12, 2021
1 parent 99282bb commit 8410e4f
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ internal object AutoOffhand : Module(
safeListener<PacketEvent.Receive> {
if (it.packet !is SPacketConfirmTransaction || it.packet.windowId != 0 || !transactionLog.containsKey(it.packet.actionNumber)) return@safeListener

transactionLog[it.packet.actionNumber] = it.packet.wasAccepted()
transactionLog[it.packet.actionNumber] = true
if (!transactionLog.containsValue(false)) {
confirmTimer.reset(confirmTimeout * -50L) // If all the click packets were accepted then we reset the timer for next moving
}
Expand Down Expand Up @@ -158,12 +158,13 @@ internal object AutoOffhand : Module(
// First check for whether player is holding the right item already or not
if (typeOriginal == null || checkOffhandItem(typeOriginal)) return

getItemSlot(typeOriginal)?.let { (slot, typeAlt) ->
// Second check is for case of when player ran out of the original type of item
if (!alternativeType && typeAlt != typeOriginal || checkOffhandItem(typeAlt)) return
val attempts = if (alternativeType) 4 else 1

getItemSlot(typeOriginal, attempts)?.let { (slot, typeAlt) ->
if (slot == player.offhandSlot) return

transactionLog.clear()
moveToSlot(slot.slotNumber, 45).forEach {
moveToSlot(slot, player.offhandSlot).forEach {
transactionLog[it] = false
}

Expand All @@ -178,22 +179,32 @@ internal object AutoOffhand : Module(

private fun SafeClientEvent.checkOffhandItem(type: Type) = type.filter(player.heldItemOffhand)

private fun SafeClientEvent.getItemSlot(type: Type, loopTime: Int = 1): Pair<Slot, Type>? =
private fun SafeClientEvent.getItemSlot(type: Type, attempts: Int): Pair<Slot, Type>? =
getSlot(type)?.to(type)
?: if (loopTime <= 3) {
getItemSlot(type.next(), loopTime + 1)
?: if (attempts > 1) {
getItemSlot(type.next(), attempts - 1)
} else {
null
}

private fun SafeClientEvent.getSlot(type: Type): Slot? {
val slots = player.inventorySlots
return player.offhandSlot.takeIf(filter(type))
?: if (priority == Priority.HOTBAR) {
player.hotbarSlots.findItemByType(type)
?: player.inventorySlots.findItemByType(type)
?: player.craftingSlots.findItemByType(type)
} else {
player.inventorySlots.findItemByType(type)
?: player.hotbarSlots.findItemByType(type)
?: player.craftingSlots.findItemByType(type)
}
}

return if (priority == Priority.HOTBAR) {
slots.lastOrNull { type.filter(it.stack) }
} else {
slots.firstOrNull { type.filter(it.stack) }
}
private fun List<Slot>.findItemByType(type: Type) =
find(filter(type))

private fun filter(type: Type) = { it: Slot ->
type.filter(it.stack)
}

private fun SafeClientEvent.updateDamage() {
Expand Down

0 comments on commit 8410e4f

Please sign in to comment.