Skip to content

Commit 3654ee9

Browse files
committed
swing
1 parent eb17f25 commit 3654ee9

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

common/src/main/kotlin/com/lambda/config/groups/BreakSettings.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class BreakSettings(
3131
override val breakThreshold by c.setting("Break Threshold", 1.0f, 0.1f..1.0f, 0.02f, "The break amount at which the block is considered broken") { vis() }
3232
override val doubleBreak by c.setting("Double Break", false, "Allows breaking two blocks at once") { vis() }
3333
override val breakDelay by c.setting("Break Delay", 5, 0..5, 1, "The delay between breaking blocks", " ticks") { vis() }
34+
override val swing by c.setting("Swing Mode", SwingMode.Constant, "The times at which to swing the players hand") { vis() }
35+
override val swingType by c.setting("Swing Type", SwingType.Vanilla, "The style of swing")
3436
override val sounds by c.setting("Sounds", true, "Plays the breaking sounds") { vis() }
3537
override val particles by c.setting("Particles", true, "Renders the breaking particles") { vis() }
3638
override val breakingTexture by c.setting("Breaking Overlay", true, "Overlays the breaking texture at its different stages") { vis() }

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ abstract class BreakConfig(
2828
abstract val breakThreshold: Float
2929
abstract val doubleBreak: Boolean
3030
abstract val breakDelay: Int
31+
abstract val swing: SwingMode
32+
abstract val swingType: SwingType
3133
abstract val sounds: Boolean
3234
abstract val particles: Boolean
3335
abstract val breakingTexture: Boolean
@@ -49,6 +51,19 @@ abstract class BreakConfig(
4951
Packet
5052
}
5153

54+
enum class SwingMode {
55+
Constant,
56+
StartAndEnd,
57+
Start,
58+
End
59+
}
60+
61+
enum class SwingType {
62+
Vanilla,
63+
Server,
64+
Client
65+
}
66+
5267
enum class BreakConfirmationMode {
5368
None,
5469
BreakThenAwait,

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ import com.lambda.util.BlockUtils.fluidState
3636
import com.lambda.util.Communication.info
3737
import com.lambda.util.Communication.warn
3838
import com.lambda.util.collections.LimitedDecayQueue
39+
import com.lambda.util.player.swingHandClient
3940
import net.minecraft.block.BlockState
4041
import net.minecraft.block.OperatorBlock
4142
import net.minecraft.client.sound.PositionedSoundInstance
4243
import net.minecraft.client.sound.SoundInstance
4344
import net.minecraft.client.world.ClientWorld
4445
import net.minecraft.entity.player.PlayerEntity
4546
import net.minecraft.item.ItemStack
47+
import net.minecraft.network.packet.c2s.play.HandSwingC2SPacket
4648
import net.minecraft.network.packet.c2s.play.PlayerActionC2SPacket
4749
import net.minecraft.sound.SoundCategory
4850
import net.minecraft.util.math.BlockPos
@@ -204,6 +206,7 @@ object BreakManager : RequestHandler<BreakRequest>() {
204206
info.nullify()
205207
return false
206208
}
209+
if (info.breakConfig.swing != BreakConfig.SwingMode.End) swingHand(info)
207210
return true
208211
}
209212

@@ -245,15 +248,18 @@ object BreakManager : RequestHandler<BreakRequest>() {
245248
)
246249
}
247250

251+
if (info.breakConfig.breakingTexture) {
252+
setBreakingTextureStage(info)
253+
}
254+
248255
if (progress >= info.getBreakThreshold()) {
249256
interaction.sendSequencedPacket(world) { sequence ->
250257
onBlockBreak(info)
251258
PlayerActionC2SPacket(PlayerActionC2SPacket.Action.STOP_DESTROY_BLOCK, ctx.expectedPos, hitResult.side, sequence)
252259
}
253-
}
254-
255-
if (info.breakConfig.breakingTexture) {
256-
setBreakingTextureStage(info)
260+
if (info.breakConfig.swing != BreakConfig.SwingMode.Start) swingHand(info)
261+
} else {
262+
if (info.breakConfig.swing == BreakConfig.SwingMode.Constant) swingHand(info)
257263
}
258264

259265
return true
@@ -366,6 +372,14 @@ object BreakManager : RequestHandler<BreakRequest>() {
366372
)
367373
}
368374

375+
private fun SafeContext.swingHand(info: BreakInfo) {
376+
when (info.breakConfig.swingType) {
377+
BreakConfig.SwingType.Vanilla -> player.swingHand(player.activeHand)
378+
BreakConfig.SwingType.Server -> connection.sendPacket(HandSwingC2SPacket(player.activeHand))
379+
BreakConfig.SwingType.Client -> swingHandClient(player.activeHand)
380+
}
381+
}
382+
369383
data class BreakInfo(
370384
val context: BreakContext,
371385
var type: BreakType,

common/src/main/kotlin/com/lambda/util/player/PlayerUtils.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import net.minecraft.client.network.ClientPlayerEntity
66
import net.minecraft.client.network.OtherClientPlayerEntity
77
import net.minecraft.client.network.PlayerListEntry
88
import net.minecraft.entity.player.PlayerEntity
9+
import net.minecraft.util.Hand
910

1011
fun SafeContext.copyPlayer(entity: ClientPlayerEntity) =
1112
ClientPlayerEntity(mc, world, mc.networkHandler, null, null, entity.isSneaking, entity.isSprinting).apply {
@@ -39,3 +40,11 @@ fun SafeContext.spawnFakePlayer(
3940

4041
return entity
4142
}
43+
44+
fun SafeContext.swingHandClient(hand: Hand) {
45+
if (!player.handSwinging || player.handSwingTicks >= player.handSwingDuration / 2 || player.handSwingTicks < 0) {
46+
player.handSwingTicks = -1
47+
player.handSwinging = true
48+
player.preferredHand = hand
49+
}
50+
}

0 commit comments

Comments
 (0)