Skip to content
This repository has been archived by the owner on May 12, 2024. It is now read-only.

Commit

Permalink
Optimized compatible stack search
Browse files Browse the repository at this point in the history
  • Loading branch information
Luna5ama committed Sep 17, 2023
1 parent f1a46f1 commit 8c2bb27
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,12 @@ import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.inventory.Container
import net.minecraft.inventory.ContainerShulkerBox
import net.minecraft.inventory.Slot
import net.minecraft.item.Item
import net.minecraft.item.ItemArmor
import net.minecraft.item.ItemShulkerBox
import net.minecraft.network.play.client.CPacketPlayer
import net.minecraft.network.play.client.CPacketPlayerTryUseItemOnBlock
import net.minecraft.util.EnumFacing
import net.minecraft.util.EnumHand
import net.minecraft.util.math.AxisAlignedBB
import net.minecraft.util.math.BlockPos

internal object AutoRegear : Module(
Expand Down Expand Up @@ -355,7 +353,7 @@ internal object AutoRegear : Module(

if (isHotbar && slotToStack.item is ItemArmor) continue

val slotFrom = containerSlots.getMaxCompatibleStack(slotTo, targetItem) ?: continue
val slotFrom = containerSlots.findMaxCompatibleStack(slotTo, targetItem) ?: continue

lastTask = if (!hasEmptyBefore && slotToStack.isStackable(slotFrom.stack)) {
inventoryTask {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import dev.luna5ama.trollhack.event.safeParallelListener
import dev.luna5ama.trollhack.module.Category
import dev.luna5ama.trollhack.module.Module
import dev.luna5ama.trollhack.util.inventory.InventoryTask
import dev.luna5ama.trollhack.util.inventory.confirmedOrTrue
import dev.luna5ama.trollhack.util.inventory.executedOrTrue
import dev.luna5ama.trollhack.util.inventory.inventoryTask
import dev.luna5ama.trollhack.util.inventory.operation.moveTo
Expand Down Expand Up @@ -44,7 +43,7 @@ internal object HotbarRefill : Module(
if (stack.count >= (stack.maxStackSize / 64.0f * refillThreshold).ceilToInt()) continue
if (AutoEject.ejectMap.value.containsKey(stack.item.registryName.toString())) continue

val slotFrom = sourceSlots.getMaxCompatibleStack(slotTo) ?: continue
val slotFrom = sourceSlots.findFirstCompatibleStack(slotTo) ?: continue
lastTask = if (slotTo is HotbarSlot) {
inventoryTask {
quickMove(slotFrom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import dev.luna5ama.trollhack.util.inventory.InventoryTask
import dev.luna5ama.trollhack.util.inventory.executedOrTrue
import dev.luna5ama.trollhack.util.inventory.inventoryTask
import dev.luna5ama.trollhack.util.inventory.operation.pickUp
import dev.luna5ama.trollhack.util.inventory.slot.getMaxCompatibleStack
import dev.luna5ama.trollhack.util.inventory.slot.findFirstCompatibleStack
import dev.luna5ama.trollhack.util.inventory.slot.findMaxCompatibleStack
import dev.luna5ama.trollhack.util.inventory.slot.inventorySlots
import dev.luna5ama.trollhack.util.text.NoSpamMessage
import dev.luna5ama.trollhack.util.threads.runSafe
import net.minecraft.init.Items
import net.minecraft.inventory.Slot
import net.minecraft.item.Item

internal object InventorySorter : Module(
name = "Inventory Sorter",
Expand Down Expand Up @@ -93,7 +93,7 @@ internal object InventorySorter : Module(
it.slotNumber != slotTo.slotNumber && targetItem.item == it.stack.item
}
} else {
slots.getMaxCompatibleStack(slotTo, targetItem)
slots.findFirstCompatibleStack(slotTo, targetItem)
}

if (slot == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,11 @@ fun Iterable<Slot>.getCompatibleStack(slotTo: Slot): Slot? {
}


fun Iterable<Slot>.getMaxCompatibleStack(slotTo: Slot): Slot? {
return getMaxCompatibleStack(slotTo, slotTo.stack.item)
fun Iterable<Slot>.findMaxCompatibleStack(slotTo: Slot): Slot? {
return findMaxCompatibleStack(slotTo, slotTo.stack.item)
}

internal fun Iterable<Slot>.getMaxCompatibleStack(slotTo: Slot, targetItem: Kit.ItemEntry): Slot? {
internal fun Iterable<Slot>.findMaxCompatibleStack(slotTo: Slot, targetItem: Kit.ItemEntry): Slot? {
var maxSlot: Slot? = null
var maxSize = 0

Expand Down Expand Up @@ -204,10 +204,10 @@ internal fun Iterable<Slot>.getMaxCompatibleStack(slotTo: Slot, targetItem: Kit.
}
}

return maxSlot ?: getMaxCompatibleStack(slotTo, targetItem.item)
return maxSlot ?: findMaxCompatibleStack(slotTo, targetItem.item)
}

fun Iterable<Slot>.getMaxCompatibleStack(slotTo: Slot, targetItem: Item): Slot? {
fun Iterable<Slot>.findMaxCompatibleStack(slotTo: Slot, targetItem: Item): Slot? {
var maxSlot: Slot? = null
var maxSize = 0

Expand Down Expand Up @@ -239,3 +239,54 @@ fun Iterable<Slot>.getMaxCompatibleStack(slotTo: Slot, targetItem: Item): Slot?

return maxSlot
}


fun Iterable<Slot>.findFirstCompatibleStack(slotTo: Slot): Slot? {
return findFirstCompatibleStack(slotTo, slotTo.stack.item)
}

internal fun Iterable<Slot>.findFirstCompatibleStack(slotTo: Slot, targetItem: Kit.ItemEntry): Slot? {
val stackTo = slotTo.stack
val isEmpty = stackTo.isEmpty
val neededSize = if (isEmpty) 64 else stackTo.maxStackSize - stackTo.count
if (neededSize <= 0) return null

for (slotFrom in this) {
if (slotFrom.slotNumber == slotTo.slotNumber) continue

val stackFrom = slotFrom.stack
if (!targetItem.equals(stackFrom)) continue

if (!isEmpty && targetItem.equals(stackFrom)) {
if (!stackTo.isItemEqual(stackFrom)) continue
if (!ItemStack.areItemStackTagsEqual(stackTo, stackFrom)) continue
}

return slotFrom
}

return findFirstCompatibleStack(slotTo, targetItem.item)
}

fun Iterable<Slot>.findFirstCompatibleStack(slotTo: Slot, targetItem: Item): Slot? {
val stackTo = slotTo.stack
val isEmpty = stackTo.isEmpty
val neededSize = if (isEmpty) 64 else stackTo.maxStackSize - stackTo.count
if (neededSize <= 0) return null

for (slotFrom in this) {
if (slotFrom.slotNumber == slotTo.slotNumber) continue

val stackFrom = slotFrom.stack
if (stackFrom.item != targetItem) continue

if (!isEmpty && stackTo.item == targetItem) {
if (!stackTo.isItemEqual(stackFrom)) continue
if (!ItemStack.areItemStackTagsEqual(stackTo, stackFrom)) continue
}

return slotFrom
}

return null
}

0 comments on commit 8c2bb27

Please sign in to comment.