Skip to content

Commit 9de3cc1

Browse files
Doogie13Avanatiker
andauthored
Refactor Flight (#381)
* Rewrite Flight -> Packet mode * Cleanup Co-authored-by: Constructor <fractalminds@protonmail.com>
1 parent 5ebd677 commit 9de3cc1

File tree

1 file changed

+209
-48
lines changed
  • src/main/kotlin/com/lambda/client/module/modules/movement

1 file changed

+209
-48
lines changed

src/main/kotlin/com/lambda/client/module/modules/movement/Flight.kt

+209-48
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package com.lambda.client.module.modules.movement
33
import com.lambda.client.event.Phase
44
import com.lambda.client.event.events.OnUpdateWalkingPlayerEvent
55
import com.lambda.client.event.events.PacketEvent
6-
import com.lambda.client.event.events.PlayerTravelEvent
6+
import com.lambda.client.event.events.PlayerMoveEvent
77
import com.lambda.client.event.listener.listener
8-
import com.lambda.client.manager.managers.PacketManager
98
import com.lambda.client.manager.managers.PlayerPacketManager.sendPlayerPacket
109
import com.lambda.client.mixin.extension.playerPosLookPitch
1110
import com.lambda.client.mixin.extension.playerPosLookYaw
@@ -14,96 +13,228 @@ import com.lambda.client.module.Module
1413
import com.lambda.client.util.MovementUtils
1514
import com.lambda.client.util.MovementUtils.calcMoveYaw
1615
import com.lambda.client.util.threads.runSafe
16+
import com.lambda.client.util.threads.runSafeR
1717
import com.lambda.client.util.threads.safeListener
1818
import net.minecraft.network.play.client.CPacketConfirmTeleport
1919
import net.minecraft.network.play.client.CPacketPlayer
2020
import net.minecraft.network.play.server.SPacketCloseWindow
2121
import net.minecraft.network.play.server.SPacketPlayerPosLook
22+
import net.minecraft.util.math.Vec3d
2223
import kotlin.math.cos
24+
import kotlin.math.floor
25+
import kotlin.math.hypot
2326
import kotlin.math.sin
2427

28+
2529
object Flight : Module(
2630
name = "Flight",
2731
description = "Makes the player fly",
2832
category = Category.MOVEMENT,
2933
modulePriority = 500
3034
) {
35+
// non packet
3136
private val mode by setting("Mode", FlightMode.PACKET)
32-
private val speed by setting("Speed", 1.0f, 0.0f..10.0f, 0.1f)
33-
private val glideSpeed by setting("Glide Speed", 0.05, 0.0..0.3, 0.001)
34-
private val packetMode by setting("Packet Mode", PacketMode.NEGATIVE, { mode == FlightMode.PACKET })
35-
private val upSpeed by setting("Up Speed", 0.0622, 0.0..0.3, 0.001, { mode == FlightMode.PACKET })
37+
private val speed by setting("Speed", 1.0f, 0f..10f, 0.1f, { mode != FlightMode.PACKET })
38+
private val glideSpeed by setting("Glide Speed", 0.05, 0.0..0.3, 0.001, { mode != FlightMode.PACKET })
39+
40+
// packet
41+
private val packetMode by setting("Packet Mode", PacketMode.FAST, { mode == FlightMode.PACKET })
42+
private val bounds by setting("Packet Type", PacketType.NEGATIVE, { mode == FlightMode.PACKET })
43+
private val factor by setting("Bypass Factor", 1f, 0f..10f, .1f, { mode == FlightMode.PACKET })
44+
private val concealFactor by setting("Conceal Factor", 2f, 0f..10f, .1f, { mode == FlightMode.PACKET })
45+
private val conceal by setting("Conceal", false, { mode == FlightMode.PACKET })
3646
private val antiKick by setting("Anti Kick", true, { mode == FlightMode.PACKET })
37-
private val antiKickSpeed by setting("Anti Kick Speed", 0.0622, 0.0..0.3, 0.001, { mode == FlightMode.PACKET && antiKick })
38-
private val antiKickDelay by setting("Anti Kick Delay", 14, 0..100, 1, { mode == FlightMode.PACKET && antiKick }, unit = " ticks")
39-
private val hShrinkAmount by setting("Horizontal Shrink Amount", 4.0, 1.0..10.0, 0.1, { mode == FlightMode.PACKET })
40-
private val vShrinkAmount by setting("Vertical Shrink Amount", 2.70, 1.0..10.0, 0.1, { mode == FlightMode.PACKET })
4147

4248
private enum class FlightMode {
4349
PACKET, VANILLA, STATIC
4450
}
4551

52+
private enum class PacketType {
53+
POSITIVE, NEGATIVE, STRICT
54+
}
55+
4656
private enum class PacketMode {
47-
POSITIVE, NEGATIVE
57+
FAST, SETBACK
4858
}
4959

60+
private const val BASE_SPEED = .2873
61+
private const val CONCEAL_SPEED = .0624
62+
private const val SQRT_TWO_OVER_TWO = .707106781
63+
private const val ANTIKICK_AMOUNT = .03125
64+
65+
private val history = HashMap<Int, Vec3d>()
66+
private val filter = ArrayList<CPacketPlayer.Position>()
67+
private var tpID = -1
68+
private var ticksEnabled = 0
69+
5070
init {
5171
onDisable {
5272
runSafe {
73+
tpID = -1
74+
ticksEnabled = 0
75+
player.noClip = false
76+
5377
player.capabilities?.apply {
5478
isFlying = false
5579
flySpeed = 0.05f
5680
}
5781
}
5882
}
5983

60-
safeListener<PlayerTravelEvent> {
84+
onEnable {
85+
runSafeR {
86+
val position = CPacketPlayer.Position(.0, .0, .0, true)
87+
filter.add(position)
88+
connection.sendPacket(position)
89+
} ?: disable()
90+
}
91+
92+
safeListener<PlayerMoveEvent> {
6193
when (mode) {
94+
// uses the same concepts as https://gist.github.com/Doogie13/aa04c6a8eb496c1afdb9c675e2ebd91c
95+
// completely written from scratch, however
6296
FlightMode.PACKET -> {
63-
it.cancel()
97+
player.noClip = true
6498

65-
player.motionY = if (mc.gameSettings.keyBindJump.isKeyDown xor mc.gameSettings.keyBindSneak.isKeyDown) {
66-
if (mc.gameSettings.keyBindJump.isKeyDown) {
67-
if (player.ticksExisted % antiKickDelay == 0 && antiKick) {
68-
-antiKickSpeed / vShrinkAmount
69-
} else {
70-
upSpeed / vShrinkAmount
71-
}
72-
} else (-upSpeed / vShrinkAmount)
73-
} else {
74-
if (MovementUtils.isInputting) {
75-
val yaw = calcMoveYaw()
76-
player.motionX = (-sin(yaw) * 0.2f * speed) / hShrinkAmount
77-
player.motionZ = (cos(yaw) * 0.2f * speed) / hShrinkAmount
78-
}
79-
-glideSpeed / vShrinkAmount
99+
// region Motion
100+
val concealing = world.collidesWithAnyBlock(player.entityBoundingBox) || conceal
101+
var motionY: Double
102+
var up = 0
103+
104+
// we must use else if to allow phasing
105+
if (mc.gameSettings.keyBindJump.isKeyDown)
106+
up++
107+
else if (mc.gameSettings.keyBindSneak.isKeyDown)
108+
up--
109+
110+
motionY = if (up == 0)
111+
.0
112+
else
113+
CONCEAL_SPEED * up.toDouble()
114+
115+
var motionXZ: Double = if (!MovementUtils.isInputting)
116+
.0
117+
else if (concealing)
118+
CONCEAL_SPEED
119+
else
120+
BASE_SPEED
121+
122+
if (motionY != .0 && motionXZ == BASE_SPEED)
123+
motionY = .0
124+
125+
if (motionXZ == CONCEAL_SPEED && motionY == CONCEAL_SPEED) {
126+
motionXZ *= SQRT_TWO_OVER_TWO
127+
motionY *= SQRT_TWO_OVER_TWO
80128
}
81129

82-
val posX = player.posX + (player.motionX * hShrinkAmount)
83-
val posY = player.posY + (player.motionY * vShrinkAmount)
84-
val posZ = player.posZ + (player.motionZ * hShrinkAmount)
130+
//endregion
131+
132+
//region packets
133+
134+
val calcFactor =
135+
if (hypot(motionXZ, motionY) < .0625)
136+
concealFactor
137+
else
138+
factor
139+
140+
var factorInt = floor(calcFactor).toInt()
141+
val diff = calcFactor - factorInt
85142

86-
val invalidPacketOffset = when (packetMode) {
87-
PacketMode.POSITIVE -> 1000
88-
PacketMode.NEGATIVE -> -1000
143+
if (++ticksEnabled % 10 < diff * 10)
144+
factorInt++
145+
146+
if (factorInt == 0) {
147+
player.setVelocity(.0, .0, .0)
148+
return@safeListener
89149
}
90150

91-
connection.sendPacket(CPacketPlayer.Position(posX, posY, posZ, false))
92-
connection.sendPacket(CPacketPlayer.Position(posX, player.posY + invalidPacketOffset, posZ, false))
93-
if (PacketManager.lastTeleportId != -1) {
94-
connection.sendPacket(CPacketConfirmTeleport(PacketManager.lastTeleportId++))
151+
if (motionXZ == .0 && motionY == .0)
152+
factorInt = 1
153+
154+
val yaw = calcMoveYaw()
155+
156+
val baseX = -sin(yaw) * motionXZ
157+
val baseY = motionY
158+
val baseZ = cos(yaw) * motionXZ
159+
160+
var currentX = baseX
161+
var currentY = if (antiKick && ticksEnabled % 10 == 0) -ANTIKICK_AMOUNT else baseY
162+
var currentZ = baseZ
163+
164+
for (i in 1..factorInt) {
165+
// should never happen
166+
if (i > 10)
167+
break
168+
169+
val moveVec = Vec3d(currentX, currentY, currentZ)
170+
171+
var yOffset: Double
172+
173+
//region bounds
174+
when (bounds) {
175+
PacketType.STRICT -> {
176+
var random = (Math.random() * 256) + 256
177+
178+
(random + player.posY > (if (player.dimension == -1) 127 else 255))
179+
random *= -1
180+
181+
yOffset = random
182+
}
183+
PacketType.POSITIVE -> {
184+
yOffset = 1337.0
185+
}
186+
PacketType.NEGATIVE -> {
187+
yOffset = -1337.0
188+
}
189+
}
190+
//endregion
191+
192+
//region sending
193+
194+
val boundsPacket = CPacketPlayer.Position(player.posX + moveVec.x, player.posY + moveVec.y + yOffset, player.posZ + moveVec.z, true)
195+
val movePacket = CPacketPlayer.Position(player.posX + moveVec.x, player.posY + moveVec.y, player.posZ + moveVec.z, true)
196+
197+
filter.add(movePacket)
198+
filter.add(boundsPacket)
199+
200+
connection.sendPacket(movePacket)
201+
connection.sendPacket(boundsPacket)
202+
203+
history[++tpID] = moveVec.add(player.positionVector)
204+
connection.sendPacket(CPacketConfirmTeleport(tpID))
205+
206+
currentX += baseX
207+
currentY += baseY
208+
currentZ += baseZ
209+
210+
//endregion
211+
95212
}
213+
214+
//endregion
215+
216+
if (packetMode == PacketMode.FAST)
217+
player.setVelocity(currentX - baseX, currentY - baseY, currentZ - baseZ)
218+
else
219+
player.setVelocity(.0, .0, .0)
220+
96221
}
97222
FlightMode.STATIC -> {
98-
player.capabilities.isFlying = true
99-
player.capabilities.flySpeed = speed
223+
var up = 0
224+
225+
if (mc.gameSettings.keyBindJump.isKeyDown) up++
226+
227+
if (mc.gameSettings.keyBindSneak.isKeyDown) up--
100228

101-
player.motionX = 0.0
102-
player.motionY = -glideSpeed
103-
player.motionZ = 0.0
229+
player.motionY = if (up == 0) -glideSpeed else speed * up.toDouble()
230+
231+
if (!MovementUtils.isInputting)
232+
return@safeListener
233+
234+
val yaw = calcMoveYaw()
235+
player.motionX -= sin(yaw) * speed
236+
player.motionZ += cos(yaw) * speed
104237

105-
if (mc.gameSettings.keyBindJump.isKeyDown) player.motionY += speed / 2.0f
106-
if (mc.gameSettings.keyBindSneak.isKeyDown) player.motionY -= speed / 2.0f
107238
}
108239
FlightMode.VANILLA -> {
109240
player.capabilities.isFlying = true
@@ -125,15 +256,45 @@ object Flight : Module(
125256

126257
safeListener<PacketEvent.Receive> {
127258
if (mode != FlightMode.PACKET) return@safeListener
128-
when (it.packet) {
259+
260+
when (val packet = it.packet) {
129261
is SPacketPlayerPosLook -> {
130-
it.packet.playerPosLookYaw = player.rotationYaw
131-
it.packet.playerPosLookPitch = player.rotationPitch
262+
val id = packet.teleportId
263+
264+
if (history.containsKey(packet.teleportId) && tpID != -1) {
265+
history[id]?.let { vec ->
266+
if (vec.x == packet.x && vec.y == packet.y && vec.z == packet.z) {
267+
if (packetMode != PacketMode.SETBACK)
268+
it.cancel()
269+
270+
history.remove(id)
271+
player.connection.sendPacket(CPacketConfirmTeleport(id))
272+
return@safeListener
273+
}
274+
}
275+
}
276+
277+
packet.playerPosLookYaw = player.rotationYaw
278+
packet.playerPosLookPitch = player.rotationPitch
279+
280+
player.connection.sendPacket(CPacketConfirmTeleport(id))
281+
282+
tpID = id
132283
}
133284
is SPacketCloseWindow -> {
134285
it.cancel()
135286
}
136287
}
137288
}
289+
290+
safeListener<PacketEvent.Send> {
291+
if (mode != FlightMode.PACKET || it.packet !is CPacketPlayer) return@safeListener
292+
293+
if (!filter.contains(it.packet))
294+
it.cancel()
295+
else
296+
filter.remove(it.packet)
297+
298+
}
138299
}
139300
}

0 commit comments

Comments
 (0)