@@ -37,6 +37,8 @@ import com.lambda.util.player.SlotUtils.clickSlot
3737import com.lambda.util.player.SlotUtils.hotbarAndStorage
3838import net.minecraft.block.BlockState
3939import net.minecraft.entity.ItemEntity
40+ import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
41+ import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket.Action
4042import net.minecraft.screen.slot.SlotActionType
4143import net.minecraft.util.math.BlockPos
4244import 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