Skip to content

Commit 9daee66

Browse files
committed
Bug fixes
1 parent 8a6e8fd commit 9daee66

File tree

14 files changed

+129
-71
lines changed

14 files changed

+129
-71
lines changed
Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
package com.lambda.command.commands
22

3-
import com.lambda.brigadier.CommandResult
4-
import com.lambda.brigadier.argument.integer
5-
import com.lambda.brigadier.argument.itemStack
6-
import com.lambda.brigadier.argument.string
7-
import com.lambda.brigadier.argument.value
3+
import com.lambda.brigadier.CommandResult.Companion.failure
4+
import com.lambda.brigadier.CommandResult.Companion.success
5+
import com.lambda.brigadier.argument.*
86
import com.lambda.brigadier.executeWithResult
97
import com.lambda.brigadier.required
108
import com.lambda.command.LambdaCommand
119
import com.lambda.interaction.material.ContainerManager
1210
import com.lambda.interaction.material.ContainerManager.containerMatchSelection
13-
import com.lambda.interaction.material.ContainerManager.findContainerWithSelection
14-
import com.lambda.interaction.material.ContainerManager.transfer
1511
import com.lambda.interaction.material.StackSelection.Companion.selectStack
1612
import com.lambda.interaction.material.transfer.TransferResult
1713
import com.lambda.util.Communication.info
1814
import com.lambda.util.primitives.extension.CommandBuilder
1915

