Skip to content

Commit 076a785

Browse files
committed
Improve inventory transaction speed and accuracy
1 parent 3eb57a7 commit 076a785

File tree

11 files changed

+48
-57
lines changed

11 files changed

+48
-57
lines changed

common/src/main/kotlin/com/lambda/config/groups/InventoryConfig.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ interface InventoryConfig {
2525
val disposables: Set<Block>
2626
val accessEnderChest: Boolean
2727

28-
val actionTimout: Int
2928
val swapWithDisposables: Boolean
3029

3130
val providerPriority: Priority

common/src/main/kotlin/com/lambda/config/groups/InventorySettings.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class InventorySettings(
2626
) : InventoryConfig {
2727
override val disposables by c.setting("Disposables", ItemUtils.defaultDisposables, "Items that will be ignored when checking for a free slot", vis)
2828
override val accessEnderChest by c.setting("Access Ender Chest", false, "Allow access to the player's ender chest", vis)
29-
override val actionTimout by c.setting("Action Timeout", 10, 0..100, 1, "How long to wait for after each inventory action", " ticks", vis)
3029
override val swapWithDisposables by c.setting("Swap With Disposables", true, "Swap items with disposable ones", vis)
3130
override val providerPriority by c.setting("Provider Priority", InventoryConfig.Priority.WithMinItems, "What container to prefer when retrieving the item from", vis)
3231
override val storePriority by c.setting("Store Priority", InventoryConfig.Priority.WithMinItems, "What container to prefer when storing the item to", vis)

common/src/main/kotlin/com/lambda/interaction/construction/context/PlaceContext.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import com.lambda.interaction.construction.verify.TargetState
2525
import com.lambda.interaction.request.rotation.RotationRequest
2626
import com.lambda.threading.runSafe
2727
import com.lambda.util.BlockUtils
28+
import com.lambda.util.BlockUtils.blockState
2829
import com.lambda.util.Communication.warn
2930
import net.minecraft.block.BlockState
3031
import net.minecraft.util.Hand
@@ -94,7 +95,7 @@ data class PlaceContext(
9495

9596
override fun SafeContext.buildRenderer() {
9697
withState(expectedState, expectedPos, baseColor, DirectionMask.ALL.exclude(result.side.opposite))
97-
withState(expectedState, expectedPos, sideColor, result.side.opposite)
98+
withState(result.blockPos.blockState(world), result.blockPos, sideColor, result.side.opposite)
9899
}
99100

100101
override fun shouldRotate(config: BuildConfig) = config.rotateForPlace

common/src/main/kotlin/com/lambda/interaction/construction/result/BuildResult.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ abstract class BuildResult : ComparableResult<Rank>, Nameable {
235235
val neededStack: ItemStack,
236236
val inventory: InventoryConfig
237237
) : Drawable, Resolvable, BuildResult() {
238-
override val name: String get() = "Wrong stack for $blockPos need $neededStack."
238+
override val name: String get() = "Wrong stack for ${blockPos.toShortString()} need $neededStack."
239239
override val rank = Rank.WRONG_ITEM
240240
private val color = Color(3, 252, 169, 25)
241241

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import com.lambda.task.Task
2222
import com.lambda.threading.runSafe
2323

2424
abstract class InventoryTransaction : Task<InventoryChanges>() {
25-
private lateinit var changes: InventoryChanges
25+
lateinit var changes: InventoryChanges
2626

2727
override fun SafeContext.onStart() {
2828
changes = InventoryChanges(player.currentScreenHandler.slots)

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

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.lambda.interaction.material.transfer
1919

20-
import com.lambda.Lambda.LOG
2120
import com.lambda.config.groups.InventoryConfig
2221
import com.lambda.context.SafeContext
2322
import com.lambda.event.events.TickEvent
@@ -28,6 +27,7 @@ import com.lambda.module.modules.client.TaskFlowModule
2827
import com.lambda.task.Task
2928
import com.lambda.util.extension.containerSlots
3029
import com.lambda.util.extension.inventorySlots
30+
import com.lambda.util.item.ItemUtils.block
3131
import net.minecraft.screen.ScreenHandler
3232
import net.minecraft.screen.slot.Slot
3333

@@ -37,11 +37,10 @@ class SlotTransfer @Ta5kBuilder constructor(
3737
val from: List<Slot>,
3838
val to: List<Slot>,
3939
private val closeScreen: Boolean = true,
40-
private val settings: InventoryConfig = TaskFlowModule.inventory,
40+
private val config: InventoryConfig = TaskFlowModule.inventory,
4141
) : Task<Unit>() {
42-
private var selectedFrom = selection.filterSlots(from)
43-
private var selectedTo =
44-
to.filter { it.stack.isEmpty } // + to.filter { it.stack.item.block in TaskFlowModule.disposables }
42+
private var selectedFrom = listOf<Slot>()
43+
private var selectedTo = listOf<Slot>()
4544
override val name: String
4645
get() = "Moving $selection from slots [${selectedFrom.joinToString { "${it.id}" }}] to slots [${selectedTo.joinToString { "${it.id}" }}] in ${screen::class.simpleName}"
4746

@@ -54,42 +53,31 @@ class SlotTransfer @Ta5kBuilder constructor(
5453

5554
init {
5655
listen<TickEvent.Pre> {
57-
val current = player.currentScreenHandler
58-
if (current != screen) {
59-
failure("Screen has changed. Expected ${screen::class.simpleName} (revision ${screen.revision}) but got ${current::class.simpleName} (revision ${current.revision})")
60-
return@listen
61-
}
62-
6356
if (changes.fulfillsSelection(to, selection)) {
64-
if (closeScreen) player.closeHandledScreen()
57+
if (closeScreen) { player.closeHandledScreen() }
6558
success()
6659
return@listen
6760
}
6861

69-
if (--delay >= 0) return@listen
70-
delay = settings.actionTimout
62+
val current = player.currentScreenHandler
63+
if (current != screen) {
64+
failure("Screen has changed. Expected ${screen::class.simpleName} (revision ${screen.revision}) but got ${current::class.simpleName} (revision ${current.revision})")
65+
return@listen
66+
}
7167

7268
selectedFrom = selection.filterSlots(from)
73-
selectedTo =
74-
to.filter { it.stack.isEmpty } // + to.filter { it.stack.item.block in TaskFlowModule.disposables }
69+
selectedTo = to.filter { it.stack.isEmpty } + to.filter { it.stack.item.block in config.disposables }
7570

7671
val nextFrom = selectedFrom.firstOrNull() ?: return@listen
7772
val nextTo = selectedTo.firstOrNull() ?: return@listen
7873

79-
LOG.info("Changes so far:\n$changes")
80-
8174
transfer {
8275
moveSlot(nextFrom.id, nextTo.id)
76+
// swap(nextFrom.id, 0)
77+
// swap(nextTo.id, 0)
8378
}.finally { change ->
8479
changes merge change
8580
}.execute(this@SlotTransfer)
86-
87-
// if (transfer.fulfillsSelection(selection)) {
88-
// info("Transfer complete")
89-
//// success()
90-
//// if (closeScreen) player.closeHandledScreen()
91-
// return@listen
92-
// }
9381
}
9482
}
9583

common/src/main/kotlin/com/lambda/interaction/material/transfer/transaction/ClickSlotTransaction.kt

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package com.lambda.interaction.material.transfer.transaction
1919

20+
import com.lambda.context.SafeContext
2021
import com.lambda.event.events.TickEvent
2122
import com.lambda.event.listener.SafeListener.Companion.listen
23+
import com.lambda.interaction.material.transfer.InventoryChanges
2224
import com.lambda.interaction.material.transfer.InventoryTransaction
2325
import com.lambda.util.player.SlotUtils.clickSlot
2426
import net.minecraft.screen.slot.SlotActionType
@@ -30,13 +32,10 @@ class ClickSlotTransaction @Ta5kBuilder constructor(
3032
) : InventoryTransaction() {
3133
override val name: String get() = "Click slot #$slotId with action $actionType and button $button"
3234

33-
init {
34-
listen<TickEvent.Pre> {
35-
clickSlot(slotId, button, actionType)
36-
}
37-
38-
listen<TickEvent.Post> {
39-
finish()
40-
}
35+
override fun SafeContext.onStart() {
36+
changes = InventoryChanges(player.currentScreenHandler.slots)
37+
clickSlot(slotId, button, actionType)
38+
changes.detectChanges()
39+
success(changes)
4140
}
4241
}

common/src/main/kotlin/com/lambda/interaction/material/transfer/transaction/DropItemInHandTransaction.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717

1818
package com.lambda.interaction.material.transfer.transaction
1919

20+
import com.lambda.context.SafeContext
2021
import com.lambda.event.events.TickEvent
2122
import com.lambda.event.listener.SafeListener.Companion.listen
23+
import com.lambda.interaction.material.transfer.InventoryChanges
2224
import com.lambda.interaction.material.transfer.InventoryTransaction
2325
import net.minecraft.util.Hand
2426

@@ -27,15 +29,12 @@ class DropItemInHandTransaction @Ta5kBuilder constructor(
2729
) : InventoryTransaction() {
2830
override val name: String get() = "Dropping ${if (entireStack) "stack" else "item"} in hand"
2931

30-
init {
31-
listen<TickEvent.Pre> {
32-
if (!player.isSpectator && player.dropSelectedItem(entireStack)) {
33-
player.swingHand(Hand.MAIN_HAND)
34-
}
35-
}
36-
37-
listen<TickEvent.Post> {
38-
finish()
32+
override fun SafeContext.onStart() {
33+
changes = InventoryChanges(player.currentScreenHandler.slots)
34+
if (!player.isSpectator && player.dropSelectedItem(entireStack)) {
35+
player.swingHand(Hand.MAIN_HAND)
3936
}
37+
changes.detectChanges()
38+
success(changes)
4039
}
4140
}

common/src/main/kotlin/com/lambda/interaction/material/transfer/transaction/PickFromInventoryTransaction.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class PickFromInventoryTransaction @Ta5kBuilder constructor(
3737
}
3838

3939
listen<InventoryEvent.HotbarSlot.Update> {
40+
// ToDo: Check slot
4041
finish()
4142
}
4243
}

common/src/main/kotlin/com/lambda/interaction/material/transfer/transaction/SwapHandsTransaction.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,27 @@ import net.minecraft.util.math.Direction
2727

2828
class SwapHandsTransaction @Ta5kBuilder constructor() : InventoryTransaction() {
2929
override val name: String get() = "Swap Hand Stacks"
30+
private var confirming = false
3031

3132
init {
3233
listen<TickEvent.Pre> {
33-
if (player.isSpectator) return@listen
34+
if (player.isSpectator) {
35+
failure("Spectators cannot swap hands")
36+
return@listen
37+
}
3438
connection.sendPacket(
3539
PlayerActionC2SPacket(
3640
PlayerActionC2SPacket.Action.SWAP_ITEM_WITH_OFFHAND,
3741
BlockPos.ORIGIN,
3842
Direction.DOWN
3943
)
4044
)
45+
confirming = true
4146
}
4247

4348
listen<InventoryEvent.SlotUpdate> {
44-
finish()
49+
// ToDo: Check slot
50+
if (confirming) finish()
4551
}
4652
}
4753
}

0 commit comments

Comments
 (0)