diff --git a/src/main/kotlin/com/lambda/Lambda.kt b/src/main/kotlin/com/lambda/Lambda.kt index e5a3c6dc5..046f9f900 100644 --- a/src/main/kotlin/com/lambda/Lambda.kt +++ b/src/main/kotlin/com/lambda/Lambda.kt @@ -17,7 +17,6 @@ package com.lambda -import com.lambda.Lambda.mapper import com.lambda.config.Deserializer import com.lambda.config.FallbackDeserializer import com.lambda.config.FallbackSerializer diff --git a/src/main/kotlin/com/lambda/config/ConfigCategory.kt b/src/main/kotlin/com/lambda/config/ConfigCategory.kt index 35ec67f6c..09e10e230 100644 --- a/src/main/kotlin/com/lambda/config/ConfigCategory.kt +++ b/src/main/kotlin/com/lambda/config/ConfigCategory.kt @@ -25,7 +25,6 @@ import com.lambda.config.migration.ConfigMigrationHandler import com.lambda.core.Loadable import com.lambda.event.events.ClientEvent import com.lambda.event.listener.UnsafeListener.Companion.listenUnsafe -import com.lambda.interaction.handler.handlers.BaritoneHandler.primary import com.lambda.threading.runIO import com.lambda.util.CommunicationUtils.info import com.lambda.util.CommunicationUtils.logError diff --git a/src/main/kotlin/com/lambda/config/categories/ModuleCategory.kt b/src/main/kotlin/com/lambda/config/categories/ModuleCategory.kt index 75eb37f5a..ffeef56d9 100644 --- a/src/main/kotlin/com/lambda/config/categories/ModuleCategory.kt +++ b/src/main/kotlin/com/lambda/config/categories/ModuleCategory.kt @@ -18,7 +18,6 @@ package com.lambda.config.categories import com.lambda.config.ConfigCategory -import com.lambda.config.categories.ModuleCategory.name import com.lambda.util.FolderRegistry import java.io.File diff --git a/src/main/kotlin/com/lambda/config/serializers/EnderChestContainerSerializer.kt b/src/main/kotlin/com/lambda/config/serializers/EnderChestContainerSerializer.kt index 08c442617..96a5c7add 100644 --- a/src/main/kotlin/com/lambda/config/serializers/EnderChestContainerSerializer.kt +++ b/src/main/kotlin/com/lambda/config/serializers/EnderChestContainerSerializer.kt @@ -84,7 +84,7 @@ object EnderChestContainerDeserializer : Deserializer(Ender } else decodedStacks EnderChestContainer.update(stacks) - EnderChestContainer.scanStacksForNestedContainers() + EnderChestContainer.scanContainerContents() return EnderChestContainer } } diff --git a/src/main/kotlin/com/lambda/config/serializers/PlacedContainerSerializer.kt b/src/main/kotlin/com/lambda/config/serializers/PlacedContainerSerializer.kt index ce2872caf..115d5a2ef 100644 --- a/src/main/kotlin/com/lambda/config/serializers/PlacedContainerSerializer.kt +++ b/src/main/kotlin/com/lambda/config/serializers/PlacedContainerSerializer.kt @@ -125,7 +125,7 @@ object PlacedContainerDeserializer : FallbackDeserializer(Place else -> return null } - container.scanStacksForNestedContainers() + container.scanContainerContents() return container } } diff --git a/src/main/kotlin/com/lambda/event/EventFlow.kt b/src/main/kotlin/com/lambda/event/EventFlow.kt index e21723c90..50b224229 100644 --- a/src/main/kotlin/com/lambda/event/EventFlow.kt +++ b/src/main/kotlin/com/lambda/event/EventFlow.kt @@ -18,11 +18,6 @@ package com.lambda.event import com.lambda.context.SafeContext -import com.lambda.event.EventFlow.concurrentFlow -import com.lambda.event.EventFlow.concurrentListeners -import com.lambda.event.EventFlow.lambdaScope -import com.lambda.event.EventFlow.post -import com.lambda.event.EventFlow.syncListeners import com.lambda.event.callback.ICancellable import com.lambda.event.listener.Listener import com.lambda.threading.runConcurrent diff --git a/src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt b/src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt index c6c126daf..f79770219 100644 --- a/src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt +++ b/src/main/kotlin/com/lambda/event/listener/UnsafeListener.kt @@ -21,9 +21,6 @@ import com.lambda.context.SafeContext import com.lambda.event.Event import com.lambda.event.EventFlow import com.lambda.event.Muteable -import com.lambda.event.listener.SafeListener.Companion.listen -import com.lambda.event.listener.SafeListener.Companion.listenConcurrently -import com.lambda.event.listener.SafeListener.Companion.listenOnce import com.lambda.threading.runConcurrent import com.lambda.util.Pointer import com.lambda.util.collections.updatable diff --git a/src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt b/src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt index 5ba9f8b16..0031980d7 100644 --- a/src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt +++ b/src/main/kotlin/com/lambda/gui/dsl/ImGuiBuilder.kt @@ -20,10 +20,6 @@ package com.lambda.gui.dsl import com.lambda.gui.components.ClickGuiLayout -import com.lambda.gui.dsl.ImGuiBuilder.onItemClick -import com.lambda.gui.dsl.ImGuiBuilder.onItemHover -import com.lambda.gui.dsl.ImGuiBuilder.openPopup -import com.lambda.gui.dsl.ImGuiBuilder.text import com.lambda.imgui.ImDrawList import com.lambda.imgui.ImFont import com.lambda.imgui.ImGui diff --git a/src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/GenericResult.kt b/src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/GenericResult.kt index b02445f0e..5183b8bf9 100644 --- a/src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/GenericResult.kt +++ b/src/main/kotlin/com/lambda/interaction/construction/simulation/result/results/GenericResult.kt @@ -31,7 +31,6 @@ import com.lambda.interaction.container.selection.StackSelection import com.lambda.interaction.container.selection.select import com.lambda.interaction.handler.handlers.BaritoneHandler import com.lambda.task.tasks.transfer -import net.minecraft.client.data.TextureMap.side import net.minecraft.item.ItemStack import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Box diff --git a/src/main/kotlin/com/lambda/interaction/construction/simulation/sims/InteractSim.kt b/src/main/kotlin/com/lambda/interaction/construction/simulation/sims/InteractSim.kt index 74eaccae2..bb9151dee 100644 --- a/src/main/kotlin/com/lambda/interaction/construction/simulation/sims/InteractSim.kt +++ b/src/main/kotlin/com/lambda/interaction/construction/simulation/sims/InteractSim.kt @@ -208,6 +208,9 @@ class InteractSim internal constructor(simInfo: InteractSimInfo) countEmptyAsOne() alwaysWholeStacks() item?.let { isItem(it) } + (targetState as? TargetState.Stack)?.let { target -> + sortedWith { compareByDescending { it.stack === target.itemStack } } + } sortedWith { compareByDescending { it.slot?.index == player.inventory.selectedSlot } } } diff --git a/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt b/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt index 0cb98dd11..3b8f03567 100644 --- a/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt +++ b/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt @@ -35,6 +35,7 @@ import net.minecraft.state.property.Property import net.minecraft.util.math.BlockPos import net.minecraft.util.math.Direction +//ToDo: Rethink this design sealed class TargetState : StateMatcher { data object Empty : TargetState() { override fun toString() = "Empty" diff --git a/src/main/kotlin/com/lambda/interaction/container/Container.kt b/src/main/kotlin/com/lambda/interaction/container/Container.kt index ed2875296..40fcc532d 100644 --- a/src/main/kotlin/com/lambda/interaction/container/Container.kt +++ b/src/main/kotlin/com/lambda/interaction/container/Container.kt @@ -59,6 +59,7 @@ abstract class Container( abstract val slots: List abstract var stacks: List + open val stacksMatchSlots get() = this is ExternalContainer val storedContainers = mutableMapOf() open val dimension = Dimension.Unknown @@ -84,6 +85,8 @@ abstract class Container( open val isAccessed get() = true + open fun slotIdentity(index: Int): SlotIdentity? = null + context(_: SafeContext) fun descriptionAndStock(selection: StackSelection) = buildText { @@ -112,8 +115,12 @@ abstract class Container( this.stacks = slots } - fun scanStacksForNestedContainers() { - stacks.forEachIndexed { index, stack -> + fun scanContainerContents() { + val indexed = + slots.takeUnless { it.isEmpty() }?.map { it.index to it.stack } + ?: stacks.takeIf { stacksMatchSlots }?.mapIndexed { index, stack -> index to stack } + ?: return + indexed.forEach { (index, stack) -> if (stack.item in SHULKER_BOXES) { storedContainers[index] = ShulkerBoxContainer( @@ -122,7 +129,7 @@ abstract class Container( stack.shulkerBoxStacks, this, index - ).also { it.scanStacksForNestedContainers() } + ).also { it.scanContainerContents() } } else storedContainers.remove(index) } } @@ -134,16 +141,14 @@ abstract class Container( open fun count(selection: StackSelection) = slots .takeUnless { it.isEmpty() } - ?.let { - selection.filter(it, false).count(selection.countEmptyAsOne) - } ?: selection.filter(stacks, false).count(selection.countEmptyAsOne) + ?.let { selection.filter(it, false).count(selection.countEmptyAsOne) } + ?: selection.filter(stacks, false).count(selection.countEmptyAsOne) open fun spaceLeft(selection: StackSelection) = slots .takeUnless { it.isEmpty() } - ?.let { - selection.filter(it, false).spaceLeft + selection.emptySpace(it) - } ?: (selection.filter(stacks, false).spaceLeft + stacks.emptySpace) + ?.let { selection.filter(it, false).spaceLeft + selection.emptySpace(it) } + ?: (selection.filter(stacks, false).spaceLeft + stacks.emptySpace) open fun findSlots(selection: StackSelection) = selection.filter(slots) diff --git a/src/main/kotlin/com/lambda/interaction/container/NestedContainer.kt b/src/main/kotlin/com/lambda/interaction/container/NestedContainer.kt index 3b18a6700..e23654848 100644 --- a/src/main/kotlin/com/lambda/interaction/container/NestedContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/NestedContainer.kt @@ -23,6 +23,8 @@ abstract class NestedContainer(type: ContainerType) : Container(type), ExternalC abstract val containedIn: Container abstract val index: Int + val slotIdentity get() = containedIn.slotIdentity(index) + override val dimension get() = containedIn.dimension context(_: Automated) diff --git a/src/main/kotlin/com/lambda/interaction/container/OpenedContainerContext.kt b/src/main/kotlin/com/lambda/interaction/container/OpenedContainerContext.kt index df9b9fdca..534e5c5ae 100644 --- a/src/main/kotlin/com/lambda/interaction/container/OpenedContainerContext.kt +++ b/src/main/kotlin/com/lambda/interaction/container/OpenedContainerContext.kt @@ -23,6 +23,8 @@ import com.lambda.task.Task.Ta5kBuilder import com.lambda.task.tasks.wrappers.actionTask interface OpenedContainerContext { + val container: Container? get() = null + @Ta5kBuilder context(automated: Automated) fun close(): Task<*>? diff --git a/src/main/kotlin/com/lambda/interaction/container/PlacedContainer.kt b/src/main/kotlin/com/lambda/interaction/container/PlacedContainer.kt index 2383ae0af..012439bfb 100644 --- a/src/main/kotlin/com/lambda/interaction/container/PlacedContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/PlacedContainer.kt @@ -27,5 +27,7 @@ abstract class PlacedContainer( ) : Container(type), ExternalContainer { abstract val pos: BlockPos abstract val stash: StashContainer? - open val positions: Set = setOf(pos) + open val positions: Set get() = setOf(pos) + + override fun slotIdentity(index: Int) = SlotIdentity.Placed(positions, index) } \ No newline at end of file diff --git a/src/main/kotlin/com/lambda/interaction/container/SlotIdentity.kt b/src/main/kotlin/com/lambda/interaction/container/SlotIdentity.kt index 5753ef868..0569f00c8 100644 --- a/src/main/kotlin/com/lambda/interaction/container/SlotIdentity.kt +++ b/src/main/kotlin/com/lambda/interaction/container/SlotIdentity.kt @@ -17,6 +17,7 @@ package com.lambda.interaction.container +import com.lambda.Lambda.mc import com.lambda.context.SafeContext import com.lambda.interaction.handler.handlers.ContainerHandler import net.minecraft.block.entity.EnderChestBlockEntity @@ -31,9 +32,8 @@ sealed interface SlotIdentity { data class Placed(val positions: Set, val index: Int) : SlotIdentity data class Ender(val index: Int) : SlotIdentity - context(safeContext: SafeContext) fun resolve(): Slot? { - val handler = safeContext.player.currentScreenHandler + val handler = mc.player?.currentScreenHandler ?: return null val (key, ender) = currentContainerAnchor() return resolveIn(handler.slots, key, ender) } @@ -62,6 +62,9 @@ sealed interface SlotIdentity { if (live.isEmpty || stack.isEmpty) return false return live.item == stack.item && live.name.string == stack.name.string } + + context(safeContext: SafeContext) + fun isEmpty() = resolve()?.stack?.isEmpty == true } fun Slot.identity(): SlotIdentity? { diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/ArmorContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/ArmorContainer.kt index 2a76c37ab..313c0c487 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/ArmorContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/ArmorContainer.kt @@ -20,6 +20,7 @@ package com.lambda.interaction.container.containers import com.lambda.Lambda.mc import com.lambda.interaction.container.Container import com.lambda.interaction.container.ContainerType +import com.lambda.interaction.container.SlotIdentity import com.lambda.util.player.SlotUtils.armorSlots import com.lambda.util.player.SlotUtils.armorStacks import com.lambda.util.text.buildText @@ -37,4 +38,6 @@ object ArmorContainer : Container(ContainerType.Armor) { override val description = buildText { literal("Armor") } override val isAccessed get() = mc.player?.currentScreenHandler?.syncId == 0 + + override fun slotIdentity(index: Int) = SlotIdentity.Player(index) } diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/HotbarContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/HotbarContainer.kt index 3e5ba3744..695c52801 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/HotbarContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/HotbarContainer.kt @@ -21,6 +21,7 @@ import com.lambda.Lambda.mc import com.lambda.context.SafeContext import com.lambda.interaction.container.Container import com.lambda.interaction.container.ContainerType +import com.lambda.interaction.container.SlotIdentity import com.lambda.interaction.manager.managers.inventory.InvRequestBuilder import com.lambda.util.player.SlotUtils.hotbarSlots import com.lambda.util.player.SlotUtils.hotbarStacks @@ -39,6 +40,8 @@ object HotbarContainer : Container(ContainerType.Hotbar) { override val description = buildText { literal("Hotbar") } + override fun slotIdentity(index: Int) = SlotIdentity.Player(index) + context(safeContext: SafeContext) override fun InvRequestBuilder.swap(fromHere: Slot, toSlot: Slot) { swapWithHotbar(toSlot.id, safeContext.player.hotbarSlots.indexOf(fromHere)) diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/InventoryContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/InventoryContainer.kt index f1c56e1cf..c96148033 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/InventoryContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/InventoryContainer.kt @@ -20,6 +20,7 @@ package com.lambda.interaction.container.containers import com.lambda.Lambda.mc import com.lambda.interaction.container.Container import com.lambda.interaction.container.ContainerType +import com.lambda.interaction.container.SlotIdentity import com.lambda.util.player.SlotUtils.inventorySlots import com.lambda.util.player.SlotUtils.inventoryStacks import com.lambda.util.text.buildText @@ -35,4 +36,6 @@ object InventoryContainer : Container(ContainerType.Inventory) { set(_) {} override val description = buildText { literal("Inventory") } + + override fun slotIdentity(index: Int) = SlotIdentity.Player(index) } diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/OffhandContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/OffhandContainer.kt index 21513584a..2596c9ccb 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/OffhandContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/OffhandContainer.kt @@ -21,6 +21,7 @@ import com.lambda.Lambda.mc import com.lambda.context.SafeContext import com.lambda.interaction.container.Container import com.lambda.interaction.container.ContainerType +import com.lambda.interaction.container.SlotIdentity import com.lambda.interaction.manager.managers.inventory.InvRequestBuilder import com.lambda.util.player.SlotUtils.offHandSlots import com.lambda.util.text.buildText @@ -39,6 +40,8 @@ object OffhandContainer : Container(ContainerType.OffHand) { override val description = buildText { literal("OffHand") } + override fun slotIdentity(index: Int) = SlotIdentity.Player(index) + context(safeContext: SafeContext) override fun InvRequestBuilder.swap(fromHere: Slot, toSlot: Slot) { swapWithHotbar(toSlot.id, 40) diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/external/ChestContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/external/ChestContainer.kt index ae38f8a45..238e2dd84 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/external/ChestContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/external/ChestContainer.kt @@ -80,7 +80,7 @@ data class ChestContainer( automated: Automated ) : OpenContainerTask(description), Automated by automated { override fun SafeContext.onStart() { - openContainer(pos) + openContainer(pos) .onSuccess { success(BasicOpenedContainerContext(::isAccessed)) } .start() } diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/external/DoubleChestContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/external/DoubleChestContainer.kt index 4b5dd9d14..6e8af9bc1 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/external/DoubleChestContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/external/DoubleChestContainer.kt @@ -92,8 +92,8 @@ data class DoubleChestContainer( val rightDist = rightPos distSq player.pos val closestPos = if (leftDist <= rightDist) leftPos else rightPos val farthestPos = if (leftDist > rightDist) leftPos else rightPos - openContainer(closestPos) - .withRecovery { openContainer(farthestPos) } + openContainer(closestPos) + .withRecovery { openContainer(farthestPos) } .onSuccess { success(BasicOpenedContainerContext(::isAccessed)) } .start() } diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/external/EnderChestContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/external/EnderChestContainer.kt index 12eb8e38f..507c187c4 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/external/EnderChestContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/external/EnderChestContainer.kt @@ -25,6 +25,7 @@ import com.lambda.interaction.container.ContainerType import com.lambda.interaction.container.ExternalContainer import com.lambda.interaction.container.OpenContainerTask import com.lambda.interaction.container.OpenedContainerContext +import com.lambda.interaction.container.SlotIdentity import com.lambda.interaction.container.containers.HotbarContainer import com.lambda.interaction.container.selection.select import com.lambda.interaction.handler.handlers.ContainerHandler.findSlot @@ -67,6 +68,8 @@ object EnderChestContainer : Container(ContainerType.EnderChest), ExternalContai mc.player?.currentScreenHandler?.typeSafe == ScreenHandlerType.GENERIC_9X3 } ?: false + override fun slotIdentity(index: Int) = SlotIdentity.Ender(index) + @Ta5kBuilder context(automated: Automated) override fun access() = diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/external/PlacedShulkerBoxContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/external/PlacedShulkerBoxContainer.kt index 5fbab6e76..5a82c7259 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/external/PlacedShulkerBoxContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/external/PlacedShulkerBoxContainer.kt @@ -80,7 +80,7 @@ class PlacedShulkerBoxContainer( automated: Automated ) : OpenContainerTask(description), Automated by automated { override fun SafeContext.onStart() { - openContainer(pos) + openContainer(pos) .onSuccess { success(BasicOpenedContainerContext(::isAccessed)) } .start() } diff --git a/src/main/kotlin/com/lambda/interaction/container/containers/external/ShulkerBoxContainer.kt b/src/main/kotlin/com/lambda/interaction/container/containers/external/ShulkerBoxContainer.kt index 0ccaca878..3b2b57ca9 100644 --- a/src/main/kotlin/com/lambda/interaction/container/containers/external/ShulkerBoxContainer.kt +++ b/src/main/kotlin/com/lambda/interaction/container/containers/external/ShulkerBoxContainer.kt @@ -28,10 +28,10 @@ import com.lambda.interaction.container.OpenContainerTask import com.lambda.interaction.container.OpenedContainerContext import com.lambda.interaction.container.containers.HotbarContainer import com.lambda.interaction.container.selection.ContainerSelection -import com.lambda.interaction.container.selection.mutate import com.lambda.interaction.container.selection.select import com.lambda.interaction.container.selection.stackSelection import com.lambda.interaction.handler.handlers.ContainerHandler.findSlot +import com.lambda.interaction.handler.handlers.ContainerHandler.slotsAvailable import com.lambda.interaction.handler.handlers.ContainerSearchScope import com.lambda.task.Task import com.lambda.task.Task.Ta5kBuilder @@ -42,6 +42,7 @@ import com.lambda.task.tasks.transfer import com.lambda.task.tasks.wrappers.actionTask import com.lambda.task.tasks.wrappers.taskOrNull import com.lambda.task.tasks.wrappers.then +import com.lambda.task.tasks.wrappers.thenOrNull import com.lambda.util.extension.containerSlots import com.lambda.util.text.buildText import com.lambda.util.text.highlighted @@ -49,7 +50,6 @@ import com.lambda.util.text.literal import net.minecraft.item.Item import net.minecraft.item.ItemStack import net.minecraft.screen.slot.Slot -import net.minecraft.util.math.BlockPos import java.util.Objects.hash data class ShulkerBoxContainer( @@ -82,67 +82,69 @@ data class ShulkerBoxContainer( automated: Automated ) : OpenContainerTask(description), Automated by automated { override fun SafeContext.onStart() { - transfer( - stackSelection { - sortedWith { compareByDescending { it.slot?.index == index } } - isItem(shulkerItem) - withName(itemName) - sortedByBestContentMatch(stacks) - }, - containedIn.select(ContainerSearchScope.Loaded), - HotbarContainer.select() - ).then { result -> - placeContainer( - findSlot( - result.stackSelection.mutate { - sortedByRecentChange() - }, - result.containerSelection - ) - ) + val selection = stackSelection { + isItem(shulkerItem) + withName(itemName) + slotIdentity?.let { sortedBySlotMatch(it) } + ?: sortedWith { compareByDescending { it.slot?.index == index } } + sortedByBestContentMatch(stacks) + } + taskOrNull { + if (containedIn == HotbarContainer) null + else selection.transfer(containedIn.select(ContainerSearchScope.Loaded), HotbarContainer.select()) + }.then { result -> + placeContainer(findSlot(result?.stackSelection ?: selection, HotbarContainer.select())) }.then { pos -> - openContainer(pos).onSuccess { - success(OpenedShulkerBoxContext(pos)) + openContainer(pos).onSuccess { + success(OpenedShulkerBoxContext(it)) } }.start() } } inner class OpenedShulkerBoxContext( - val blockPos: BlockPos, + override val container: PlacedShulkerBoxContainer, ) : OpenedContainerContext { context(automated: Automated) override fun close(): Task<*> { return taskOrNull { - if (!isAccessed) null + if (!container.isAccessed) null else actionTask { player.closeHandledScreen() } - }.then { breakAndCollect(blockPos) } - .then { outcome -> - val pickedUp = outcome.pickups - .asSequence() - .filter { it.stack.item == shulkerItem && it.stack.name.string == itemName } - .firstNotNullOfOrNull { it.slot } + }.then { + breakAndCollect(container.pos) + }.thenOrNull { outcome -> + val ownSlot = slotIdentity ?: return@thenOrNull null + val pickedUp = outcome.pickups + .asSequence() + .filter { it.stack.item == shulkerItem && it.stack.name.string == itemName } + .firstNotNullOfOrNull { it.slot } + val inContainer = pickedUp != null && + slotsAvailable( + pickedUp.select(), + containedIn.select(ContainerSearchScope.Loaded) + ) + if (inContainer && !ownSlot.isEmpty()) return@thenOrNull null + stackSelection { + isItem(shulkerItem) + withName(itemName) + if (pickedUp != null) isSlot(pickedUp) + sortedByBestContentMatch(container.stacks) + sortedByRecentChange() + }.transfer( + ContainerSelection.HOTBAR_AND_INVENTORY, + containedIn.select(ContainerSearchScope.Loaded), stackSelection { - isItem(shulkerItem) - withName(itemName) - if (pickedUp != null) sortedBySlotMatch(pickedUp) - sortedByBestContentMatch(stacks) - sortedByRecentChange() - }.transfer( - ContainerSelection.HOTBAR_AND_INVENTORY, - containedIn.select(ContainerSearchScope.Loaded), - stackSelection { - sortedByMatches { - stackSelection { - predicate { _, slot -> slot?.index == index } - isEmpty() - } + isEmpty() + sortedByMatches { + stackSelection { + isSlot(ownSlot) } - }, - allowQuickMove = false - ) - } + } + }, + allowQuickMove = false + ) + } } } diff --git a/src/main/kotlin/com/lambda/interaction/container/selection/StackSelection.kt b/src/main/kotlin/com/lambda/interaction/container/selection/StackSelection.kt index dc987933a..f8e8ca754 100644 --- a/src/main/kotlin/com/lambda/interaction/container/selection/StackSelection.kt +++ b/src/main/kotlin/com/lambda/interaction/container/selection/StackSelection.kt @@ -41,6 +41,7 @@ import net.minecraft.registry.RegistryKey import net.minecraft.registry.tag.TagKey import net.minecraft.screen.slot.Slot import java.util.* +import kotlin.math.min import kotlin.time.Instant @ContainerDslMarker @@ -49,6 +50,9 @@ fun Item.select(count: Int = 1) = stackSelection(count) { isItem(this@select) } @ContainerDslMarker fun ItemStack.select(count: Int = 1) = stackSelection(count) { isItemStack(this@select) } +@ContainerDslMarker +fun SlotIdentity.select(count: Int = resolve()?.stack?.count ?: 1) = stackSelection(count) { isSlot(this@select) } + fun stackSelection( count: Int = 1, builder: StackSelectionBuilder.() -> Unit = {} @@ -68,10 +72,10 @@ class StackSelection @ContainerDslMarker internal constructor( val shulkerBoxScope: ShulkerBoxScope = ShulkerBoxScope.Both, val countEmptyAsOne: Boolean = false, val alwaysWholeStacks: Boolean = false, - val comparator: Comparator> = compareBy { it.stack.count }, + val comparator: Comparator>? = null, val selector: (ItemStack, Slot?) -> Boolean, ) { - val optimalStack = itemStack ?: item?.let { ItemStack(it, count) } + val optimalStack = itemStack ?: item?.let { ItemStack(it, it.maxCount) } val isUnbounded: Boolean get() = count < 0 @@ -127,7 +131,7 @@ class StackSelection @ContainerDslMarker internal constructor( .asSequence() .filter(::matches) .let { - if (sorted) + if (sorted && comparator != null) it.map { StackAndSlot(it, null) } .sortedWith(comparator) .map { it.stack } @@ -149,7 +153,7 @@ class StackSelection @ContainerDslMarker internal constructor( .filter { it.identity() !in blacklistedSlots } .filter(::matches) .let { - if (sorted) + if (sorted && comparator != null) it.map { StackAndSlot(it.stack, it) } .sortedWith(comparator) .map { it.slot } @@ -171,7 +175,7 @@ class StackSelection @ContainerDslMarker internal constructor( .filter { it.slot?.identity() !in blacklistedSlots } .filter(::matches) .let { - if (sorted) it.sortedWith(comparator) + if (sorted && comparator != null) it.sortedWith(comparator) else it } .toList() @@ -206,7 +210,13 @@ class StackSelection @ContainerDslMarker internal constructor( @ContainerDslMarker fun emptySpace(slots: Iterable) = - slots.count { it.stack.isEmpty && canInsert(it) } + slots.sumOf { slot -> + if (slot.stack.isEmpty && canInsert(slot)) { + optimalStack?.let { + min(it.maxCount, slot.maxItemCount) + } ?: slot.maxItemCount.coerceAtMost(64) + } else 0 + } companion object { const val UNBOUNDED = -1 @@ -477,6 +487,26 @@ class StackSelectionBuilder @ContainerDslMarker internal constructor( optimal?.let { cannotInsert(it) } } + fun isSlot(slot: SlotIdentity) { + whitelistedSlots.add(slot) + appendSelector { _, s -> s?.identity() == slot } + } + + fun notSlot(slot: SlotIdentity) { + blacklistedSlots.add(slot) + appendSelector { _, s -> s?.identity() != slot } + } + + fun ofAnySlots(vararg slots: SlotIdentity) { + whitelistedSlots.addAll(slots) + appendSelector { _, slot -> slot?.identity() in slots } + } + + fun noneOfSlots(vararg slots: SlotIdentity) { + blacklistedSlots.addAll(slots) + appendSelector { _, slot -> slot?.identity() !in slots } + } + fun sortedWith(newComparator: Comparator>) { comparator = comparator?.thenComparing(newComparator) ?: newComparator } @@ -605,7 +635,7 @@ class StackSelectionBuilder @ContainerDslMarker internal constructor( shulkerBoxScope, countEmptyAsOne, alwaysWholeStacks, - comparator ?: compareBy { it.stack.count }, + comparator, selector ) diff --git a/src/main/kotlin/com/lambda/interaction/handler/handlers/ContainerHandler.kt b/src/main/kotlin/com/lambda/interaction/handler/handlers/ContainerHandler.kt index 2e45dfb9e..a55af676a 100644 --- a/src/main/kotlin/com/lambda/interaction/handler/handlers/ContainerHandler.kt +++ b/src/main/kotlin/com/lambda/interaction/handler/handlers/ContainerHandler.kt @@ -44,7 +44,6 @@ import com.lambda.interaction.container.containers.external.ChestContainer import com.lambda.interaction.container.containers.external.DoubleChestContainer import com.lambda.interaction.container.containers.external.EnderChestContainer import com.lambda.interaction.container.containers.external.PlacedShulkerBoxContainer -import com.lambda.interaction.container.containers.external.ShulkerBoxContainer import com.lambda.interaction.container.selection.ContainerSelection import com.lambda.interaction.container.selection.StackSelection import com.lambda.interaction.container.selection.containerSelection @@ -55,8 +54,6 @@ import com.lambda.util.BlockUtils.blockEntity import com.lambda.util.ReflectionUtils.getInstances import com.lambda.util.extension.containerStacks import com.lambda.util.item.ItemStackUtils.count -import com.lambda.util.item.ItemStackUtils.shulkerBoxStacks -import com.lambda.util.item.ItemUtils.SHULKER_BOXES import com.lambda.util.player.SlotUtils.typeSafe import com.lambda.util.world.Dimension import net.minecraft.block.ChestBlock @@ -130,7 +127,7 @@ object ContainerHandler : Loadable { pendingInteractedBlockEntity = null } - listen { onContainerUpdate() } + listen(priority = { Int.MAX_VALUE }) { onContainerUpdate() } listen { onContainerUpdate() } listen { event -> @@ -407,23 +404,6 @@ object ContainerHandler : Loadable { } } } - - private fun Container.scanContainerContents() { - slots.forEach { slot -> - val stack = slot.stack - val index = slot.index - if (stack.item in SHULKER_BOXES) { - storedContainers[index] = - ShulkerBoxContainer( - stack.name.string, - stack.item, - stack.shulkerBoxStacks, - this, - index - ).also { it.scanContainerContents() } - } else storedContainers.remove(index) - } - } } @ContainerDslMarker diff --git a/src/main/kotlin/com/lambda/interaction/handler/handlers/breaking/RebreakHandler.kt b/src/main/kotlin/com/lambda/interaction/handler/handlers/breaking/RebreakHandler.kt index 8b0e78842..0c22c97f7 100644 --- a/src/main/kotlin/com/lambda/interaction/handler/handlers/breaking/RebreakHandler.kt +++ b/src/main/kotlin/com/lambda/interaction/handler/handlers/breaking/RebreakHandler.kt @@ -27,7 +27,6 @@ import com.lambda.interaction.construction.simulation.context.BreakContext import com.lambda.interaction.handler.handlers.PacketLimitHandler import com.lambda.interaction.handler.handlers.PacketType import com.lambda.interaction.handler.handlers.breaking.BrokenBlockHandler.destroyBlock -import com.lambda.interaction.handler.handlers.breaking.RebreakHandler.reBreak import com.lambda.interaction.manager.managers.breaking.BreakInfo import com.lambda.interaction.manager.managers.breaking.BreakManager import com.lambda.interaction.manager.managers.breaking.BreakManager.calcBreakDelta diff --git a/src/main/kotlin/com/lambda/interaction/manager/managers/hotbar/HotbarManager.kt b/src/main/kotlin/com/lambda/interaction/manager/managers/hotbar/HotbarManager.kt index bd536ccc9..89d2a8dfd 100644 --- a/src/main/kotlin/com/lambda/interaction/manager/managers/hotbar/HotbarManager.kt +++ b/src/main/kotlin/com/lambda/interaction/manager/managers/hotbar/HotbarManager.kt @@ -20,11 +20,11 @@ package com.lambda.interaction.manager.managers.hotbar import com.lambda.config.blocks.HotbarConfig.SwapMode import com.lambda.context.AutomatedSafeContext import com.lambda.context.SafeContext +import com.lambda.event.Event import com.lambda.event.events.TickEvent import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.interaction.manager.Manager import com.lambda.interaction.manager.managers.hotbar.HotbarManager.activeRequest -import com.lambda.interaction.manager.managers.hotbar.HotbarManager.activeSlot import com.lambda.interaction.manager.managers.hotbar.HotbarManager.setActiveRequest import com.lambda.interaction.manager.managers.hotbar.HotbarManager.setActiveSlot import com.lambda.threading.runSafe @@ -41,6 +41,7 @@ import net.minecraft.item.ItemStack object HotbarManager : Manager(1) { private var activeRequest: HotbarRequest? = null @JvmStatic var activeSlot: Int = -1 + private var activeRequestStage: Event? = null val serverSlot get() = runSafe { interaction.lastSelectedSlot @@ -112,6 +113,7 @@ object HotbarManager : Manager(1) { private fun AutomatedSafeContext.setActiveRequest(request: HotbarRequest) { maxSwapsThisTick = hotbarConfig.swapsPerTick activeRequest = request + activeRequestStage = tickStage } /** @@ -148,6 +150,7 @@ object HotbarManager : Manager(1) { private fun SafeContext.checkResetSwap() { activeRequest?.let { active -> if (active.keepTicks <= 0) { + if (active.keepTicks == 0 && tickStage != activeRequestStage) return if (active.hotbarConfig.swapMode == SwapMode.Permanent) { activeRequest = null activeSlot = -1 diff --git a/src/main/kotlin/com/lambda/interaction/manager/managers/interacting/InteractManager.kt b/src/main/kotlin/com/lambda/interaction/manager/managers/interacting/InteractManager.kt index f372930f7..c38aee6e2 100644 --- a/src/main/kotlin/com/lambda/interaction/manager/managers/interacting/InteractManager.kt +++ b/src/main/kotlin/com/lambda/interaction/manager/managers/interacting/InteractManager.kt @@ -41,7 +41,6 @@ import com.lambda.interaction.manager.managers.interacting.InteractManager.activ import com.lambda.interaction.manager.managers.interacting.InteractManager.maxInteractionsThisTick import com.lambda.interaction.manager.managers.interacting.InteractManager.populateFrom import com.lambda.interaction.manager.managers.interacting.InteractManager.potentialInteractions -import com.lambda.interaction.manager.managers.interacting.InteractManager.processRequest import com.lambda.interaction.manager.managers.inventory.inventoryRequest import com.lambda.module.modules.world.AutoSign import com.lambda.threading.runConcurrent @@ -71,6 +70,7 @@ import net.minecraft.util.Hand import net.minecraft.util.hit.BlockHitResult import net.minecraft.util.math.BlockPos import net.minecraft.world.GameMode +import kotlin.time.Duration.Companion.milliseconds object InteractManager : Manager(0), PositionBlocking { private var activeRequest: InteractRequest? = null @@ -215,7 +215,7 @@ object InteractManager : Manager(0), PositionBlocking { if (ctx.preProcessingInfo.placing && expectedState.block is AbstractSignBlock) { cancellingSignScreens++ runConcurrent { - delay(AutoSign.signWriteDelay) + delay(AutoSign.signWriteDelay.milliseconds) runSafeGameScheduled { connection.sendPacket { UpdateSignC2SPacket(ctx.blockPos, true, "", "", "", "") diff --git a/src/main/kotlin/com/lambda/interaction/manager/managers/inventory/InventoryManager.kt b/src/main/kotlin/com/lambda/interaction/manager/managers/inventory/InventoryManager.kt index 010151b7e..92b654f8c 100644 --- a/src/main/kotlin/com/lambda/interaction/manager/managers/inventory/InventoryManager.kt +++ b/src/main/kotlin/com/lambda/interaction/manager/managers/inventory/InventoryManager.kt @@ -33,7 +33,6 @@ import com.lambda.interaction.manager.managers.inventory.InventoryManager.action import com.lambda.interaction.manager.managers.inventory.InventoryManager.activeRequest import com.lambda.interaction.manager.managers.inventory.InventoryManager.filter import com.lambda.interaction.manager.managers.inventory.InventoryManager.indexInventoryChanges -import com.lambda.interaction.manager.managers.inventory.InventoryManager.onInventoryUpdate import com.lambda.module.modules.client.Client import com.lambda.module.modules.client.Client.verboseDebug import com.lambda.threading.runSafe diff --git a/src/main/kotlin/com/lambda/interaction/manager/managers/rotating/RotationManager.kt b/src/main/kotlin/com/lambda/interaction/manager/managers/rotating/RotationManager.kt index 4d40683d3..e091937e8 100644 --- a/src/main/kotlin/com/lambda/interaction/manager/managers/rotating/RotationManager.kt +++ b/src/main/kotlin/com/lambda/interaction/manager/managers/rotating/RotationManager.kt @@ -28,8 +28,6 @@ import com.lambda.event.listener.SafeListener.Companion.listen import com.lambda.interaction.manager.Manager import com.lambda.interaction.manager.managers.rotating.Rotation.Companion.slerpPitch import com.lambda.interaction.manager.managers.rotating.Rotation.Companion.slerpYaw -import com.lambda.interaction.manager.managers.rotating.RotationManager.activeRotation -import com.lambda.interaction.manager.managers.rotating.RotationManager.serverRotation import com.lambda.interaction.manager.managers.rotating.RotationManager.updateActiveRotation import com.lambda.threading.runSafe import com.lambda.util.extension.rotation diff --git a/src/main/kotlin/com/lambda/module/modules/player/InventoryTweaks.kt b/src/main/kotlin/com/lambda/module/modules/player/InventoryTweaks.kt index 9dadac63c..2bc4629ab 100644 --- a/src/main/kotlin/com/lambda/module/modules/player/InventoryTweaks.kt +++ b/src/main/kotlin/com/lambda/module/modules/player/InventoryTweaks.kt @@ -27,6 +27,7 @@ import com.lambda.interaction.container.ContainerType import com.lambda.interaction.container.NestedContainer import com.lambda.interaction.container.OpenedContainerContext import com.lambda.interaction.container.containers.external.EnderChestContainer +import com.lambda.interaction.container.identity import com.lambda.interaction.container.selection.containerSelection import com.lambda.interaction.handler.handlers.ContainerHandler.findContainer import com.lambda.interaction.handler.handlers.ContainerSearchScope @@ -78,7 +79,7 @@ object InventoryTweaks : Module( containerSelection(ContainerSearchScope.Accessed) { ofAnyType(ContainerType.ShulkerBox) predicate { container -> - container is NestedContainer && container.index == slot.index + container is NestedContainer && container.slotIdentity == slot.identity() } } ) ?: return@listen diff --git a/src/main/kotlin/com/lambda/task/tasks/CollectDropsTask.kt b/src/main/kotlin/com/lambda/task/tasks/CollectDropsTask.kt index cbdb85d3a..aa31addfb 100644 --- a/src/main/kotlin/com/lambda/task/tasks/CollectDropsTask.kt +++ b/src/main/kotlin/com/lambda/task/tasks/CollectDropsTask.kt @@ -67,6 +67,7 @@ class CollectDropsTask @Ta5kBuilder internal constructor( private val packets = ConcurrentHashMap() private val lost = mutableMapOf() private var selfId = -1 + private var idleTicks = 0 private var lastGoal: BlockPos? = null init { @@ -105,6 +106,7 @@ class CollectDropsTask @Ta5kBuilder internal constructor( if (gone.isNotEmpty()) { drops.removeAll(gone.toSet()) BaritoneHandler.cancel() + lastGoal = null } val target = drops.firstOrNull() @@ -141,8 +143,10 @@ class CollectDropsTask @Ta5kBuilder internal constructor( return } - if (target.blockPos != lastGoal) { + idleTicks = if (BaritoneHandler.isActive) 0 else idleTicks + 1 + if (target.blockPos != lastGoal || idleTicks >= REPATH_TICKS) { lastGoal = target.blockPos + idleTicks = 0 BaritoneHandler.setGoalAndPath(GoalBlock(target.blockPos)) } } @@ -230,5 +234,6 @@ class CollectDropsTask @Ta5kBuilder internal constructor( private companion object { const val CLOSE_GRACE_TICKS = 20 + const val REPATH_TICKS = 20 } } diff --git a/src/main/kotlin/com/lambda/task/tasks/ContainerTransferTask.kt b/src/main/kotlin/com/lambda/task/tasks/ContainerTransferTask.kt index 4590d71fd..bf9c3d13d 100644 --- a/src/main/kotlin/com/lambda/task/tasks/ContainerTransferTask.kt +++ b/src/main/kotlin/com/lambda/task/tasks/ContainerTransferTask.kt @@ -33,7 +33,6 @@ import com.lambda.interaction.container.selection.ContainerSelection import com.lambda.interaction.container.selection.StackSelection import com.lambda.interaction.container.selection.mutate import com.lambda.interaction.container.selection.selectContainers -import com.lambda.interaction.container.selection.stackSelection import com.lambda.interaction.handler.handlers.ContainerHandler.findContainers import com.lambda.interaction.handler.handlers.PacketLimitHandler.availablePackets import com.lambda.interaction.handler.handlers.PacketType @@ -166,6 +165,7 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( return } val to = toQueue.firstOrNull { !it.haveMatchingInventories(from) } + ?: toQueue.firstOrNull { it !is ExternalContainer } ?: run { fail("No destination container has space left for $fromStack ($progressText, ${describeState()})") return @@ -224,22 +224,23 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( } return taskOrNull { from.access() }.then { fromContext -> + val fromContainer = fromContext?.container ?: from taskOrNull { to.access() }.then { toContext -> MoveTask( - fromContainers = listOf(from), - toContainers = toContainers, + fromContainers = listOf(fromContainer), + toContainers = toContext?.container?.let { listOf(it) } ?: toContainers, fromSelection = fromStack, toSelection = toStack, limit = remaining ).thenOrNull { toContainers - .filter { !canReceive(it) } + .filter { !canReceive(toContext?.container ?: it) } .forEach { full -> toQueue.removeAll { it.haveMatchingInventories(full) } } toContext?.close() }.thenOrNull { - if (from.count(fromStack) <= 0) { + if (fromContainer.count(fromStack) <= 0) { fromQueue.removeAll { it.haveMatchingInventories(from) } } fromContext?.close() @@ -251,8 +252,9 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( private fun tripChain(from: Container): Task<*> = taskOrNull { from.access() } .then { fromContext -> + val fromContainer = fromContext?.container ?: from MoveTask( - fromContainers = listOf(from), + fromContainers = listOf(fromContainer), toContainers = stagingContainers, fromSelection = fromStack, toSelection = StackSelection.EVERYTHING, @@ -261,7 +263,7 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( trackResult = false ).thenOrNull { pulled -> staged += pulled - if (from.count(fromStack) <= 0) { + if (fromContainer.count(fromStack) <= 0) { fromQueue.removeAll { it.haveMatchingInventories(from) } } fromContext?.close() @@ -276,15 +278,16 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( return taskOrNull { to.access() } .then { toContext -> + val toContainer = toContext?.container ?: to MoveTask( fromContainers = stagingContainers, - toContainers = listOf(to), + toContainers = listOf(toContainer), fromSelection = fromStack, toSelection = toStack, limit = pushLimit ).thenOrNull { pushed -> staged = (staged - pushed).coerceAtLeast(0) - if (pushed <= 0 || !canReceive(to)) { + if (pushed <= 0 || !canReceive(toContainer)) { toQueue.removeAll { it.haveMatchingInventories(to) } } toContext?.close() @@ -368,11 +371,12 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( val fromSlot = fromContainer.findSlot(currentSelection) ?: return@firstNotNullOfOrNull null toContainers.firstNotNullOfOrNull { toContainer -> - if (toContainer.haveMatchingInventories(fromContainer)) return@firstNotNullOfOrNull null if (!fromContainer.canSwapWith(toContainer)) return@firstNotNullOfOrNull null - findToSlot(fromSlot, toContainer)?.let { toSlot -> - SlotPair(fromContainer, fromSlot, toContainer, toSlot) - } + findToSlot(fromSlot, toContainer) + ?.takeIf { !toContainer.haveMatchingInventories(fromContainer) || it.index != fromSlot.index } + ?.let { toSlot -> + SlotPair(fromContainer, fromSlot, toContainer, toSlot) + } } } @@ -391,17 +395,13 @@ class ContainerTransferTask @Ta5kBuilder internal constructor( private fun AutomatedSafeContext.findToSlot(fromSlot: Slot, toContainer: Container): Slot? { val stack = fromSlot.stack return toContainer.findSlot( - stackSelection { - predicate { candidate, _ -> !candidate.isEmpty && candidate.hasSpace && candidate.sameItemAs(stack) } + baseToSelection.mutate { + predicate { candidate, slot -> slot !== fromSlot && !candidate.isEmpty && candidate.hasSpace && candidate.sameItemAs(stack) } canInsert(stack) - sortedWith(baseToSelection.comparator) } ) ?: toContainer.findReplaceSlot( stack, - stackSelection(StackSelection.UNBOUNDED) { - isEmpty() - sortedWith(baseToSelection.comparator) - } + baseToSelection.mutate { isEmpty() } ) ?: findSwappableSlot(fromSlot, toContainer) } diff --git a/src/main/kotlin/com/lambda/task/tasks/OpenContainerTask.kt b/src/main/kotlin/com/lambda/task/tasks/OpenContainerTask.kt index becafe1fb..a88618e6b 100644 --- a/src/main/kotlin/com/lambda/task/tasks/OpenContainerTask.kt +++ b/src/main/kotlin/com/lambda/task/tasks/OpenContainerTask.kt @@ -22,7 +22,9 @@ import com.lambda.context.Automated import com.lambda.event.events.InventoryEvent import com.lambda.event.events.TickEvent import com.lambda.event.listener.SafeListener.Companion.listen +import com.lambda.interaction.container.PlacedContainer import com.lambda.interaction.handler.handlers.BaritoneHandler +import com.lambda.interaction.handler.handlers.ContainerHandler.accessedPlacedContainer import com.lambda.interaction.manager.managers.rotating.rotationRequest import com.lambda.task.Task import com.lambda.task.Task.Ta5kBuilder @@ -39,16 +41,27 @@ import net.minecraft.util.math.Direction context(automated: Automated) fun openContainer( blockPos: BlockPos, - waitForSlotLoad: Boolean = true, sides: Set = Direction.entries.toSet() -) = OpenContainerTask(blockPos, waitForSlotLoad, sides, automated) +) = OpenContainerTask(blockPos, sides, automated) { it } -class OpenContainerTask @Ta5kBuilder internal constructor( +@Ta5kBuilder +@JvmName("openTypedContainer") +context(automated: Automated) +inline fun openContainer( + blockPos: BlockPos, + sides: Set = Direction.entries.toSet() +) = OpenContainerTask(blockPos, sides, automated) { + accessedPlacedContainer?.takeIf { blockPos in it.positions } as? T +} + +// TODO: Open containers found in the world (built with empty stacks until they're opened) instead of a position and a +// result callback, once PlacedContainer is generic over an enum of types +class OpenContainerTask @Ta5kBuilder @PublishedApi internal constructor( private val blockPos: BlockPos, - private val waitForSlotLoad: Boolean = true, - private val sides: Set = Direction.entries.toSet(), - automated: Automated -) : Task(), Automated by automated { + private val sides: Set, + automated: Automated, + private val result: (ScreenHandler) -> R? +) : Task(), Automated by automated { override val name get() = "${containerState.description()} at ${blockPos.toShortString()}" private var screenHandler: ScreenHandler? = null @@ -79,8 +92,6 @@ class OpenContainerTask @Ta5kBuilder internal constructor( screenHandler = it.screenHandler containerState = State.SlotLoading - - if (!waitForSlotLoad) success(it.screenHandler) } listen { @@ -93,8 +104,9 @@ class OpenContainerTask @Ta5kBuilder internal constructor( listen { if (containerState != State.SlotLoading) return@listen - screenHandler?.let { - success(it) + screenHandler?.let { handler -> + result(handler)?.let { success(it) } + ?: failure("No matching container was indexed at ${blockPos.toShortString()}") } } diff --git a/src/main/kotlin/com/lambda/util/world/WorldUtils.kt b/src/main/kotlin/com/lambda/util/world/WorldUtils.kt index a13fa4684..239d2d0c9 100644 --- a/src/main/kotlin/com/lambda/util/world/WorldUtils.kt +++ b/src/main/kotlin/com/lambda/util/world/WorldUtils.kt @@ -20,8 +20,6 @@ package com.lambda.util.world import com.lambda.context.SafeContext import com.lambda.util.extension.getBlockState import com.lambda.util.extension.getFluidState -import com.lambda.util.world.WorldUtils.internalGetEntities -import com.lambda.util.world.WorldUtils.internalGetFastEntities import net.minecraft.block.BlockState import net.minecraft.block.entity.BlockEntity import net.minecraft.entity.Entity