2016
object TransferCommand : LambdaCommand(
2117
name = "transfer",
22-
usage = "transfer <item> <amount> <to>",
23-
description = "Transfer items to a container"
18+
usage = "transfer <move|cancel|undo> <item> <amount> <to>",
19+
description = "Transfer items from anywhere to anywhere",
2420
) {
21+
private var lastTransfer: TransferResult.Success? = null
22+
2523
override fun CommandBuilder.create() {
2624
required(itemStack("stack", registry)) { stack ->
2725
required(integer("amount")) { amount ->
@@ -41,7 +39,8 @@ object TransferCommand : LambdaCommand(
4139
isItem(stack(ctx).value().item)
4240
}
4341
ContainerManager.container().forEach {
44-
builder.suggest("\"${it.name} with space left ${it.spaceLeft(selection)}\"")
42+
val space = it.spaceLeft(selection)
43+
if (space > 0) builder.suggest("\"${it.name} with $space space left\"")
4544
}
4645
builder.buildFuture()
4746
}
@@ -51,31 +50,51 @@ object TransferCommand : LambdaCommand(
5150
}
5251
val fromContainer = ContainerManager.container().find {
5352
it.name == from().value().split(" with ").firstOrNull()
54-
} ?: return@executeWithResult CommandResult.failure("From container not found")
53+
} ?: return@executeWithResult failure("From container not found")
5554

5655
val toContainer = ContainerManager.container().find {
5756
it.name == to().value().split(" with ").firstOrNull()
58-
} ?: return@executeWithResult CommandResult.failure("To container not found")
57+
} ?: return@executeWithResult failure("To container not found")
5958

6059
when (val result = fromContainer.transfer(selection, toContainer)) {
6160
is TransferResult.Success -> {
6261
info("Transferring $selection from ${fromContainer.name} to ${toContainer.name}")
62+
lastTransfer = result
6363
result.solve.start(null)
64-
return@executeWithResult CommandResult.success()
64+
return@executeWithResult success()
6565
}
6666
is TransferResult.MissingItems -> {
67-
return@executeWithResult CommandResult.failure("Missing items: ${result.missing}")
67+
return@executeWithResult failure("Missing items: ${result.missing}")
6868
}
6969
is TransferResult.NoSpace -> {
70-
return@executeWithResult CommandResult.failure("No space in ${toContainer.name}")
70+
return@executeWithResult failure("No space in ${toContainer.name}")
7171
}
7272
}
7373

74-
return@executeWithResult CommandResult.success()
74+
return@executeWithResult success()
7575
}
7676
}
7777
}
7878
}
7979
}
80+
81+
required(literal("cancel")) {
82+
executeWithResult {
83+
lastTransfer?.solve?.cancel() ?: run {
84+
return@executeWithResult failure("No transfer to cancel")
85+
}
86+
lastTransfer = null
87+
success()
88+
}
89+
}
90+
91+
required(literal("undo")) {
92+
executeWithResult {
93+
lastTransfer?.undo ?: run {
94+
return@executeWithResult failure("No transfer to undo")
95+
}
96+
success()
97+
}
98+
}
8099
}
81100
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.lambda.interaction.material.ContainerManager.transfer
88
import com.lambda.interaction.material.StackSelection.Companion.select
99
import com.lambda.interaction.material.StackSelection.Companion.selectStack
1010
import com.lambda.interaction.material.container.MainHandContainer
11-
import com.lambda.task.Task
1211
import com.lambda.task.Task.Companion.emptyTask
1312
import com.lambda.task.tasks.BreakBlock.Companion.breakBlock
1413
import com.lambda.task.tasks.GoalTask.Companion.moveToGoalUntil
@@ -30,7 +29,7 @@ sealed class BreakResult : BuildResult() {
3029
) : Resolvable, BreakResult() {
3130
override val rank = Rank.BREAK_SUCCESS
3231

33-
override val resolve = breakBlock(context)
32+
override val resolve get() = breakBlock(context)
3433

3534
override fun compareTo(other: ComparableResult<Rank>): Int {
3635
return when (other) {
@@ -51,7 +50,7 @@ sealed class BreakResult : BuildResult() {
5150
) : Resolvable, BreakResult() {
5251
override val rank = Rank.BREAK_NOT_EXPOSED
5352

54-
override val resolve = emptyTask()
53+
override val resolve get() = emptyTask("Block is not exposed to air.")
5554

5655
override fun compareTo(other: ComparableResult<Rank>): Int {
5756
return when (other) {
@@ -72,13 +71,13 @@ sealed class BreakResult : BuildResult() {
7271
val badItem: Item
7372
) : Resolvable, BreakResult() {
7473
override val rank = Rank.BREAK_ITEM_CANT_MINE
75-
override val resolve = findBestAvailableTool(blockState)
74+
override val resolve get() = findBestAvailableTool(blockState)
7675
?.select()
7776
?.transfer(MainHandContainer)
7877
?.solve ?: run {
7978
selectStack {
8079
isItem(badItem).not()
81-
}.transfer(MainHandContainer)?.solve ?: emptyTask() // ToDo: Should throw error
80+
}.transfer(MainHandContainer)?.solve ?: emptyTask("No item found or space") // ToDo: Should throw error
8281
}
8382

8483
override fun compareTo(other: ComparableResult<Rank>): Int {
@@ -120,7 +119,7 @@ sealed class BreakResult : BuildResult() {
120119
) : Resolvable, BreakResult() {
121120
override val rank = Rank.BREAK_PLAYER_ON_TOP
122121

123-
override val resolve =
122+
override val resolve get() =
124123
moveToGoalUntil(GoalInverted(GoalBlock(blockPos))) {
125124
val pBox = player.boundingBox
126125
val aabb = Box(pBox.minX, pBox.minY - 1.0E-6, pBox.minZ, pBox.maxX, pBox.minY, pBox.maxZ)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ abstract class BuildResult : ComparableResult<Rank> {
4747
) : Resolvable, BuildResult() {
4848
override val rank = Rank.CHUNK_NOT_LOADED
4949

50-
override val resolve = moveUntilLoaded(blockPos)
50+
override val resolve get() = moveUntilLoaded(blockPos)
5151

5252
override fun compareTo(other: ComparableResult<Rank>): Int {
5353
return when (other) {
@@ -76,7 +76,7 @@ abstract class BuildResult : ComparableResult<Rank> {
7676
override val blockPos: BlockPos,
7777
val blockState: BlockState
7878
) : BuildResult() {
79-
override val rank = Rank.BREAK_NO_PERMISSION
79+
override val rank get() = Rank.BREAK_NO_PERMISSION
8080
}
8181

8282
/**
@@ -114,7 +114,7 @@ abstract class BuildResult : ComparableResult<Rank> {
114114
) : Resolvable, BuildResult() {
115115
override val rank = Rank.NOT_VISIBLE
116116

117-
override val resolve = moveToGoal(GoalPlace(blockPos))
117+
override val resolve get() = moveToGoal(GoalPlace(blockPos))
118118

119119
override fun compareTo(other: ComparableResult<Rank>): Int {
120120
return when (other) {
@@ -135,8 +135,8 @@ abstract class BuildResult : ComparableResult<Rank> {
135135
) : Resolvable, BuildResult() {
136136
override val rank = Rank.WRONG_ITEM
137137

138-
override val resolve: Task<*> =
139-
neededItem.select().transfer(MainHandContainer)?.solve ?: emptyTask() // ToDo: Should throw error
138+
override val resolve get() =
139+
neededItem.select().transfer(MainHandContainer)?.solve ?: emptyTask("Item ${neededItem.name.string} not found") // ToDo: Should throw error
140140

141141
override fun compareTo(other: ComparableResult<Rank>): Int {
142142
return when (other) {
@@ -158,7 +158,7 @@ abstract class BuildResult : ComparableResult<Rank> {
158158
) : Resolvable, BuildResult() {
159159
override val rank = Rank.WRONG_ITEM
160160

161-
override val resolve: Task<*> =
161+
override val resolve get() =
162162
neededStack.select().transfer(MainHandContainer)?.solve ?: emptyTask() // ToDo: Should throw error
163163

164164
override fun compareTo(other: ComparableResult<Rank>): Int {
@@ -190,7 +190,7 @@ abstract class BuildResult : ComparableResult<Rank> {
190190
startVec.distanceTo(hitVec)
191191
}
192192

193-
override val resolve = moveNearBlock(blockPos, 2)
193+
override val resolve get() = moveNearBlock(blockPos, 2)
194194

195195
override fun compareTo(other: ComparableResult<Rank>): Int {
196196
return when (other) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ sealed class PlaceResult : BuildResult() {
3333
) : Resolvable, PlaceResult() {
3434
override val rank = Rank.PLACE_SUCCESS
3535

36-
override val resolve = placeBlock(context)
36+
override val resolve get() = placeBlock(context)
3737

3838
override fun compareTo(other: ComparableResult<Rank>): Int {
3939
return when (other) {
@@ -62,7 +62,7 @@ sealed class PlaceResult : BuildResult() {
6262
) : Resolvable, PlaceResult() {
6363
override val rank = Rank.PLACE_BLOCKED_BY_PLAYER
6464

65-
override val resolve = moveToGoalUntil(GoalInverted(GoalBlock(blockPos))) {
65+
override val resolve get() = moveToGoalUntil(GoalInverted(GoalBlock(blockPos))) {
6666
!world.canCollide(player, Box(blockPos))
6767
}
6868
}
@@ -78,7 +78,7 @@ sealed class PlaceResult : BuildResult() {
7878
override val rank = Rank.PLACE_CANT_REPLACE
7979

8080
// override val resolve = breakBlock(simulated.blockPos)
81-
override val resolve = moveToGoalUntil(GoalInverted(GoalBlock(blockPos))) {
81+
override val resolve get() = moveToGoalUntil(GoalInverted(GoalBlock(blockPos))) {
8282
!world.canCollide(player, Box(blockPos))
8383
}
8484
}
@@ -123,6 +123,6 @@ sealed class PlaceResult : BuildResult() {
123123
) : Resolvable, PlaceResult() {
124124
override val rank = Rank.PLACE_NOT_ITEM_BLOCK
125125

126-
override val resolve = Task.emptyTask() // ToDo: analyze interaction with non-block items
126+
override val resolve get() = Task.emptyTask() // ToDo: analyze interaction with non-block items
127127
}
128128
}

common/src/main/kotlin/com/lambda/interaction/material/MaterialContainer.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,47 @@ abstract class MaterialContainer(
4545
abstract fun withdraw(selection: StackSelection): Task<*>
4646

4747
@Task.Ta5kBuilder
48-
fun doWithdrawal(selection: StackSelection) =
49-
prepare()?.let { prep ->
48+
fun doWithdrawal(selection: StackSelection): Task<*> {
49+
return prepare()?.let { prep ->
5050
prep.onSuccess { _, _ ->
5151
withdraw(selection).start(prep)
52+
}.onStart {
53+
LOG.info("${it.identifier} withdrawing [$selection] from [$name]")
5254
}
53-
} ?: withdraw(selection)
55+
} ?: withdraw(selection).onStart {
56+
LOG.info("${it.identifier} withdrawing [$selection] from [$name]")
57+
}
58+
}
5459

5560
/**
5661
* Deposits items from the player's inventory into the container.
5762
*/
5863
abstract fun deposit(selection: StackSelection): Task<*>
5964

6065
@Task.Ta5kBuilder
61-
fun doDeposit(selection: StackSelection) =
62-
prepare()?.let { prep ->
66+
fun doDeposit(selection: StackSelection): Task<*> {
67+
return prepare()?.let { prep ->
6368
prep.onSuccess { _, _ ->
6469
deposit(selection).start(prep)
70+
}.onStart {
71+
LOG.info("${it.identifier} depositing [$selection] to [$name]")
6572
}
66-
} ?: deposit(selection)
73+
} ?: deposit(selection).onStart {
74+
LOG.info("${it.identifier} depositing [$selection] to [$name]")
75+
}
76+
}
6777

68-
open fun stacksMatching(selection: StackSelection) =
78+
open fun matchingStacks(selection: StackSelection) =
6979
selection.filterStacks(stacks)
7080

71-
open fun stacksMatching(selection: (ItemStack) -> Boolean) =
72-
stacksMatching(selection.select())
81+
open fun matchingStacks(selection: (ItemStack) -> Boolean) =
82+
matchingStacks(selection.select())
7383

7484
open fun available(selection: StackSelection) =
75-
stacksMatching(selection).count
85+
matchingStacks(selection).count
7686

7787
open fun spaceLeft(selection: StackSelection) =
78-
stacksMatching(selection).spaceLeft + stacks.empty * selection.stackSize
88+
matchingStacks(selection).spaceLeft + stacks.empty * selection.stackSize
7989

8090
fun transfer(selection: StackSelection, destination: MaterialContainer): TransferResult {
8191
val amount = available(selection)
@@ -92,12 +102,7 @@ abstract class MaterialContainer(
92102
// selection.selector = { true }
93103
// selection.count = transferAmount
94104

95-
LOG.info("Transferring $selection from $name to ${destination.name}")
96-
return TransferResult.Success(
97-
doWithdrawal(selection).onSuccess { withdraw, _ ->
98-
destination.doDeposit(selection).start(withdraw)
99-
}
100-
)
105+
return TransferResult.Success(selection, from = this, to = destination)
101106
}
102107

103108
enum class Rank {

common/src/main/kotlin/com/lambda/interaction/material/container/ChestContainer.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import net.minecraft.util.math.BlockPos
1616
data class ChestContainer(
1717
override var stacks: List<ItemStack>,
1818
val blockPos: BlockPos,
19+
val containedInStash: StashContainer? = null,
1920
) : MaterialContainer(Rank.CHEST) {
2021
override val name = "Chest at ${blockPos.toShortString()}"
2122

common/src/main/kotlin/com/lambda/interaction/material/container/EnderChestContainer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.lambda.interaction.material.container
33
import com.lambda.interaction.material.MaterialContainer
44
import com.lambda.interaction.material.StackSelection
55
import com.lambda.task.Task
6+
import com.lambda.task.Task.Companion.emptyTask
67
import com.lambda.task.tasks.InventoryTask.Companion.deposit
78
import com.lambda.task.tasks.InventoryTask.Companion.withdraw
89
import com.lambda.task.tasks.OpenContainer.Companion.openContainer
@@ -30,15 +31,15 @@ object EnderChestContainer : MaterialContainer(Rank.ENDER_CHEST) {
3031
// }
3132

3233
override fun withdraw(selection: StackSelection): Task<*> {
33-
val pos = placePos ?: return Task.emptyTask()
34+
val pos = placePos ?: return emptyTask("No placePos found for EnderChestContainer")
3435
return openContainer<GenericContainerScreenHandler>(pos)
3536
.onSuccess { _, screen ->
3637
withdraw(screen, selection)
3738
}
3839
}
3940

4041
override fun deposit(selection: StackSelection): Task<*> {
41-
val pos = placePos ?: return Task.emptyTask()
42+
val pos = placePos ?: return emptyTask("No placePos found for EnderChestContainer")
4243
return openContainer<GenericContainerScreenHandler>(pos)
4344
.onSuccess { _, screen ->
4445
deposit(screen, selection)

common/src/main/kotlin/com/lambda/interaction/material/container/InventoryContainer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.lambda.Lambda.mc
44
import com.lambda.interaction.material.MaterialContainer
55
import com.lambda.interaction.material.StackSelection
66
import com.lambda.task.Task
7+
import com.lambda.task.Task.Companion.emptyTask
78
import com.lambda.util.player.SlotUtils.combined
89
import net.minecraft.item.ItemStack
910

@@ -13,7 +14,7 @@ object InventoryContainer : MaterialContainer(Rank.INVENTORY) {
1314
set(_) {}
1415
override val name = "Inventory"
1516

16-
override fun withdraw(selection: StackSelection) = Task.emptyTask()
17+
override fun withdraw(selection: StackSelection) = emptyTask("WithdrawFromInventory")
1718

18-
override fun deposit(selection: StackSelection) = Task.emptyTask()
19+
override fun deposit(selection: StackSelection) = emptyTask("DepositToInventory")
1920
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ object MainHandContainer : MaterialContainer(Rank.MAIN_HAND) {
2121
override fun withdraw(selection: StackSelection) = emptyTask("WithdrawFromMainHand")
2222

2323
override fun deposit(selection: StackSelection) = buildTask("DepositToMainHand") {
24-
InventoryContainer.stacksMatching(selection).firstOrNull()?.let { stack ->
24+
InventoryContainer.matchingStacks(selection).firstOrNull()?.let { stack ->
2525
if (ItemStack.areEqual(stack, player.mainHandStack)) {
2626
return@buildTask
2727
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.lambda.interaction.material.container
33
import com.lambda.Lambda.mc
44
import com.lambda.interaction.material.MaterialContainer
55
import com.lambda.interaction.material.StackSelection
6-
import com.lambda.task.Task
76
import com.lambda.task.Task.Companion.buildTask
87
import com.lambda.task.Task.Companion.emptyTask
98
import com.lambda.util.player.SlotUtils.combined
@@ -22,7 +21,7 @@ object OffHandContainer : MaterialContainer(Rank.OFF_HAND) {
2221
override fun withdraw(selection: StackSelection) = emptyTask("WithdrawFromOffHand")
2322

2423
override fun deposit(selection: StackSelection) = buildTask("DepositToOffHand") {
25-
InventoryContainer.stacksMatching(selection).firstOrNull()?.let { stack ->
24+
InventoryContainer.matchingStacks(selection).firstOrNull()?.let { stack ->
2625
if (ItemStack.areEqual(stack, player.offHandStack)) {
2726
return@buildTask
2827
}

0 commit comments

Comments
 (0)