Skip to content

Commit a5ea17c

Browse files
committed
Refactor Task system for enhanced flexibility and clarity
Replaced `TaskResult` with more explicit task patterns and streamlined result handling. Improved modularity by refactoring references to `TaskFlow` into `TaskFlowModule`. Added `name` properties to various tasks for better debugging and logging.
1 parent d91811f commit a5ea17c

Some content is hidden

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

52 files changed

+667
-826
lines changed

common/src/main/java/com/lambda/mixin/render/DebugHudMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package com.lambda.mixin.render;
1919

20-
import com.lambda.task.RootTask;
20+
import com.lambda.task.TaskFlow;
2121
import com.lambda.util.DebugInfoHud;
2222
import net.minecraft.client.gui.hud.DebugHud;
2323
import org.spongepowered.asm.mixin.Mixin;
@@ -36,6 +36,6 @@ private void onGetRightText(CallbackInfoReturnable<List<String>> cir) {
3636

3737
@Inject(method = "getLeftText", at = @At(value = "TAIL"))
3838
private void onGetLeftText(CallbackInfoReturnable<List<String>> cir) {
39-
RootTask.INSTANCE.addInfo(cir.getReturnValue());
39+
cir.getReturnValue().addAll(List.of(TaskFlow.INSTANCE.toString().split("\n")));
4040
}
4141
}

common/src/main/java/com/lambda/mixin/world/ClientWorldMixin.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@
3434

