Skip to content

Commit 4adfb18

Browse files
committed
Nullable withdrawal or deposit tasks
1 parent 48f1144 commit 4adfb18

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ abstract class BuildResult : ComparableResult<Rank>, Nameable {
206206
override val pausesParent get() = true
207207

208208
override fun resolve() = neededItem.select()
209-
.transfer(MainHandContainer, inventory) ?: MaterialContainer.Nothing()
209+
.transfer(MainHandContainer, inventory) ?: MaterialContainer.Nothing("Couldn't find ${neededItem.name.string} anywhere.")
210210

211211
override fun SafeContext.buildRenderer() {
212212
if (blockPos.blockState(world).isAir) {
@@ -242,7 +242,7 @@ abstract class BuildResult : ComparableResult<Rank>, Nameable {
242242
override val pausesParent get() = true
243243

244244
override fun resolve() =
245-
neededStack.select().transfer(MainHandContainer, inventory) ?: MaterialContainer.Nothing()
245+
neededStack.select().transfer(MainHandContainer, inventory) ?: MaterialContainer.Nothing("Couldn't find ${neededStack.item.name.string} anywhere.")
246246

247247
override fun SafeContext.buildRenderer() {
248248
if (blockPos.blockState(world).isAir) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,21 +82,21 @@ abstract class MaterialContainer(
8282

8383
class Nothing(override val name: String = "Nothing") : Task<Unit>() {
8484
override fun SafeContext.onStart() {
85-
success()
85+
failure(name)
8686
}
8787
}
8888

8989
/**
9090
* Withdraws items from the container to the player's inventory.
9191
*/
9292
@Task.Ta5kBuilder
93-
open fun withdraw(selection: StackSelection): Task<*> = Nothing(name)
93+
open fun withdraw(selection: StackSelection): Task<*>? = null
9494

9595
/**
9696
* Deposits items from the player's inventory into the container.
9797
*/
9898
@Task.Ta5kBuilder
99-
open fun deposit(selection: StackSelection): Task<*> = Nothing(name)
99+
open fun deposit(selection: StackSelection): Task<*>? = null
100100

101101
open fun matchingStacks(selection: StackSelection) =
102102
selection.filterStacks(stacks)

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,25 @@ abstract class TransferResult : Task<Unit>() {
3131
override val name = "Container Transfer of [$selection] from [${from.name}] to [${to.name}]"
3232

3333
override fun SafeContext.onStart() {
34-
from.withdraw(selection).then {
35-
to.deposit(selection).finally {
36-
success()
34+
val withdrawal = from.withdraw(selection)
35+
val deposit = to.deposit(selection)
36+
37+
val task = when {
38+
withdrawal != null && deposit != null -> {
39+
withdrawal.then {
40+
deposit.finally { success() }
41+
}
42+
}
43+
withdrawal != null -> {
44+
withdrawal.finally { success() }
3745
}
38-
}.execute(this@ContainerTransfer)
46+
deposit != null -> {
47+
deposit.finally { success() }
48+
}
49+
else -> null
50+
}
51+
52+
task?.execute(this@ContainerTransfer)
3953
}
4054
}
4155

0 commit comments

Comments
 (0)