Skip to content

Commit 11e9e7d

Browse files
committed
more general packet mine logic
1 parent 43eb8e0 commit 11e9e7d

File tree

2 files changed

+96
-26
lines changed

2 files changed

+96
-26
lines changed

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

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,45 +46,65 @@ object PacketMineTaskRewrite : Module(
4646
) {
4747
private val page by setting("Page", Page.General)
4848

49-
private val doubleBreak = setting(
49+
private val doubleBreak by setting(
5050
"Double Break",
5151
false,
5252
"Lets the player break two blocks at once"
5353
) { page == Page.General }
54-
private val breakThreshold = setting(
55-
"Break Threshold",
56-
0.7f,
57-
0.0f..1.1f,
58-
0.1f,
59-
"The time taken to break a block compared to vanilla speed (lower is faster)"
60-
) { page == Page.General }
61-
private val validateBreak = setting(
54+
private val validateBreak by setting(
6255
"Validate Break",
6356
false,
6457
"Awaits the servers response before confirming the block to be broken"
6558
) { page == Page.General }
66-
private val useInventory = setting(
59+
private val useInventory by setting(
6760
"Use Inventory",
6861
false,
6962
"Allows usage of tools within the players inventory"
7063
) { page == Page.General }
7164

72-
private var primaryBreakTask: PacketBreakInfo? = null
73-
private var secondaryBreakTask: PacketBreakInfo? = null
65+
private val reBreak by setting(
66+
"Re-Break",
67+
true,
68+
"Re-breaks the block if replaced again, keeping the progress from the first"
69+
) { page == Page.Rebreak }
70+
private val reBreakStyle by setting(
71+
"Re-Break Style",
72+
ReBreak.Standard,
73+
"Affects the speed and style of re-break"
74+
) { page == Page.Rebreak }
75+
76+
private val queueBlocks by setting(
77+
"Queue Blocks",
78+
false,
79+
"Adds blocks to a queue to break sequentially after the current breaking blocks are complete"
80+
) { page == Page.Queue }
81+
82+
private var breakTasks = arrayOfNulls<PacketBreakInfo>(2)
83+
private var primaryBreakTask: PacketBreakInfo?
84+
get() = breakTasks[0]
85+
set(value) { breakTasks[0] = value }
86+
private var secondaryBreakTask: PacketBreakInfo?
87+
get() = breakTasks[1]
88+
set(value) { breakTasks[1] = value }
89+
90+
private val blockQueue = ArrayDeque<BlockPos>()
7491

7592
init {
7693
listen<PlayerEvent.Attack.Block> { event ->
7794
event.cancel()
7895

7996
val pos = event.pos
80-
val vec3dPos = event.pos.vec3d
81-
val hitResult = BlockHitResult(vec3dPos, event.side, pos, false)
8297
val blockState = pos.blockState(world)
8398
val bestTool =
8499
findBestAvailableTool(blockState)?.select()?.itemStack
85100
?: player.mainHandStack
86101

87-
primaryBreakTask?.cancelBreak()
102+
if (doubleBreak && secondaryBreakTask == null) {
103+
secondaryBreakTask = primaryBreakTask
104+
} else {
105+
primaryBreakTask?.cancelBreak()
106+
}
107+
88108
primaryBreakTask = PacketBreakInfo(
89109
pos,
90110
blockState,
@@ -95,7 +115,11 @@ object PacketMineTaskRewrite : Module(
95115
}
96116

97117
onDisable {
98-
118+
breakTasks.forEach {
119+
it?.cancelBreak()
120+
}
121+
breakTasks = arrayOfNulls(2)
122+
blockQueue.clear()
99123
}
100124
}
101125

@@ -122,7 +146,14 @@ object PacketMineTaskRewrite : Module(
122146
instantBreak
123147
)
124148
packetBreakTask = PacketBreakBlock(
125-
breakContext
149+
breakContext,
150+
false,
151+
TaskFlowModule.rotation,
152+
TaskFlowModule.interact,
153+
validateBreak,
154+
Direction.entries.toSet(),
155+
TaskFlowModule.build.rotateForBreak,
156+
PacketBreakBlock.SwingHandStyle.Start,
126157
)
127158
}
128159

@@ -139,6 +170,13 @@ object PacketMineTaskRewrite : Module(
139170
private enum class Page {
140171
General,
141172
Rebreak,
173+
Queue,
142174
Render
143175
}
176+
177+
private enum class ReBreak {
178+
Standard,
179+
Automatic,
180+
FastAutomatic
181+
}
144182
}

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ import com.lambda.util.player.SlotUtils.clickSlot
3737
import com.lambda.util.player.SlotUtils.hotbarAndStorage
3838
import net.minecraft.block.BlockState
3939
import net.minecraft.entity.ItemEntity
40+
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
41+
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action
4042
import net.minecraft.screen.slot.SlotActionType
4143
import net.minecraft.util.math.BlockPos
4244
import net.minecraft.util.math.Direction
@@ -46,9 +48,10 @@ class PacketBreakBlock @Ta5kBuilder constructor(
4648
private val collectDrop: Boolean = false,
4749
private val rotation: RotationConfig = TaskFlowModule.rotation,
4850
private val interact: InteractionConfig = TaskFlowModule.interact,
51+
private val validateBreak: Boolean = true,
4952
private val sides: Set<Direction> = Direction.entries.toSet(),
5053
private val rotate: Boolean = TaskFlowModule.build.rotateForBreak,
51-
private val swingHand: Boolean = TaskFlowModule.interact.swingHand,
54+
private val swingHand: SwingHandStyle = SwingHandStyle.Start,
5255
) : Task<ItemEntity?>() {
5356
override val name get() = "Packet breaking ${ctx.result.blockPos.toShortString()}"
5457

@@ -62,6 +65,8 @@ class PacketBreakBlock @Ta5kBuilder constructor(
6265
private var state = State.BREAKING
6366
private var isValid = false
6467

68+
private var breakAmount = 0f
69+
6570
enum class State {
6671
BREAKING, COLLECTING
6772
}
@@ -75,7 +80,8 @@ class PacketBreakBlock @Ta5kBuilder constructor(
7580
beginState = blockState
7681

7782
if (!rotate || ctx.instantBreak) {
78-
hitBlock(ctx.result.side)
83+
stepDamage(ctx.result.side)
84+
if (swingHand != SwingHandStyle.End) player.swingHand(ctx.hand)
7985
}
8086
}
8187

@@ -114,7 +120,7 @@ class PacketBreakBlock @Ta5kBuilder constructor(
114120
} ?: BaritoneUtils.cancel()
115121

116122
if (isValid || !rotate || ctx.instantBreak) {
117-
hitBlock(ctx.result.side)
123+
stepDamage(ctx.result.side)
118124
}
119125

120126
if (done()) {
@@ -127,22 +133,48 @@ class PacketBreakBlock @Ta5kBuilder constructor(
127133

128134
// ToDo: Find out when the stack entity is filled with the item
129135
listen<WorldEvent.EntityUpdate> {
136+
val entity = it.entity
130137
if (collectDrop
131-
&& it.entity is ItemEntity
132-
&& it.entity.pos.isInRange(blockPos.toCenterPos(), 0.5)
138+
&& entity is ItemEntity
139+
&& entity.pos.isInRange(blockPos.toCenterPos(), 0.5)
133140
) {
134-
drop = it.entity
141+
drop = entity
135142
state = State.COLLECTING
136143
}
137144
}
138145
}
139146

140-
private fun SafeContext.done() = blockState.isAir && !collectDrop
147+
private fun SafeContext.done() =
148+
!collectDrop && (blockState.isAir || (!validateBreak && breakAmount >= 1.0f))
141149

142-
private fun SafeContext.hitBlock(side: Direction) {
150+
private fun SafeContext.stepDamage(side: Direction) {
143151
if (interaction.updateBlockBreakingProgress(blockPos, side)) {
144152
if (player.isCreative) interaction.blockBreakingCooldown = 0
145-
if (swingHand) player.swingHand(ctx.hand)
146153
}
147154
}
155+
156+
private fun SafeContext.startBreakPacket(pos: BlockPos, direction: Direction) =
157+
breakPacket(pos, direction, Action.START_DESTROY_BLOCK)
158+
159+
private fun SafeContext.stopBreakPacket(pos: BlockPos, direction: Direction) =
160+
breakPacket(pos, direction, Action.STOP_DESTROY_BLOCK)
161+
162+
private fun SafeContext.abortBreakPacket(pos: BlockPos, direction: Direction) =
163+
breakPacket(pos, direction, Action.ABORT_DESTROY_BLOCK)
164+
165+
private fun SafeContext.breakPacket(pos: BlockPos, direction: Direction, action: Action) =
166+
connection.sendPacket(
167+
PlayerActionC2SPacket(
168+
action,
169+
pos,
170+
direction
171+
)
172+
)
173+
174+
enum class SwingHandStyle {
175+
Constant,
176+
StartAndEnd,
177+
Start,
178+
End
179+
}
148180
}

0 commit comments

Comments
 (0)