Skip to content

Commit 69d31f2

Browse files
committed
Fix task usecases
1 parent d1199cd commit 69d31f2

File tree

13 files changed

+142
-71
lines changed

13 files changed

+142
-71
lines changed

common/build.gradle.kts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ dependencies {
2727
implementation(kotlin("reflect"))
2828

2929
// Baritone
30-
modImplementation("baritone-api:baritone-api:1.10.2")
30+
// modImplementation("baritone-api:baritone-api:1.10.2")
3131
modImplementation("baritone-api:baritone-unoptimized-fabric:1.10.2")
32+
33+
testImplementation(kotlin("test"))
34+
}
35+
36+
tasks.test {
37+
useJUnitPlatform()
3238
}

common/src/main/java/com/lambda/mixin/MinecraftClientMixin.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import net.minecraft.client.network.ClientPlayerInteractionManager;
1515
import org.jetbrains.annotations.Nullable;
1616
import org.spongepowered.asm.mixin.Mixin;
17+
import org.spongepowered.asm.mixin.Shadow;
1718
import org.spongepowered.asm.mixin.injection.At;
1819
import org.spongepowered.asm.mixin.injection.Inject;
1920
import org.spongepowered.asm.mixin.injection.Redirect;
2021
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2122

2223
@Mixin(MinecraftClient.class)
2324
public class MinecraftClientMixin {
25+
@Shadow @Nullable public Screen currentScreen;
26+
2427
@Inject(method = "tick", at = @At("HEAD"))
2528
void onTickPre(CallbackInfo ci) {
2629
EventFlow.post(new TickEvent.Pre());
@@ -56,12 +59,12 @@ private void onScreenOpen(@Nullable Screen screen, CallbackInfo ci) {
5659

5760
@Inject(method = "setScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;removed()V", shift = At.Shift.AFTER))
5861
private void onScreenRemove(@Nullable Screen screen, CallbackInfo ci) {
59-
if (screen == null) return;
60-
if (screen instanceof ScreenHandlerProvider<?> handledScreen) {
62+
if (currentScreen == null) return;
63+
if (currentScreen instanceof ScreenHandlerProvider<?> handledScreen) {
6164
EventFlow.post(new ScreenHandlerEvent.Close<>(handledScreen.getScreenHandler()));
6265
}
6366

64-
EventFlow.post(new ScreenEvent.Close<>(screen));
67+
EventFlow.post(new ScreenEvent.Close<>(currentScreen));
6568
}
6669

6770
@Redirect(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;isBreakingBlock()Z"))

common/src/main/kotlin/com/lambda/core/Loader.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.lambda.graphics.renderer.gui.font.LambdaFont
88
import com.lambda.gui.GuiConfigurable
99
import com.lambda.interaction.PlayerPacketManager
1010
import com.lambda.interaction.RotationManager
11+
import com.lambda.interaction.material.ContainerManager
1112
import com.lambda.module.ModuleRegistry
1213
import com.lambda.sound.SoundRegistry
1314
import com.lambda.util.Communication.ascii
@@ -23,6 +24,7 @@ object Loader {
2324
GuiConfigurable,
2425
FriendRegistry,
2526
SoundRegistry,
27+
ContainerManager
2628
)
2729

2830
fun initialize() {

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

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

3+
import com.lambda.core.Loadable
34
import com.lambda.event.events.InteractionEvent
45
import com.lambda.event.events.ScreenHandlerEvent
56
import com.lambda.event.listener.SafeListener.Companion.listener
67
import com.lambda.interaction.material.StackSelection.Companion.select
78
import com.lambda.interaction.material.container.*
9+
import com.lambda.util.BlockUtils.blockEntity
810
import com.lambda.util.Communication.info
911
import com.lambda.util.item.ItemUtils
1012
import com.lambda.util.primitives.extension.containerStacks
@@ -19,7 +21,7 @@ import net.minecraft.screen.ScreenHandlerType
1921
import java.util.TreeSet
2022

2123
// ToDo: Make this a Configurable to save container caches. Should use a cached region based storage system.
22-
object ContainerManager {
24+
object ContainerManager : Loadable {
2325
// ToDo: Maybe use reflection to get all containers?
2426
val container = TreeSet<MaterialContainer>().apply {
2527
add(CreativeContainer)
@@ -34,10 +36,14 @@ object ContainerManager {
3436

3537
init {
3638
listener<InteractionEvent.Block> {
37-
lastInteractedBlockEntity = world.getBlockEntity(it.blockHitResult.blockPos)
39+
lastInteractedBlockEntity = it.blockHitResult.blockPos.blockEntity(world)
3840
}
3941

4042
listener<ScreenHandlerEvent.Close<GenericContainerScreenHandler>> { event ->
43+
// ToDo: ;-; i hate type erasure.
44+
// The listener will be triggered for any H, not just GenericContainerScreenHandler
45+
if (event.screenHandler !is GenericContainerScreenHandler) return@listener
46+
4147
val handler = event.screenHandler
4248

4349
when (val block = lastInteractedBlockEntity) {

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,19 @@ abstract class MaterialContainer(
2525
/**
2626
* Brings the player into a withdrawal/deposit state. E.g.: move to a chest etc.
2727
*/
28-
open fun prepare(): Task<*> = emptyTask()
28+
@Task.Ta5kBuilder
29+
open fun prepare(): Task<*> = emptyTask("EmptyPrepare")
2930

3031
/**
3132
* Withdraws items from the container to the player's inventory.
3233
*/
34+
@Task.Ta5kBuilder
3335
abstract fun withdraw(selection: StackSelection): Task<*>
3436

3537
/**
3638
* Deposits items from the player's inventory into the container.
3739
*/
40+
@Task.Ta5kBuilder
3841
abstract fun deposit(selection: StackSelection): Task<*>
3942

4043
open fun filter(selection: StackSelection) =
@@ -64,12 +67,15 @@ abstract class MaterialContainer(
6467
selector = { true }
6568
count = transferAmount
6669

67-
return TransferResult.Success(emptyTask().withSubTasks {
68-
prepare()
69-
withdraw(this@transfer)
70-
destination.prepare()
71-
deposit(this@transfer)
72-
})
70+
return TransferResult.Success(
71+
prepare().onSuccess { prep, _ ->
72+
withdraw(this@transfer).onSuccess { with, _ ->
73+
destination.prepare().onSuccess { dest, _ ->
74+
destination.deposit(this@transfer).start(dest)
75+
}.start(with)
76+
}.start(prep)
77+
}
78+
)
7379
}
7480

7581
fun List<ItemStack>.doShulkerCheck() =

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

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

3+
import com.lambda.Lambda.mc
34
import com.lambda.interaction.material.MaterialContainer
45
import com.lambda.interaction.material.StackSelection
56
import com.lambda.task.Task.Companion.buildTask
@@ -10,9 +11,9 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
1011
override var stacks = emptyList<ItemStack>()
1112

1213
override fun available(selection: StackSelection): Int =
13-
if (selection.optimalStack != null) Int.MAX_VALUE else 0
14+
if (mc.player?.isCreative == true && selection.optimalStack != null) Int.MAX_VALUE else 0
1415

15-
override fun deposit(selection: StackSelection) = buildTask {
16+
override fun deposit(selection: StackSelection) = buildTask("CreativeDeposit") {
1617
if (!player.isCreative) {
1718
// ToDo: Maybe switch gamemode?
1819
throw NotInCreativeModeException()
@@ -25,7 +26,7 @@ data object CreativeContainer : MaterialContainer(Rank.CREATIVE) {
2526
}
2627

2728
// Withdraws items from the creative menu to the player's main hand
28-
override fun withdraw(selection: StackSelection) = buildTask {
29+
override fun withdraw(selection: StackSelection) = buildTask("CreativeWithdraw") {
2930
selection.optimalStack?.let { optimalStack ->
3031
if (player.mainHandStack.equal(optimalStack)) return@buildTask
3132

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +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.interaction.material.StackSelection.Companion.select
65
import com.lambda.task.Task
7-
import com.lambda.task.tasks.AcquireMaterial.Companion.acquireStack
8-
import com.lambda.task.tasks.GoalTask.Companion.moveIntoEntityRange
96
import com.lambda.task.tasks.InventoryTask.Companion.deposit
107
import com.lambda.task.tasks.InventoryTask.Companion.withdraw
118
import com.lambda.task.tasks.OpenContainer.Companion.openContainer
12-
import com.lambda.task.tasks.PlaceContainer.Companion.placeContainer
139
import net.minecraft.block.Blocks
1410
import net.minecraft.item.ItemStack
1511
import net.minecraft.item.Items
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.lambda.module.modules.debug
2+
3+
import com.lambda.event.events.TickEvent
4+
import com.lambda.event.listener.SafeListener.Companion.listener
5+
import com.lambda.interaction.material.StackSelection.Companion.select
6+
import com.lambda.module.Module
7+
import com.lambda.module.tag.ModuleTag
8+
import com.lambda.task.tasks.AcquireMaterial.Companion.acquire
9+
import com.lambda.util.Communication.info
10+
import net.minecraft.item.Items
11+
12+
object ContainerTest : Module(
13+
name = "ContainerTest",
14+
description = "Test container",
15+
defaultTags = setOf(ModuleTag.DEBUG)
16+
) {
17+
init {
18+
listener<TickEvent.Pre> {
19+
// info(task.info)
20+
}
21+
22+
onEnable {
23+
acquire {
24+
Items.OBSIDIAN.select()
25+
}.start(null)
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)