Skip to content

Commit 4db79c6

Browse files
committed
automatic re-break
1 parent a018c9e commit 4db79c6

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakInfo.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ data class BreakInfo(
4343
var breakingTicks = 0
4444
var soundsCooldown = 0.0f
4545

46+
var pending = false
47+
4648
var vanillaInstantBreakable = false
4749
val reBreakable get() = !vanillaInstantBreakable && isPrimary
4850

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakManager.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ object BreakManager : RequestHandler<BreakRequest>(
455455
*/
456456
private fun BreakInfo.nullify() {
457457
type.nullify()
458-
if (!broken && !isReBreaking && !isRedundant) internalOnCancel()
458+
if (!broken && !pending && !isReBreaking && !isRedundant) internalOnCancel()
459459
}
460460

461461
/**
@@ -519,7 +519,9 @@ object BreakManager : RequestHandler<BreakRequest>(
519519
} ?: false
520520
}
521521
is ReBreakResult.ReBroke -> {
522+
info.type = ReBreak
522523
info.nullify()
524+
info.request.onReBreak?.invoke(info.context.expectedPos)
523525
return true
524526
}
525527
else -> {}

common/src/main/kotlin/com/lambda/interaction/request/breaking/BreakRequest.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ data class BreakRequest(
3939
val onCancel: ((BlockPos) -> Unit)? = null,
4040
val onBreak: ((BlockPos) -> Unit)? = null,
4141
val onItemDrop: ((ItemEntity) -> Unit)? = null,
42+
val onReBreakStart: ((BlockPos) -> Unit)? = null,
43+
val onReBreak: ((BlockPos) -> Unit)? = null,
4244
private val prio: Priority = 0
4345
) : Request(prio, build.breaking) {
4446
override val done: Boolean

common/src/main/kotlin/com/lambda/interaction/request/breaking/BrokenBlockHandler.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ object BrokenBlockHandler {
128128
* Adds the [info] to the [BrokenBlockHandler], and requesters, pending interaction collections.
129129
*/
130130
fun BreakInfo.startPending() {
131+
pending = true
131132
pendingBreaks.add(this)
132133
pendingInteractions.add(context)
133134
}

common/src/main/kotlin/com/lambda/interaction/request/breaking/ReBreakManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ object ReBreakManager {
5151
type = BreakType.ReBreak
5252
breaking = true
5353
}
54+
info.request.onReBreakStart?.invoke(info.context.expectedPos)
5455
}
5556

5657
fun clearReBreak() {

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.lambda.config.groups.BuildSettings
2121
import com.lambda.config.groups.HotbarSettings
2222
import com.lambda.config.groups.InteractionSettings
2323
import com.lambda.config.groups.InventorySettings
24+
import com.lambda.config.groups.ReBreakSettings
2425
import com.lambda.config.groups.RotationSettings
2526
import com.lambda.context.SafeContext
2627
import com.lambda.event.events.PlayerEvent
@@ -59,6 +60,7 @@ object PacketMine : Module(
5960
private var itemDrops = 0
6061

6162
private val breakingPositions = arrayOfNulls<BlockPos>(2)
63+
private var reBreakPos: BlockPos? = null
6264

6365
private var requestedThisTick = false
6466

@@ -67,6 +69,14 @@ object PacketMine : Module(
6769
requestedThisTick = false
6870
}
6971

72+
//ToDo: run on every tick stage
73+
listen<TickEvent.Pre> {
74+
val reBreakMode = breakConfig.reBreak.mode
75+
if (reBreakMode != ReBreakSettings.Mode.Auto && reBreakMode != ReBreakSettings.Mode.AutoConstant) return@listen
76+
val reBreak = reBreakPos ?: return@listen
77+
requestBreakManager(listOf(reBreak))
78+
}
79+
7080
listen<PlayerEvent.Attack.Block> { it.cancel() }
7181
listen<PlayerEvent.Breaking.Update> { event ->
7282
event.cancel()
@@ -76,6 +86,7 @@ object PacketMine : Module(
7686
}
7787
breakingPositions[0] = null
7888
sendBreakRequest(event.pos)
89+
requestedThisTick = true
7990
}
8091

8192
listen<TickEvent.Input.Post> {
@@ -89,18 +100,33 @@ object PacketMine : Module(
89100
requestPositions.add(pos)
90101
}
91102

103+
if (requestPositions.isNotEmpty()) requestBreakManager(requestPositions)
104+
}
105+
106+
private fun SafeContext.requestBreakManager(requestPositions: List<BlockPos>) {
92107
val request = BreakRequest(
93108
breakContexts(requestPositions), build, rotation, hotbar, pendingInteractions = pendingInteractionsList,
94-
onAccept = { breakingPositions[0] = it },
95-
onCancel = { nullifyBreakPos(it) },
96-
onBreak = { breaks++; nullifyBreakPos(it) },
109+
onAccept = {
110+
breakingPositions[0] = it
111+
reBreakPos = null
112+
},
113+
onCancel = { nullifyBreakPos(it, true) },
114+
onBreak = {
115+
breaks++
116+
nullifyBreakPos(it)
117+
},
118+
onReBreakStart = { reBreakPos = it },
119+
onReBreak = { reBreakPos = it },
97120
onItemDrop = { _ -> itemDrops++ }
98121
)
99122
breakConfig.request(request)
100-
requestedThisTick = true
101123
}
102124

103-
private fun nullifyBreakPos(pos: BlockPos) {
125+
private fun nullifyBreakPos(pos: BlockPos, includeReBreak: Boolean = false) {
126+
if (includeReBreak && pos == reBreakPos) {
127+
reBreakPos = null
128+
return
129+
}
104130
breakingPositions.forEachIndexed { index, breakPos ->
105131
if (breakPos == pos) {
106132
breakingPositions[index] = null

0 commit comments

Comments
 (0)