Skip to content

Commit d76d2cc

Browse files
committed
Container ecosystem and new task builder DSL
1 parent 8bba416 commit d76d2cc

File tree

69 files changed

+1503
-779
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1503
-779
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import com.lambda.Lambda;
44
import com.lambda.event.EventFlow;
55
import com.lambda.event.events.ClientEvent;
6+
import com.lambda.event.events.ScreenEvent;
7+
import com.lambda.event.events.ScreenHandlerEvent;
68
import com.lambda.event.events.TickEvent;
79
import com.lambda.module.modules.player.Interact;
810
import net.minecraft.client.MinecraftClient;
11+
import net.minecraft.client.gui.screen.Screen;
12+
import net.minecraft.client.gui.screen.ingame.HandledScreen;
13+
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
914
import net.minecraft.client.network.ClientPlayerInteractionManager;
15+
import org.jetbrains.annotations.Nullable;
1016
import org.spongepowered.asm.mixin.Mixin;
1117
import org.spongepowered.asm.mixin.injection.At;
1218
import org.spongepowered.asm.mixin.injection.Inject;
@@ -38,6 +44,26 @@ private void onStartup(CallbackInfo ci) {
3844
EventFlow.post(new ClientEvent.Startup());
3945
}
4046

47+
@Inject(method = "setScreen", at = @At("HEAD"))
48+
private void onScreenOpen(@Nullable Screen screen, CallbackInfo ci) {
49+
if (screen == null) return;
50+
if (screen instanceof ScreenHandlerProvider<?> handledScreen) {
51+
EventFlow.post(new ScreenHandlerEvent.Open<>(handledScreen.getScreenHandler()));
52+
}
53+
54+
EventFlow.post(new ScreenEvent.Open<>(screen));
55+
}
56+
57+
@Inject(method = "setScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/screen/Screen;removed()V", shift = At.Shift.AFTER))
58+
private void onScreenRemove(@Nullable Screen screen, CallbackInfo ci) {
59+
if (screen == null) return;
60+
if (screen instanceof ScreenHandlerProvider<?> handledScreen) {
61+
EventFlow.post(new ScreenHandlerEvent.Close<>(handledScreen.getScreenHandler()));
62+
}
63+
64+
EventFlow.post(new ScreenEvent.Close<>(screen));
65+
}
66+
4167
@Redirect(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;isBreakingBlock()Z"))
4268
boolean injectMultiActon(ClientPlayerInteractionManager instance) {
4369
if (instance == null) return true;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.lambda.mixin.entity;
2+
3+
import com.lambda.event.EventFlow;
4+
import com.lambda.event.events.InteractionEvent;
5+
import net.minecraft.client.MinecraftClient;
6+
import net.minecraft.client.network.ClientPlayerEntity;
7+
import net.minecraft.client.network.ClientPlayerInteractionManager;
8+
import net.minecraft.util.ActionResult;
9+
import net.minecraft.util.Hand;
10+
import net.minecraft.util.hit.BlockHitResult;
11+
import org.spongepowered.asm.mixin.Final;
12+
import org.spongepowered.asm.mixin.Mixin;
13+
import org.spongepowered.asm.mixin.Shadow;
14+
import org.spongepowered.asm.mixin.injection.At;
15+
import org.spongepowered.asm.mixin.injection.Inject;
16+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
17+
18+
@Mixin(ClientPlayerInteractionManager.class)
19+
public class ClientPlayInteractionManagerMixin {
20+
21+
@Final
22+
@Shadow
23+
private MinecraftClient client;
24+
25+
@Inject(method = "interactBlock", at = @At("HEAD"))
26+
public void interactBlockHead(final ClientPlayerEntity player, final Hand hand, final BlockHitResult hitResult, final CallbackInfoReturnable<ActionResult> cir) {
27+
if (client.world == null) return;
28+
EventFlow.post(new InteractionEvent.Block(client.world, hitResult));
29+
}
30+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.lambda.mixin.render;
2+
3+
import com.lambda.event.EventFlow;
4+
import com.lambda.event.events.ScreenEvent;
5+
import com.lambda.event.events.ScreenHandlerEvent;
6+
import net.minecraft.item.ItemStack;
7+
import net.minecraft.screen.ScreenHandler;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.injection.At;
10+
import org.spongepowered.asm.mixin.injection.Inject;
11+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
12+
13+
import java.util.List;
14+
15+
@Mixin(ScreenHandler.class)
16+
public class ScreenHandlerMixin {
17+
@Inject(method = "updateSlotStacks", at = @At("HEAD"))
18+
private void onUpdateSlotStacksHead(int revision, List<ItemStack> stacks, ItemStack cursorStack, CallbackInfo ci) {
19+
EventFlow.post(new ScreenHandlerEvent.Loaded(revision, stacks, cursorStack));
20+
}
21+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import com.lambda.command.CommandManager
66
import com.lambda.interaction.PlayerPacketManager
77
import com.lambda.interaction.RotationManager
88
import com.lambda.module.ModuleRegistry
9+
import com.lambda.task.tasks.TaskTester
910
import com.lambda.util.Communication.ascii
1011
import kotlin.system.measureTimeMillis
1112

@@ -14,13 +15,15 @@ object Loader {
1415
ModuleRegistry,
1516
CommandManager,
1617
RotationManager,
17-
PlayerPacketManager
18+
PlayerPacketManager,
1819
)
1920

2021
fun initialize() {
2122
ascii.split("\n").forEach { LOG.info(it) }
2223
LOG.info("Initializing ${Lambda.MOD_NAME} ${Lambda.VERSION}")
2324

25+
TaskTester
26+
2427
val initTime = measureTimeMillis {
2528
loadables.forEach { loadable ->
2629
var info: String

common/src/main/kotlin/com/lambda/event/EventFlow.kt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,9 @@ package com.lambda.event
33
import com.lambda.event.callback.ICancellable
44
import com.lambda.event.listener.Listener
55
import com.lambda.threading.runConcurrent
6-
import kotlinx.coroutines.CoroutineScope
7-
import kotlinx.coroutines.Dispatchers
8-
import kotlinx.coroutines.Job
9-
import kotlinx.coroutines.SupervisorJob
6+
import kotlinx.coroutines.*
107
import kotlinx.coroutines.channels.BufferOverflow
11-
import kotlinx.coroutines.flow.MutableSharedFlow
12-
import kotlinx.coroutines.flow.filter
13-
import kotlinx.coroutines.flow.filterNot
8+
import kotlinx.coroutines.flow.*
149

1510

1611
/**
@@ -31,7 +26,7 @@ object EventFlow {
3126
* useful when you have multiple independent [Job]s running in parallel.
3227
*/
3328
val lambdaScope = CoroutineScope(Dispatchers.Default + SupervisorJob())
34-
private val concurrentFlow = MutableSharedFlow<Event>(
29+
val concurrentFlow = MutableSharedFlow<Event>(
3530
extraBufferCapacity = 1000,
3631
onBufferOverflow = BufferOverflow.DROP_OLDEST
3732
)
@@ -52,6 +47,30 @@ object EventFlow {
5247
}
5348
}
5449

50+
suspend inline fun <reified E : Event> awaitEvent(
51+
noinline predicate: (E) -> Boolean = { true },
52+
) = concurrentFlow.filterIsInstance<E>().first(predicate)
53+
54+
suspend inline fun <reified E : Event> awaitEvent(
55+
timeout: Long,
56+
noinline predicate: (E) -> Boolean = { true },
57+
) = runBlocking {
58+
withTimeout(timeout) {
59+
concurrentFlow.filterIsInstance<E>().first(predicate)
60+
}
61+
}
62+
63+
suspend inline fun <reified E : Event> awaitEvents(
64+
crossinline predicate: (E) -> Boolean = { true },
65+
): Flow<E> = flow {
66+
concurrentFlow
67+
.filterIsInstance<E>()
68+
.filter { predicate(it) }
69+
.collect {
70+
emit(it)
71+
}
72+
}
73+
5574
/**
5675
* Posts an [Event] to the event flow [concurrentFlow] and the synchronous [Listener]s.
5776
*
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lambda.event.events
2+
3+
import com.lambda.event.Event
4+
import net.minecraft.client.world.ClientWorld
5+
import net.minecraft.util.hit.BlockHitResult
6+
7+
sealed class InteractionEvent : Event {
8+
class Block(
9+
val world: ClientWorld,
10+
val blockHitResult: BlockHitResult
11+
) : InteractionEvent()
12+
}

common/src/main/kotlin/com/lambda/event/events/RotationEvent.kt

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import com.lambda.interaction.RotationManager
88
import com.lambda.interaction.rotation.IRotationConfig
99
import com.lambda.interaction.rotation.RotationRequest
1010
import com.lambda.interaction.visibilty.VisibilityChecker.findRotation
11+
import com.lambda.module.modules.client.TaskFlow
1112
import com.lambda.threading.runSafe
1213
import com.lambda.util.world.raycast.RayCastUtils.blockResult
1314
import com.lambda.util.world.raycast.RayCastUtils.entityResult
@@ -44,22 +45,22 @@ abstract class RotationEvent : Event {
4445
}
4546

4647
fun lookAtBlock(
47-
rotationConfig: IRotationConfig,
48-
interactionConfig: InteractionConfig,
4948
blockPos: BlockPos,
49+
rotationConfig: IRotationConfig = TaskFlow.rotationSettings,
50+
interactionConfig: InteractionConfig = TaskFlow.interactionSettings,
5051
sides: Set<Direction> = emptySet(),
5152
priority: Int = 0,
52-
) {
53-
runSafe {
54-
val state = world.getBlockState(blockPos)
55-
val voxelShape = state.getOutlineShape(world, blockPos)
56-
val boundingBoxes = voxelShape.boundingBoxes.map { it.offset(blockPos) }
57-
findRotation(rotationConfig, interactionConfig, boundingBoxes, priority, sides) {
58-
blockResult?.blockPos == blockPos && (blockResult?.side in sides || sides.isEmpty())
59-
}?.let {
60-
requests.add(it)
61-
}
53+
) = runSafe {
54+
val state = world.getBlockState(blockPos)
55+
val voxelShape = state.getOutlineShape(world, blockPos)
56+
val boundingBoxes = voxelShape.boundingBoxes.map { it.offset(blockPos) }
57+
findRotation(rotationConfig, interactionConfig, boundingBoxes, priority, sides) {
58+
blockResult?.blockPos == blockPos && (blockResult?.side in sides || sides.isEmpty())
59+
}?.let {
60+
requests.add(it)
61+
return@runSafe it
6262
}
63+
return@runSafe null
6364
}
6465
}
6566

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lambda.event.events
2+
3+
import com.lambda.event.Event
4+
import net.minecraft.client.gui.screen.Screen
5+
import net.minecraft.client.gui.screen.ingame.HandledScreen
6+
import net.minecraft.item.ItemStack
7+
import net.minecraft.screen.ScreenHandler
8+
9+
sealed class ScreenEvent : Event {
10+
class Open<T : Screen>(val screen: T) : ScreenEvent()
11+
class Close<T : Screen>(val screen: T) : ScreenEvent()
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.event.events
2+
3+
import com.lambda.event.Event
4+
import net.minecraft.client.gui.screen.Screen
5+
import net.minecraft.client.gui.screen.ingame.HandledScreen
6+
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider
7+
import net.minecraft.item.ItemStack
8+
import net.minecraft.screen.ScreenHandler
9+
10+
sealed class ScreenHandlerEvent : Event {
11+
class Open<T : ScreenHandler>(val screen: T) : ScreenHandlerEvent()
12+
class Close<T : ScreenHandler>(val screen: T) : ScreenHandlerEvent()
13+
data class Loaded(
14+
val revision: Int,
15+
val stacks: List<ItemStack>,
16+
val cursorStack: ItemStack,
17+
) : ScreenHandlerEvent()
18+
}

common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import com.lambda.interaction.rotation.RotationContext
1515
import com.lambda.interaction.rotation.RotationMode
1616
import com.lambda.interaction.rotation.RotationRequest
1717
import com.lambda.module.modules.client.Baritone
18-
import com.lambda.threading.runOnGameThread
18+
import com.lambda.threading.runOnGameThreadConcurrent
1919
import com.lambda.threading.runSafe
2020
import com.lambda.util.math.MathUtils.lerp
2121
import com.lambda.util.math.MathUtils.toRadian
@@ -52,7 +52,7 @@ object RotationManager : Loadable {
5252
val packet = event.packet
5353
if (packet !is PlayerPositionLookS2CPacket) return@listener
5454

55-
runOnGameThread {
55+
runOnGameThreadConcurrent {
5656
reset(Rotation(packet.yaw, packet.pitch))
5757
}
5858
}
@@ -117,7 +117,7 @@ object RotationManager : Loadable {
117117
}
118118
}
119119

120-
return@runSafe chosenRequest
120+
return@runSafe currentRequest
121121
}
122122

123123
private fun reset(rotation: Rotation) {

0 commit comments

Comments
 (0)