Skip to content

Commit 070c2b7

Browse files
committed
Material transaction optimizations, task cooldown, drop collection ...
1 parent fe82165 commit 070c2b7

File tree

24 files changed

+306
-182
lines changed

24 files changed

+306
-182
lines changed

common/src/main/kotlin/com/lambda/command/commands/TransferCommand.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ object TransferCommand : LambdaCommand(
2525
required(integer("amount")) { amount ->
2626
required(string("from")) { from ->
2727
suggests { ctx, builder ->
28-
val selection = selectStack(amount(ctx).value()) {
28+
val count = amount(ctx).value()
29+
val selection = selectStack(count) {
2930
isItem(stack(ctx).value().item)
3031
}
3132
containerMatchSelection(selection).forEach {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ abstract class BuildResult : ComparableResult<Rank> {
115115
) : Navigable, Resolvable, BuildResult() {
116116
override val rank = Rank.NOT_VISIBLE
117117

118-
override val resolve get() = moveToGoal(GoalPlace(blockPos))
118+
override val resolve get() = moveToGoal {
119+
GoalPlace(blockPos)
120+
}
119121

120122
override fun compareTo(other: ComparableResult<Rank>): Int {
121123
return when (other) {

common/src/main/kotlin/com/lambda/interaction/construction/simulation/BuildSimulator.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ object BuildSimulator {
121121
if (voxelShape.isEmpty) return@forEach
122122

123123
val boxes = voxelShape.boundingBoxes.map { it.offset(hitPos) }
124+
125+
if (boxes.all { it.center.distanceTo(eye) > interact.reach + 1 }) {
126+
acc.add(BuildResult.OutOfReach(pos, eye, hitPos.vecOf(hitSide), interact.reach, hitSide))
127+
return@forEach
128+
}
124129
val verify: HitResult.() -> Boolean = {
125130
blockResult?.blockPos == hitPos && blockResult?.side == hitSide
126131
}
@@ -321,9 +326,14 @@ object BuildSimulator {
321326
voxelShape.getClosestPointTo(eye).ifPresent {
322327
// ToDo: Use closest point of shape
323328
}
329+
324330
val boxes = voxelShape.boundingBoxes.map { it.offset(pos) }
325-
val verify: HitResult.() -> Boolean = { blockResult?.blockPos == pos }
331+
if (boxes.all { it.center.distanceTo(eye) > interact.reach + 1 }) {
332+
acc.add(BuildResult.OutOfReach(pos, eye, pos.toCenterPos(), interact.reach, Direction.UP))
333+
return acc
334+
}
326335

336+
val verify: HitResult.() -> Boolean = { blockResult?.blockPos == pos }
327337
/* the player is buried inside the block */
328338
if (boxes.any { it.contains(eye) }) {
329339
currentCast?.blockResult?.let { blockHit ->

common/src/main/kotlin/com/lambda/interaction/construction/verify/TargetState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ sealed class TargetState : StateMatcher {
4242
block.getPickStack(world, pos, block.defaultState)
4343
}
4444
data class Stack(val itemStack: ItemStack) : TargetState() {
45-
val copy = itemStack.copy()
45+
val copy: ItemStack = itemStack.copy()
4646

4747
override fun matches(state: BlockState, pos: BlockPos, world: ClientWorld) =
4848
state.block == copy.item.block
4949

5050
override fun getStack(world: ClientWorld, pos: BlockPos): ItemStack =
51-
itemStack
51+
copy
5252
}
5353
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import net.minecraft.block.entity.EnderChestBlockEntity
1717
import net.minecraft.item.Item
1818
import net.minecraft.item.ItemStack
1919
import net.minecraft.screen.GenericContainerScreenHandler
20+
import net.minecraft.screen.ScreenHandler
2021
import net.minecraft.screen.ScreenHandlerType
2122
import java.util.TreeSet
2223

@@ -39,7 +40,7 @@ object ContainerManager : Loadable {
3940
lastInteractedBlockEntity = it.blockHitResult.blockPos.blockEntity(world)
4041
}
4142

42-
listener<ScreenHandlerEvent.Close<GenericContainerScreenHandler>> { event ->
43+
listener<ScreenHandlerEvent.Close<ScreenHandler>> { event ->
4344
// ToDo: ;-; i hate type erasure.
4445
// The listener will be triggered for any H, not just GenericContainerScreenHandler
4546
if (event.screenHandler !is GenericContainerScreenHandler) return@listener

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.interaction.material
22

3-
import com.lambda.Lambda.LOG
43
import com.lambda.interaction.material.StackSelection.Companion.select
54
import com.lambda.interaction.material.container.ShulkerBoxContainer
65
import com.lambda.interaction.material.transfer.TransferResult
@@ -34,46 +33,17 @@ abstract class MaterialContainer(
3433
this.stacks = stacks
3534
}
3635

37-
/**
38-
* Brings the player into a withdrawal/deposit state. E.g.: move to a chest etc.
39-
*/
40-
open fun prepare(): Task<*>? = null
41-
4236
/**
4337
* Withdraws items from the container to the player's inventory.
4438
*/
45-
abstract fun withdraw(selection: StackSelection): Task<*>
46-
4739
@Task.Ta5kBuilder
48-
fun doWithdrawal(selection: StackSelection): Task<*> {
49-
return prepare()?.let { prep ->
50-
prep.onSuccess { _, _ ->
51-
withdraw(selection).start(prep)
52-
}.onStart {
53-
LOG.info("${it.identifier} withdrawing [$selection] from [$name]")
54-
}
55-
} ?: withdraw(selection).onStart {
56-
LOG.info("${it.identifier} withdrawing [$selection] from [$name]")
57-
}
58-
}
40+
abstract fun withdraw(selection: StackSelection): Task<*>
5941

6042
/**
6143
* Deposits items from the player's inventory into the container.
6244
*/
63-
abstract fun deposit(selection: StackSelection): Task<*>
64-
6545
@Task.Ta5kBuilder
66-
fun doDeposit(selection: StackSelection): Task<*> {
67-
return prepare()?.let { prep ->
68-
prep.onSuccess { _, _ ->
69-
deposit(selection).start(prep)
70-
}.onStart {
71-
LOG.info("${it.identifier} depositing [$selection] to [$name]")
72-
}
73-
} ?: deposit(selection).onStart {
74-
LOG.info("${it.identifier} depositing [$selection] to [$name]")
75-
}
76-
}
46+
abstract fun deposit(selection: StackSelection): Task<*>
7747

7848
open fun matchingStacks(selection: StackSelection) =
7949
selection.filterStacks(stacks)
@@ -90,7 +60,7 @@ abstract class MaterialContainer(
9060
fun transfer(selection: StackSelection, destination: MaterialContainer): TransferResult {
9161
val amount = available(selection)
9262
if (amount < selection.count) {
93-
return TransferResult.MissingItems(amount - selection.count)
63+
return TransferResult.MissingItems( selection.count - amount)
9464
}
9565

9666
// val space = destination.spaceLeft(selection)

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package com.lambda.interaction.material.container
22

33
import com.lambda.interaction.material.MaterialContainer
44
import com.lambda.interaction.material.StackSelection
5-
import com.lambda.task.tasks.GoalTask.Companion.moveIntoEntityRange
65
import com.lambda.task.tasks.InventoryTask.Companion.deposit
76
import com.lambda.task.tasks.InventoryTask.Companion.withdraw
87
import com.lambda.task.tasks.OpenContainer.Companion.openContainer
98
import com.lambda.util.Communication.info
10-
import net.minecraft.block.ChestBlock
119
import net.minecraft.item.ItemStack
1210
import net.minecraft.screen.GenericContainerScreenHandler
1311
import net.minecraft.screen.ScreenHandler
@@ -20,16 +18,16 @@ data class ChestContainer(
2018
) : MaterialContainer(Rank.CHEST) {
2119
override val name = "Chest at ${blockPos.toShortString()}"
2220

23-
override fun prepare() =
24-
moveIntoEntityRange(blockPos).onSuccess { _, _ ->
25-
// when {
26-
// ChestBlock.hasBlockOnTop(world, blockPos) -> breakBlock(blockPos.up())
27-
// ChestBlock.hasCatOnTop(world, blockPos) -> kill(cat)
21+
// override fun prepare() =
22+
// moveIntoEntityRange(blockPos).onSuccess { _, _ ->
23+
//// when {
24+
//// ChestBlock.hasBlockOnTop(world, blockPos) -> breakBlock(blockPos.up())
25+
//// ChestBlock.hasCatOnTop(world, blockPos) -> kill(cat)
26+
//// }
27+
// if (ChestBlock.isChestBlocked(world, blockPos)) {
28+
// throw ChestBlockedException()
2829
// }
29-
if (ChestBlock.isChestBlocked(world, blockPos)) {
30-
throw ChestBlockedException()
31-
}
32-
}
30+
// }
3331

3432
override fun withdraw(selection: StackSelection) =
3533
openContainer<GenericContainerScreenHandler>(blockPos)

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

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +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
76
import com.lambda.task.Task.Companion.failTask
8-
import com.lambda.task.tasks.InventoryTask.Companion.deposit
9-
import com.lambda.task.tasks.InventoryTask.Companion.withdraw
107
import com.lambda.task.tasks.OpenContainer.Companion.openContainer
118
import net.minecraft.item.ItemStack
129
import net.minecraft.screen.GenericContainerScreenHandler
@@ -17,9 +14,9 @@ object EnderChestContainer : MaterialContainer(Rank.ENDER_CHEST) {
1714
override val name = "EnderChest"
1815
private var placePos: BlockPos? = null
1916

20-
override fun prepare(): Task<*> {
21-
TODO("Not yet implemented")
22-
}
17+
// override fun prepare(): Task<*> {
18+
// TODO("Not yet implemented")
19+
// }
2320
// findBlock(Blocks.ENDER_CHEST).onSuccess { pos ->
2421
// moveIntoEntityRange(pos)
2522
// placePos = pos
@@ -32,18 +29,10 @@ object EnderChestContainer : MaterialContainer(Rank.ENDER_CHEST) {
3229
// }
3330

3431
override fun withdraw(selection: StackSelection): Task<*> {
35-
val pos = placePos ?: return failTask("No placePos found for EnderChestContainer")
36-
return openContainer<GenericContainerScreenHandler>(pos)
37-
.onSuccess { _, screen ->
38-
withdraw(screen, selection)
39-
}
32+
TODO()
4033
}
4134

4235
override fun deposit(selection: StackSelection): Task<*> {
43-
val pos = placePos ?: return failTask("No placePos found for EnderChestContainer")
44-
return openContainer<GenericContainerScreenHandler>(pos)
45-
.onSuccess { _, screen ->
46-
deposit(screen, selection)
47-
}
36+
TODO()
4837
}
4938
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.lambda.task.Task
77
import com.lambda.task.Task.Companion.emptyTask
88
import com.lambda.task.tasks.InventoryTask.Companion.deposit
99
import com.lambda.util.player.SlotUtils.hotbar
10-
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider
1110
import net.minecraft.item.ItemStack
1211

1312
object HotbarContainer : MaterialContainer(Rank.HOTBAR) {
@@ -19,7 +18,7 @@ object HotbarContainer : MaterialContainer(Rank.HOTBAR) {
1918
override fun withdraw(selection: StackSelection) = emptyTask("WithdrawFromHotbar")
2019

2120
override fun deposit(selection: StackSelection): Task<*> {
22-
val handledScreen = mc.currentScreen as? ScreenHandlerProvider<*> ?: return emptyTask()
23-
return deposit(handledScreen.screenHandler, selection)
21+
val handler = mc.player?.currentScreenHandler ?: return emptyTask("NoScreenHandler")
22+
return deposit(handler, selection)
2423
}
2524
}
Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,63 @@
11
package com.lambda.interaction.material.container
22

33
import com.lambda.Lambda.LOG
4+
import com.lambda.context.SafeContext
45
import com.lambda.interaction.material.MaterialContainer
56
import com.lambda.interaction.material.StackSelection
67
import com.lambda.task.Task
7-
import com.lambda.task.Task.Companion.emptyTask
8-
import com.lambda.task.Task.Companion.failTask
98
import com.lambda.task.tasks.BuildStructure.Companion.breakAndCollectBlock
109
import com.lambda.task.tasks.InventoryTask.Companion.deposit
1110
import com.lambda.task.tasks.InventoryTask.Companion.withdraw
1211
import com.lambda.task.tasks.OpenContainer.Companion.openContainer
1312
import com.lambda.task.tasks.PlaceContainer.Companion.placeContainer
14-
import com.lambda.util.Communication.info
1513
import net.minecraft.item.ItemStack
1614
import net.minecraft.screen.ShulkerBoxScreenHandler
17-
import net.minecraft.util.math.BlockPos
1815

1916
data class ShulkerBoxContainer(
2017
override var stacks: List<ItemStack>,
2118
val containedIn: MaterialContainer,
2219
val shulkerStack: ItemStack,
2320
) : MaterialContainer(Rank.SHULKER_BOX) {
24-
private var openScreen: ShulkerBoxScreenHandler? = null
25-
private var placePosition: BlockPos? = null
26-
2721
override val name = "${shulkerStack.name.string} in slot $slotInContainer in ${containedIn.name}"
2822

2923
private val slotInContainer: Int get() = containedIn.stacks.indexOf(shulkerStack)
3024

31-
override fun prepare() =
32-
placeContainer(shulkerStack).onSuccess { place, placePos ->
33-
LOG.info("Container placed. Opening now ShulkerBoxContainer")
34-
placePosition = placePos
35-
openContainer<ShulkerBoxScreenHandler>(placePos).onSuccess { _, screen ->
36-
openScreen = screen
37-
}.start(place)
38-
}
39-
40-
override fun withdraw(selection: StackSelection): Task<*> {
41-
val open = openScreen ?: return failTask("No open screen found for ShulkerBoxContainer")
42-
val place = placePosition ?: return failTask("No place position found for ShulkerBoxContainer")
43-
info("Withdrawing $selection from ${shulkerStack.name.string}")
44-
return withdraw(open, selection).onSuccess { withdraw, _ ->
45-
breakAndCollectBlock(place).start(withdraw)
25+
class Withdraw(
26+
private val selection: StackSelection,
27+
private val shulkerStack: ItemStack
28+
) : Task<Unit>() {
29+
override fun SafeContext.onStart() {
30+
placeContainer(shulkerStack).thenRun { _, placePos ->
31+
openContainer<ShulkerBoxScreenHandler>(placePos).thenRun { _, screen ->
32+
LOG.info("Opened shulker box screen now withdrawing $selection.")
33+
withdraw(screen, selection).thenRun { _, _ ->
34+
breakAndCollectBlock(placePos).onSuccess { _, _ ->
35+
success(Unit)
36+
}
37+
}
38+
}
39+
}.start(this@Withdraw)
4640
}
4741
}
4842

49-
override fun deposit(selection: StackSelection): Task<*> {
50-
val open = openScreen ?: return failTask("No open screen found for ShulkerBoxContainer")
51-
val place = placePosition ?: return failTask("No place position found for ShulkerBoxContainer")
52-
info("Depositing $selection to ${shulkerStack.name.string}")
53-
return deposit(open, selection).onSuccess { deposit, _ ->
54-
breakAndCollectBlock(place).start(deposit)
43+
override fun withdraw(selection: StackSelection) = Withdraw(selection, shulkerStack)
44+
45+
class Deposit(
46+
private val selection: StackSelection,
47+
private val shulkerStack: ItemStack
48+
) : Task<Unit>() {
49+
override fun SafeContext.onStart() {
50+
placeContainer(shulkerStack).thenRun { _, placePos ->
51+
openContainer<ShulkerBoxScreenHandler>(placePos).thenRun { _, screen ->
52+
deposit(screen, selection).thenRun { _, _ ->
53+
breakAndCollectBlock(placePos).onSuccess { _, _ ->
54+
success(Unit)
55+
}
56+
}
57+
}
58+
}.start(this@Deposit)
5559
}
5660
}
61+
62+
override fun deposit(selection: StackSelection) = Deposit(selection, shulkerStack)
5763
}

0 commit comments

Comments
 (0)