Skip to content

Commit 11147d2

Browse files
committed
decoupled grouped area breaks and queue sorting
1 parent 22abb0f commit 11147d2

File tree

1 file changed

+42
-28
lines changed
  • common/src/main/kotlin/com/lambda/module/modules/player

1 file changed

+42
-28
lines changed

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

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.lambda.event.events.PlayerEvent
2727
import com.lambda.event.events.TickEvent
2828
import com.lambda.event.listener.SafeListener.Companion.listen
2929
import com.lambda.interaction.construction.blueprint.StaticBlueprint.Companion.toBlueprint
30+
import com.lambda.interaction.construction.context.BreakContext
3031
import com.lambda.interaction.construction.context.BuildContext
3132
import com.lambda.interaction.construction.result.BreakResult
3233
import com.lambda.interaction.construction.simulation.BuildSimulator.simulate
@@ -38,7 +39,6 @@ import com.lambda.util.BlockUtils.blockState
3839
import com.lambda.util.math.distSq
3940
import com.lambda.util.world.raycast.InteractionMask
4041
import net.minecraft.util.math.BlockPos
41-
import java.util.*
4242
import java.util.concurrent.ConcurrentLinkedQueue
4343

4444
object PacketMine : Module(
@@ -68,12 +68,12 @@ object PacketMine : Module(
6868
private var itemDrops = 0
6969

7070
private val breakPositions = arrayOfNulls<BlockPos>(2)
71-
private val queuePositions = LinkedList<BlockPos>()
71+
private val queuePositions = LinkedHashSet<MutableCollection<BlockPos>>()
7272
private val queueSorted
7373
get() = when (queueOrder) {
7474
QueueOrder.Standard -> queuePositions
75-
QueueOrder.Reversed -> queuePositions.asReversed()
76-
}
75+
QueueOrder.Reversed -> queuePositions.reversed()
76+
}.flatten()
7777

7878
private var reBreakPos: BlockPos? = null
7979

@@ -95,29 +95,29 @@ object PacketMine : Module(
9595
listen<PlayerEvent.Breaking.Update> { event ->
9696
event.cancel()
9797
val pos = event.pos
98-
val positions = if (breakRadius > 0) {
99-
arrayListOf(pos).apply {
100-
BlockPos.iterateOutwards(pos, breakRadius, breakRadius, breakRadius).forEach { blockPos ->
101-
if (blockPos distSq pos <= breakRadius * breakRadius && (!flatten || blockPos.y >= player.blockPos.y)) {
102-
add(blockPos.toImmutable())
103-
}
98+
val positions = mutableListOf(pos).apply {
99+
if (breakRadius <= 0) return@apply
100+
BlockPos.iterateOutwards(pos, breakRadius, breakRadius, breakRadius).forEach { blockPos ->
101+
if (blockPos distSq pos <= (breakRadius * breakRadius) && (!flatten || blockPos.y >= player.blockPos.y)) {
102+
add(blockPos.toImmutable())
104103
}
105104
}
106-
} else {
107-
listOf(pos)
108105
}
109-
if ((breakPositions + queuePositions).any { pending -> positions.any { it == pending } }) return@listen
106+
positions.removeIf { breakPos ->
107+
breakPositions.any { it == breakPos }
108+
}
109+
if (positions.isEmpty()) return@listen
110110
val activeBreaking = if (queue) {
111-
queuePositions.addAll(positions)
111+
queuePositions.addLast(positions)
112112
breakPositions.toList() + queueSorted
113113
} else {
114114
queuePositions.clear()
115-
queuePositions.addAll(positions)
116-
queuePositions + if (breakConfig.doubleBreak) {
115+
queuePositions.addLast(positions)
116+
queuePositions.flatten() + if (breakConfig.doubleBreak) {
117117
breakPositions[1] ?: breakPositions[0]
118118
} else null
119119
}
120-
requestBreakManager(activeBreaking.toList())
120+
requestBreakManager(activeBreaking)
121121
attackedThisTick = true
122122
}
123123

@@ -138,13 +138,11 @@ object PacketMine : Module(
138138
if (requestPositions.isEmpty()) return
139139
val breakContexts = breakContexts(requestPositions)
140140
if (!reBreaking) {
141-
queuePositions.removeIf { queuePos ->
142-
breakContexts.none { it.expectedPos == queuePos }
143-
}
141+
queuePositions.retainAllPositions(breakContexts)
144142
}
145143
val request = BreakRequest(
146144
breakContexts, build, rotation, hotbar, pendingInteractions = pendingInteractionsList,
147-
onAccept = { queuePositions.remove(it); addBreak(it) },
145+
onAccept = { queuePositions.removePos(it); addBreak(it) },
148146
onCancel = { removeBreak(it, true) },
149147
onBreak = { removeBreak(it); breaks++ },
150148
onReBreakStart = { reBreakPos = it },
@@ -159,13 +157,7 @@ object PacketMine : Module(
159157
.filterNotNull()
160158
.associateWith { TargetState.State(blockState(it).fluidState.blockState) }
161159
.toBlueprint()
162-
.simulate(
163-
player.eyePos,
164-
interact = interact,
165-
rotation = rotation,
166-
inventory = inventory,
167-
build = build
168-
)
160+
.simulate(player.eyePos, interact, rotation, inventory, build)
169161
.filterIsInstance<BreakResult.Break>()
170162
.map { it.context }
171163

@@ -188,6 +180,28 @@ object PacketMine : Module(
188180
}
189181
}
190182

183+
private fun LinkedHashSet<MutableCollection<BlockPos>>.removePos(element: BlockPos): Boolean {
184+
var anyRemoved = false
185+
removeIf {
186+
val removed = it.remove(element)
187+
anyRemoved = anyRemoved or removed
188+
return@removeIf removed && it.isEmpty()
189+
}
190+
return anyRemoved
191+
}
192+
193+
private fun LinkedHashSet<MutableCollection<BlockPos>>.retainAllPositions(positions: Collection<BreakContext>): Boolean {
194+
var modified = false
195+
forEach {
196+
modified = modified or it.retainAll { pos ->
197+
positions.any { retain ->
198+
retain.expectedPos == pos
199+
}
200+
}
201+
}
202+
return modified
203+
}
204+
191205
enum class Page {
192206
Build, Rotation, Interaction, Inventory, Hotbar
193207
}

0 commit comments

Comments
 (0)