Skip to content

Commit 0b94661

Browse files
committed
rebreak logic
1 parent c5d36c7 commit 0b94661

File tree

2 files changed

+62
-9
lines changed

2 files changed

+62
-9
lines changed

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

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

1818
package com.lambda.module.modules.player
1919

20+
import com.lambda.context.SafeContext
2021
import com.lambda.event.events.PlayerEvent
22+
import com.lambda.event.events.TickEvent
2123
import com.lambda.event.listener.SafeListener.Companion.listen
2224
import com.lambda.interaction.RotationManager
2325
import com.lambda.interaction.construction.context.BreakContext
@@ -34,6 +36,7 @@ import com.lambda.util.BlockUtils.instantBreakable
3436
import com.lambda.util.math.VecUtils.vec3d
3537
import net.minecraft.block.BlockState
3638
import net.minecraft.item.Item
39+
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
3740
import net.minecraft.util.Hand
3841
import net.minecraft.util.hit.BlockHitResult
3942
import net.minecraft.util.hit.HitResult
@@ -89,7 +92,7 @@ object PacketMineTaskRewrite : Module(
8992
"Re-Break Style",
9093
ReBreak.Standard,
9194
"Affects the speed and style of re-break"
92-
) { page == Page.Rebreak }
95+
) { page == Page.Rebreak && reBreak }
9396

9497
private val queueBlocks by setting(
9598
"Queue Blocks",
@@ -105,13 +108,23 @@ object PacketMineTaskRewrite : Module(
105108
get() = breakTasks[1]
106109
set(value) { breakTasks[1] = value }
107110

111+
private var reBreaking: PacketBreakInfo? = null
112+
108113
private val blockQueue = ArrayDeque<BlockPos>()
109114

110115
init {
111116
listen<PlayerEvent.Attack.Block> { event ->
112117
event.cancel()
113118

114119
val pos = event.pos
120+
121+
reBreaking?.let { ctx ->
122+
if (pos == ctx.pos) {
123+
reBreakBlock(ctx, event.side)
124+
return@listen
125+
}
126+
}
127+
115128
if (breakTasks.any { it?.pos == pos }) return@listen
116129

117130
val blockState = pos.blockState(world)
@@ -136,8 +149,7 @@ object PacketMineTaskRewrite : Module(
136149
blockState,
137150
pos,
138151
bestTool?.select()?.itemStack ?: player.mainHandStack
139-
),
140-
BreakType.Primary
152+
)
141153
).run()
142154
}
143155

@@ -146,26 +158,58 @@ object PacketMineTaskRewrite : Module(
146158
event.cancel()
147159
}
148160

161+
listen<TickEvent.Pre> {
162+
if (reBreakStyle == ReBreak.Standard) return@listen
163+
164+
reBreaking?.let { ctx ->
165+
if (reBreakStyle == ReBreak.FastAutomatic) {
166+
reBreakBlock(ctx, ctx.getSide())
167+
return@listen
168+
}
169+
170+
val pos = ctx.pos
171+
val hardness = pos.blockState(world).getHardness(world, pos)
172+
if (hardness == -1.0f || hardness == 600.0f) return@listen
173+
174+
reBreakBlock(ctx, ctx.getSide())
175+
}
176+
}
177+
149178
onDisable {
150179
breakTasks.forEach {
151180
it?.cancelBreak()
152181
}
153182
breakTasks = arrayOfNulls(2)
183+
reBreaking = null
154184
blockQueue.clear()
155185
}
156186
}
157187

188+
private fun SafeContext.reBreakBlock(ctx: PacketBreakInfo, side: Direction) {
189+
val pos = ctx.pos
190+
interaction.sendSequencedPacket(world) {
191+
PlayerActionC2SPacket(
192+
PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK,
193+
pos,
194+
side
195+
)
196+
}
197+
if (!breakConfirmation) {
198+
interaction.breakBlock(pos)
199+
}
200+
}
201+
158202
private class PacketBreakInfo(
159203
val pos: BlockPos,
160-
val blockState: BlockState,
161-
val hand: Hand,
162-
val side: Direction,
204+
blockState: BlockState,
205+
hand: Hand,
206+
side: Direction,
163207
bestTool: Item?,
164208
instantBreak: Boolean,
165-
var breakType: BreakType
166209
) {
167210
private val breakContext: BreakContext
168211
private val packetBreakTask: PacketBreakBlock
212+
var breakType = BreakType.Primary
169213

170214
init {
171215
val vec3dPos = pos.vec3d
@@ -188,10 +232,15 @@ object PacketMineTaskRewrite : Module(
188232
obeyInteractionDelays = interactionDelay
189233
).finally {
190234
breakTasks[breakType.index] = null
235+
if (breakType == BreakType.Primary) {
236+
if (!reBreak) return@finally
237+
reBreaking = this@PacketBreakInfo
238+
}
191239
} as PacketBreakBlock
192240
}
193241

194242
fun run(): PacketBreakInfo {
243+
reBreaking = null
195244
packetBreakTask.run()
196245
return this
197246
}
@@ -201,6 +250,8 @@ object PacketMineTaskRewrite : Module(
201250
breakType = BreakType.Secondary
202251
}
203252

253+
fun getSide() = packetBreakTask.getSide()
254+
204255
fun cancelBreak() = packetBreakTask.cancel()
205256
}
206257

common/src/main/kotlin/com/lambda/task/tasks/PacketBreakBlock.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ class PacketBreakBlock @Ta5kBuilder constructor(
235235
*/
236236
private fun SafeContext.updateBlockBreakingProgress(): Boolean {
237237
if (obeyInteractionDelays && interaction.blockBreakingCooldown > 0) {
238-
interaction.blockBreakingCooldown--;
239-
return true;
238+
interaction.blockBreakingCooldown--
239+
return true
240240
}
241241

242242
if (interaction.currentGameMode.isCreative && world.worldBorder.contains(blockPos)) {
@@ -424,6 +424,8 @@ class PacketBreakBlock @Ta5kBuilder constructor(
424424
breakThreshold = 1.0f
425425
}
426426

427+
fun getSide() = side
428+
427429
enum class SwingHandStyle {
428430
Constant,
429431
StartAndEnd,

0 commit comments

Comments
 (0)