Skip to content

Commit 0c14c1e

Browse files
committed
use tick pre listener over onStart for inventory request actions in tasks in case they cant be completed
1 parent 1848665 commit 0c14c1e

File tree

3 files changed

+62
-53
lines changed

3 files changed

+62
-53
lines changed

src/main/kotlin/com/lambda/interaction/material/container/containers/CreativeContainer.kt

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package com.lambda.interaction.material.container.containers
1919

2020
import com.lambda.Lambda.mc
2121
import com.lambda.context.Automated
22-
import com.lambda.context.SafeContext
22+
import com.lambda.event.events.TickEvent
23+
import com.lambda.event.listener.SafeListener.Companion.listen
2324
import com.lambda.interaction.material.StackSelection
2425
import com.lambda.interaction.material.container.MaterialContainer
2526
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
@@ -44,20 +45,22 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
4445
class CreativeDeposit @Ta5kBuilder constructor(val selection: StackSelection, automated: Automated) : Task<Unit>(), Automated by automated {
4546
override val name: String get() = "Removing $selection from creative inventory"
4647

47-
override fun SafeContext.onStart() {
48-
if (!gamemode.isCreative) {
49-
// ToDo: Maybe switch gamemode?
50-
throw NotInCreativeModeException()
51-
}
48+
init {
49+
listen<TickEvent.Pre> {
50+
if (!gamemode.isCreative) {
51+
// ToDo: Maybe switch gamemode?
52+
throw NotInCreativeModeException()
53+
}
5254

53-
inventoryRequest {
54-
player.currentScreenHandler?.slots?.let { slots ->
55-
selection.filterSlots(slots).forEach {
56-
clickCreativeStack(ItemStack.EMPTY, it.id)
55+
inventoryRequest {
56+
player.currentScreenHandler?.slots?.let { slots ->
57+
selection.filterSlots(slots).forEach {
58+
clickCreativeStack(ItemStack.EMPTY, it.id)
59+
}
5760
}
58-
}
59-
onComplete { success() }
60-
}.submit(queueIfClosed = false)
61+
onComplete { success() }
62+
}.submit(queueIfClosed = false)
63+
}
6164
}
6265
}
6366

@@ -67,24 +70,29 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
6770
class CreativeWithdrawal @Ta5kBuilder constructor(val selection: StackSelection, automated: Automated) : Task<Unit>(), Automated by automated {
6871
override val name: String get() = "Withdrawing $selection from creative inventory"
6972

70-
override fun SafeContext.onStart() {
71-
selection.optimalStack?.let { optimalStack ->
72-
if (player.mainHandStack.equal(optimalStack)) return
73+
init {
74+
listen<TickEvent.Pre> {
75+
selection.optimalStack?.let { optimalStack ->
76+
if (player.mainHandStack.equal(optimalStack)) {
77+
success()
78+
return@listen
79+
}
7380

74-
if (!gamemode.isCreative) {
75-
// ToDo: Maybe switch gamemode?
76-
throw NotInCreativeModeException()
81+
if (!gamemode.isCreative) {
82+
// ToDo: Maybe switch gamemode?
83+
throw NotInCreativeModeException()
84+
}
85+
86+
inventoryRequest {
87+
clickCreativeStack(optimalStack, 36 + player.inventory.selectedSlot)
88+
action { player.inventory.selectedStack = optimalStack }
89+
onComplete { success() }
90+
}.submit(queueIfClosed = false)
91+
return@listen
7792
}
7893

79-
inventoryRequest {
80-
clickCreativeStack(optimalStack, 36 + player.inventory.selectedSlot)
81-
player.inventory.selectedStack = optimalStack
82-
onComplete { success() }
83-
}.submit(queueIfClosed = false)
84-
return
94+
throw NoOptimalStackException()
8595
}
86-
87-
throw NoOptimalStackException()
8896
}
8997
}
9098

src/main/kotlin/com/lambda/interaction/material/container/containers/MainHandContainer.kt

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ package com.lambda.interaction.material.container.containers
1919

2020
import com.lambda.Lambda.mc
2121
import com.lambda.context.Automated
22-
import com.lambda.context.SafeContext
22+
import com.lambda.event.events.TickEvent
23+
import com.lambda.event.listener.SafeListener.Companion.listen
2324
import com.lambda.interaction.material.ContainerTask
2425
import com.lambda.interaction.material.StackSelection
2526
import com.lambda.interaction.material.container.MaterialContainer
@@ -44,33 +45,35 @@ object MainHandContainer : MaterialContainer(Rank.MainHand) {
4445
) : ContainerTask(), Automated by automated {
4546
override val name: String get() = "Depositing [$selection] to ${hand.name.lowercase().replace("_", " ")}"
4647

47-
override fun SafeContext.onStart() {
48-
val moveStack = InventoryContainer.matchingStacks(selection).firstOrNull() ?: run {
49-
failure("No matching stacks found in inventory")
50-
return
51-
}
52-
53-
val handStack = player.getStackInHand(hand)
54-
if (moveStack.equal(handStack)) {
55-
success()
56-
return
57-
}
48+
init {
49+
listen<TickEvent.Pre> {
50+
val moveStack = InventoryContainer.matchingStacks(selection).firstOrNull() ?: run {
51+
failure("No matching stacks found in inventory")
52+
return@listen
53+
}
5854

59-
inventoryRequest {
60-
val stackInOffHand = moveStack.equal(player.offHandStack)
61-
val stackInMainHand = moveStack.equal(player.mainHandStack)
62-
if ((hand == Hand.MAIN_HAND && stackInOffHand) || (hand == Hand.OFF_HAND && stackInMainHand)) {
63-
swapHands()
64-
return@inventoryRequest
55+
val handStack = player.getStackInHand(hand)
56+
if (moveStack.equal(handStack)) {
57+
success()
58+
return@listen
6559
}
6660

67-
val slot = player.currentScreenHandler.slots.first { it.stack == moveStack } ?: throw NotInInventoryException()
68-
swap(slot.id, player.inventory.selectedSlot)
61+
inventoryRequest {
62+
val stackInOffHand = moveStack.equal(player.offHandStack)
63+
val stackInMainHand = moveStack.equal(player.mainHandStack)
64+
if ((hand == Hand.MAIN_HAND && stackInOffHand) || (hand == Hand.OFF_HAND && stackInMainHand)) {
65+
swapHands()
66+
return@inventoryRequest
67+
}
68+
69+
val slot = player.currentScreenHandler.slots.first { it.stack == moveStack } ?: throw NotInInventoryException()
70+
swap(slot.id, player.inventory.selectedSlot)
6971

70-
if (hand == Hand.OFF_HAND) swapHands()
72+
if (hand == Hand.OFF_HAND) swapHands()
7173

72-
onComplete { success() }
73-
}.submit(queueIfClosed = false)
74+
onComplete { success() }
75+
}.submit(queueIfClosed = false)
76+
}
7477
}
7578
}
7679

src/main/kotlin/com/lambda/task/Task.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,7 @@ abstract class Task<Result> : Nameable, Muteable {
128128
unsubscribe()
129129
state = State.Completed
130130
if (!AutomationConfig.showAllEntries) parent?.subTasks?.remove(this)
131-
runSafe {
132-
executeNextTask(result)
133-
}
131+
runSafe { executeNextTask(result) }
134132
}
135133

136134
@Ta5kBuilder

0 commit comments

Comments
 (0)