Skip to content

Commit 705e8d9

Browse files
committed
swap old system for inventory manager requests
1 parent 557c3af commit 705e8d9

26 files changed

+91
-578
lines changed

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.lambda.context.Automated
2222
import com.lambda.context.SafeContext
2323
import com.lambda.interaction.material.StackSelection
2424
import com.lambda.interaction.material.container.MaterialContainer
25-
import com.lambda.interaction.material.transfer.TransactionExecutor
25+
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
2626
import com.lambda.task.Task
2727
import com.lambda.util.item.ItemStackUtils.equal
2828
import com.lambda.util.player.gamemode
@@ -41,7 +41,7 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
4141
override fun spaceAvailable(selection: StackSelection): Int =
4242
if (mc.player?.isCreative == true && selection.optimalStack != null) Int.MAX_VALUE else 0
4343

44-
class CreativeDeposit @Ta5kBuilder constructor(val selection: StackSelection) : Task<Unit>() {
44+
class CreativeDeposit @Ta5kBuilder constructor(val selection: StackSelection, automated: Automated) : Task<Unit>(), Automated by automated {
4545
override val name: String get() = "Removing $selection from creative inventory"
4646

4747
override fun SafeContext.onStart() {
@@ -50,22 +50,21 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
5050
throw NotInCreativeModeException()
5151
}
5252

53-
TransactionExecutor.transfer(player.currentScreenHandler) {
53+
inventoryRequest {
5454
player.currentScreenHandler?.slots?.let { slots ->
5555
selection.filterSlots(slots).forEach {
5656
clickCreativeStack(ItemStack.EMPTY, it.id)
5757
}
5858
}
59-
}.finally {
60-
success()
61-
}.execute(this@CreativeDeposit)
59+
onComplete { success() }
60+
}.submit(queueIfClosed = false)
6261
}
6362
}
6463

6564
context(automated: Automated)
66-
override fun deposit(selection: StackSelection) = CreativeDeposit(selection)
65+
override fun deposit(selection: StackSelection) = CreativeDeposit(selection, automated)
6766

