Skip to content

Commit adf42c0

Browse files
committed
use block attack event and cancel handleBlockBreaking call
1 parent 1e111d1 commit adf42c0

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@
2121
import com.lambda.event.EventFlow;
2222
import com.lambda.event.events.ClientEvent;
2323
import com.lambda.event.events.InventoryEvent;
24+
import com.lambda.event.events.PlayerEvent;
2425
import com.lambda.event.events.TickEvent;
2526
import com.lambda.module.modules.player.Interact;
2627
import net.minecraft.client.MinecraftClient;
2728
import net.minecraft.client.gui.screen.Screen;
2829
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
2930
import net.minecraft.client.network.ClientPlayerInteractionManager;
31+
import net.minecraft.util.hit.BlockHitResult;
32+
import net.minecraft.util.hit.HitResult;
3033
import org.jetbrains.annotations.Nullable;
3134
import org.spongepowered.asm.mixin.Mixin;
3235
import org.spongepowered.asm.mixin.Shadow;
@@ -41,6 +44,8 @@ public class MinecraftClientMixin {
4144
@Nullable
4245
public Screen currentScreen;
4346

47+
@Shadow @javax.annotation.Nullable public HitResult crosshairTarget;
48+
4449
@Inject(method = "tick", at = @At("HEAD"))
4550
void onTickPre(CallbackInfo ci) {
4651
EventFlow.post(new TickEvent.Pre());
@@ -74,6 +79,18 @@ private void onStartup(CallbackInfo ci) {
7479
EventFlow.post(new ClientEvent.Startup());
7580
}
7681

82+
@Inject(method = "handleBlockBreaking(Z)V", at = @At("HEAD"), cancellable = true)
83+
private void onHandleBlockBreaking(boolean breaking, CallbackInfo ci) {
84+
if (EventFlow.post(
85+
new PlayerEvent.Breaking.Handle(
86+
breaking,
87+
(BlockHitResult) crosshairTarget
88+
)
89+
).isCanceled()) {
90+
ci.cancel();
91+
}
92+
}
93+
7794
@Inject(method = "setScreen", at = @At("HEAD"))
7895
private void onScreenOpen(@Nullable Screen screen, CallbackInfo ci) {
7996
if (screen == null) return;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,14 @@ sealed class PlayerEvent {
9696
}
9797

9898
sealed class Breaking {
99+
/**
100+
* Represents block break handling triggered each tick
101+
*/
102+
data class Handle(
103+
val breaking: Boolean,
104+
val blockHitResult: BlockHitResult?
105+
) : ICancellable by Cancellable()
106+
99107
/**
100108
* Represents events triggered during a block breaking action
101109
*/

common/src/main/kotlin/com/lambda/module/modules/player/PacketMineTaskRewrite.kt

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ object PacketMineTaskRewrite : Module(
111111
private val blockQueue = ArrayDeque<BlockPos>()
112112

113113
init {
114-
listen<PlayerEvent.Breaking.Update> { event ->
114+
listen<PlayerEvent.Attack.Block> { event ->
115115
event.cancel()
116116

117117
val pos = event.pos
118-
if (breakTasks.any { it?.pos == event.pos }) return@listen
118+
if (breakTasks.any { it?.pos == pos }) return@listen
119119

120120
val blockState = pos.blockState(world)
121121
val bestTool = findBestAvailableTool(blockState)
@@ -141,12 +141,9 @@ object PacketMineTaskRewrite : Module(
141141
).run()
142142
}
143143

144-
listen<PlayerEvent.Attack.Block> { event ->
145-
event.cancel()
146-
}
147-
148-
listen<PlayerEvent.Breaking.Cancel> { event ->
149-
if (breakTasks.any { it != null }) event.cancel()
144+
listen<PlayerEvent.Breaking.Handle> { event ->
145+
if (breakTasks.any { it != null } && !event.breaking)
146+
event.cancel()
150147
}
151148

152149
onDisable {

0 commit comments

Comments
 (0)