3535
@Mixin(ClientWorld.class)
3636
public class ClientWorldMixin {
37-
@Inject(method = "handleBlockUpdate", at = @At("HEAD"), cancellable = true)
38-
private void handleBlockUpdateInject(BlockPos pos, BlockState state, int flags, CallbackInfo ci) {
39-
if (EventFlow.post(new WorldEvent.BlockUpdate(pos, state, flags)).isCanceled()) {
40-
ci.cancel();
41-
}
42-
}
43-
4437
@Inject(method = "addEntity", at = @At("HEAD"), cancellable = true)
4538
private void addEntity(Entity entity, CallbackInfo ci) {
4639
if (EventFlow.post(new WorldEvent.EntitySpawn(entity)).isCanceled()) ci.cancel();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2024 Lambda
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.lambda.mixin.world;
19+
20+
import com.lambda.event.EventFlow;
21+
import com.lambda.event.events.WorldEvent;
22+
import net.minecraft.block.BlockState;
23+
import net.minecraft.util.math.BlockPos;
24+
import net.minecraft.world.World;
25+
import org.spongepowered.asm.mixin.Mixin;
26+
import org.spongepowered.asm.mixin.injection.At;
27+
import org.spongepowered.asm.mixin.injection.Inject;
28+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
29+
30+
@Mixin(World.class)
31+
public abstract class WorldMixin {
32+
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("HEAD"), cancellable = true)
33+
void setBlockStatePre(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
34+
if (EventFlow.post(new WorldEvent.BlockUpdate.Pre(pos, state, flags, maxUpdateDepth)).isCanceled()) {
35+
cir.setReturnValue(false);
36+
}
37+
}
38+
39+
@Inject(method = "setBlockState(Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/block/BlockState;II)Z", at = @At("TAIL"))
40+
void setBlockStatePost(BlockPos pos, BlockState state, int flags, int maxUpdateDepth, CallbackInfoReturnable<Boolean> cir) {
41+
EventFlow.post(new WorldEvent.BlockUpdate.Post(pos, state, flags, maxUpdateDepth));
42+
}
43+
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import com.lambda.command.LambdaCommand
2626
import com.lambda.interaction.construction.StructureRegistry
2727
import com.lambda.interaction.construction.blueprint.Blueprint.Companion.toStructure
2828
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
29+
import com.lambda.task.TaskFlow.run
2930
import com.lambda.task.tasks.BuildTask.Companion.build
3031
import com.lambda.threading.runSafe
3132
import com.lambda.util.Communication.info
@@ -62,7 +63,7 @@ object BuildCommand : LambdaCommand(
6263
.move(player.blockPos)
6364
.toBlueprint()
6465
.build(pathing = doPathing)
65-
.start(null)
66+
.run()
6667

6768
return@executeWithResult CommandResult.success()
6869
}

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,28 @@ import com.lambda.brigadier.argument.literal
2121
import com.lambda.brigadier.execute
2222
import com.lambda.brigadier.required
2323
import com.lambda.command.LambdaCommand
24-
import com.lambda.task.RootTask
24+
import com.lambda.task.TaskFlow
2525
import com.lambda.util.Communication.info
2626
import com.lambda.util.extension.CommandBuilder
2727

2828
object TaskCommand : LambdaCommand(
2929
name = "task",
30-
usage = "task <cancel>",
30+
usage = "task <cancel|clear>",
3131
description = "Control tasks"
3232
) {
3333
override fun CommandBuilder.create() {
3434
required(literal("cancel")) {
3535
execute {
3636
this@TaskCommand.info("Cancelling all tasks")
37-
RootTask.cancel()
37+
TaskFlow.cancel()
38+
}
39+
}
40+
41+
required(literal("clear")) {
42+
execute {
43+
this@TaskCommand.info("Clearing all tasks")
44+
TaskFlow.cancel()
45+
TaskFlow.clear()
3846
}
3947
}
4048
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.lambda.interaction.material.ContainerManager
2727
import com.lambda.interaction.material.ContainerManager.containerMatchSelection
2828
import com.lambda.interaction.material.StackSelection.Companion.selectStack
2929
import com.lambda.interaction.material.transfer.TransferResult
30+
import com.lambda.task.TaskFlow.run
3031
import com.lambda.util.Communication.info
3132
import com.lambda.util.extension.CommandBuilder
3233

@@ -77,18 +78,18 @@ object TransferCommand : LambdaCommand(
7778
it.name == to().value().split(" with ").firstOrNull()
7879
} ?: return@executeWithResult failure("To container not found")
7980

80-
when (val result = fromContainer.transfer(selection, toContainer)) {
81+
when (val transaction = fromContainer.transfer(selection, toContainer)) {
8182
is TransferResult.Transfer -> {
82-
info("$result started.")
83-
lastTransfer = result
84-
result.onSuccess { _, _ ->
83+
info("$transaction started.")
84+
lastTransfer = transaction
85+
transaction.finally {
8586
info("$lastTransfer completed.")
86-
}.start(null)
87+
}.run()
8788
return@executeWithResult success()
8889
}
8990

9091
is TransferResult.MissingItems -> {
91-
return@executeWithResult failure("Missing items: ${result.missing}")
92+
return@executeWithResult failure("Missing items: ${transaction.missing}")
9293
}
9394

9495
is TransferResult.NoSpace -> {

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import com.lambda.event.Event
2121
import com.lambda.event.callback.Cancellable
2222
import com.lambda.event.callback.ICancellable
2323
import net.minecraft.block.BlockState
24-
import net.minecraft.client.world.ClientWorld
2524
import net.minecraft.entity.Entity
2625
import net.minecraft.entity.data.TrackedData
2726
import net.minecraft.util.math.BlockPos
@@ -49,11 +48,15 @@ sealed class WorldEvent {
4948
/**
5049
* Represents a block update in the world
5150
*/
52-
class BlockUpdate(
51+
sealed class BlockUpdate(
5352
val pos: BlockPos,
5453
val state: BlockState,
55-
val flags: Int
56-
) : ICancellable by Cancellable()
54+
val flags: Int,
55+
val maxUpdateDepth: Int,
56+
) {
57+
class Pre(pos: BlockPos, state: BlockState, flags: Int, depth: Int) : BlockUpdate(pos, state, flags, depth), ICancellable by Cancellable()
58+
class Post(pos: BlockPos, state: BlockState, flags: Int, depth: Int) : BlockUpdate(pos, state, flags, depth), Event
59+
}
5760

5861
/**
5962
* Represents an entity being added to the world

common/src/main/kotlin/com/lambda/graphics/renderer/esp/ChunkedESP.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ChunkedESP private constructor(
5353
}
5454

5555
init {
56-
listenConcurrently<WorldEvent.BlockUpdate> { event ->
56+
listenConcurrently<WorldEvent.BlockUpdate.Post> { event ->
5757
world.getWorldChunk(event.pos).renderer.notifyChunks()
5858
}
5959

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/Blueprint.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ abstract class Blueprint {
4646

4747
fun isOutOfBounds(vec3d: Vec3d): Boolean = !bounds.contains(vec3d.blockPos)
4848

49+
val center get() = bounds.center.blockPos
50+
4951
companion object {
5052
fun emptyStructure(): Structure = emptyMap()
5153

common/src/main/kotlin/com/lambda/interaction/construction/blueprint/DynamicBlueprint.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ data class DynamicBlueprint(
3636
override var structure: Structure = emptyMap()
3737
private set
3838

39+
override fun toString() = "Dynamic Blueprint at ${center.toShortString()}"
40+
3941
companion object {
4042
fun offset(offset: Vec3i): SafeContext.(Structure) -> Structure = {
4143
it.map { (pos, state) ->

0 commit comments

Comments
 (0)