68-
class CreativeWithdrawal @Ta5kBuilder constructor(val selection: StackSelection) : Task<Unit>() {
67+
class CreativeWithdrawal @Ta5kBuilder constructor(val selection: StackSelection, automated: Automated) : Task<Unit>(), Automated by automated {
6968
override val name: String get() = "Withdrawing $selection from creative inventory"
7069

7170
override fun SafeContext.onStart() {
@@ -77,11 +76,11 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
7776
throw NotInCreativeModeException()
7877
}
7978

80-
TransactionExecutor.transfer(player.currentScreenHandler) {
79+
inventoryRequest {
8180
clickCreativeStack(optimalStack, 36 + player.inventory.selectedSlot)
82-
}.finally {
83-
success()
84-
}.execute(this@CreativeWithdrawal)
81+
player.inventory.selectedStack = optimalStack
82+
onComplete { success() }
83+
}.submit(queueIfClosed = false)
8584
return
8685
}
8786

@@ -91,7 +90,7 @@ data object CreativeContainer : MaterialContainer(Rank.Creative) {
9190

9291
// Withdraws items from the creative menu to the player's main hand
9392
context(automated: Automated)
94-
override fun withdraw(selection: StackSelection) = CreativeWithdrawal(selection)
93+
override fun withdraw(selection: StackSelection) = CreativeWithdrawal(selection, automated)
9594

9695
class NotInCreativeModeException : IllegalStateException("Insufficient permission: not in creative mode")
9796
class NoOptimalStackException : IllegalStateException("Cannot move item: no optimal stack")

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import com.lambda.context.SafeContext
2323
import com.lambda.interaction.material.ContainerTask
2424
import com.lambda.interaction.material.StackSelection
2525
import com.lambda.interaction.material.container.MaterialContainer
26-
import com.lambda.interaction.material.transfer.TransactionExecutor.Companion.transfer
26+
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
2727
import com.lambda.util.item.ItemStackUtils.equal
2828
import com.lambda.util.player.SlotUtils.combined
29-
import com.lambda.util.player.SlotUtils.hotbar
3029
import com.lambda.util.player.SlotUtils.storage
3130
import com.lambda.util.text.buildText
3231
import com.lambda.util.text.literal
@@ -40,7 +39,11 @@ object MainHandContainer : MaterialContainer(Rank.MainHand) {
4039

4140
override val description = buildText { literal("MainHand") }
4241

43-
class HandDeposit @Ta5kBuilder constructor(val selection: StackSelection, val hand: Hand) : ContainerTask() {
42+
class HandDeposit @Ta5kBuilder constructor(
43+
val selection: StackSelection,
44+
val hand: Hand,
45+
automated: Automated
46+
) : ContainerTask(), Automated by automated {
4447
override val name: String get() = "Depositing [$selection] to ${hand.name.lowercase().replace("_", " ")}"
4548

4649
override fun SafeContext.onStart() {
@@ -55,26 +58,24 @@ object MainHandContainer : MaterialContainer(Rank.MainHand) {
5558
return
5659
}
5760

58-
transfer(player.currentScreenHandler) {
61+
inventoryRequest {
5962
val stackInOffHand = moveStack.equal(player.offHandStack)
60-
if (hand == Hand.MAIN_HAND && stackInOffHand) {
63+
val stackInMainHand = moveStack.equal(player.mainHandStack)
64+
if ((hand == Hand.MAIN_HAND && stackInOffHand) || (hand == Hand.OFF_HAND && stackInMainHand)) {
6165
swapHands()
62-
return@transfer
66+
return@inventoryRequest
6367
}
6468

65-
when (moveStack) {
66-
in player.hotbar -> swapToHotbarSlot(player.hotbar.indexOf(moveStack))
67-
// ToDo: Use pickFromInventory
68-
in player.storage -> swap(player.combined.indexOf(moveStack), 0)
69-
}
69+
if (moveStack in player.storage) swap(player.combined.indexOf(moveStack), 0)
70+
else throw NotInInventoryException()
7071

71-
if (hand == Hand.OFF_HAND) swapHands()
72-
}.finally {
73-
success()
74-
}.execute(this@HandDeposit)
72+
onComplete { success() }
73+
}.submit(queueIfClosed = false)
7574
}
7675
}
7776

7877
context(automated: Automated)
79-
override fun deposit(selection: StackSelection) = HandDeposit(selection, Hand.MAIN_HAND)
78+
override fun deposit(selection: StackSelection) = HandDeposit(selection, Hand.MAIN_HAND, automated)
79+
80+
class NotInInventoryException : IllegalStateException("Cannot find stack in inventory")
8081
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ object OffHandContainer : MaterialContainer(Rank.OffHand) {
3434
override val description = buildText { literal("OffHand") }
3535

3636
context(automated: Automated)
37-
override fun deposit(selection: StackSelection) = MainHandContainer.HandDeposit(selection, Hand.OFF_HAND)
37+
override fun deposit(selection: StackSelection) = MainHandContainer.HandDeposit(selection, Hand.OFF_HAND, automated)
3838
}

src/main/kotlin/com/lambda/interaction/material/transfer/InventoryTransaction.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/main/kotlin/com/lambda/interaction/material/transfer/SlotTransfer.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.lambda.context.SafeContext
2222
import com.lambda.event.events.TickEvent
2323
import com.lambda.event.listener.SafeListener.Companion.listen
2424
import com.lambda.interaction.material.StackSelection
25-
import com.lambda.interaction.material.transfer.TransactionExecutor.Companion.transfer
25+
import com.lambda.interaction.request.inventory.InventoryRequest.Companion.inventoryRequest
2626
import com.lambda.task.Task
2727
import com.lambda.util.extension.containerSlots
2828
import com.lambda.util.extension.inventorySlots
@@ -70,13 +70,12 @@ class SlotTransfer @Ta5kBuilder constructor(
7070
val nextFrom = selectedFrom.firstOrNull() ?: return@listen
7171
val nextTo = selectedTo.firstOrNull() ?: return@listen
7272

73-
transfer(screen) {
73+
inventoryRequest {
7474
// moveSlot(nextFrom.id, nextTo.id)
7575
swap(nextTo.id, 1)
7676
swap(nextFrom.id, 1)
77-
}.finally { change ->
78-
changes merge change
79-
}.execute(this@SlotTransfer)
77+
onComplete { success() }
78+
}.submit(queueIfClosed = false)
8079
}
8180
}
8281

src/main/kotlin/com/lambda/interaction/material/transfer/TransactionExecutor.kt

Lines changed: 0 additions & 155 deletions
This file was deleted.

src/main/kotlin/com/lambda/interaction/material/transfer/TransferSelection.kt

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)