Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a2b6f64

Browse files
author
Doogie13
committedSep 19, 2022
Rewrite Flight -> Packet mode
1 parent 862ef05 commit a2b6f64

File tree

1 file changed

+250
-46
lines changed
  • src/main/kotlin/com/lambda/client/module/modules/movement

1 file changed

+250
-46
lines changed
 

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

+250-46
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
@@ -19,92 +18,246 @@ import net.minecraft.network.play.client.CPacketConfirmTeleport
1918
import net.minecraft.network.play.client.CPacketPlayer
2019
import net.minecraft.network.play.server.SPacketCloseWindow
2120
import net.minecraft.network.play.server.SPacketPlayerPosLook
21+
import net.minecraft.util.math.Vec3d
2222
import kotlin.math.cos
23+
import kotlin.math.floor
24+
import kotlin.math.hypot
2325
import kotlin.math.sin
2426

27+
2528
object Flight : Module(
2629
name = "Flight",
2730
description = "Makes the player fly",
2831
category = Category.MOVEMENT,
2932
modulePriority = 500
3033
) {
34+
// non packet
3135
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 })
36+
private val speed by setting("Speed", 1.0f, 0f..10f, 0.1f, { mode != FlightMode.PACKET })
37+
private val glideSpeed by setting("Glide Speed", 0.05, 0.0..0.3, 0.001, { mode != FlightMode.PACKET })
38+
39+
// packet
40+
private val packetMode by setting("Packet Mode", PacketMode.FAST, { mode == FlightMode.PACKET })
41+
private val bounds by setting("Packet Type", PacketType.NEGATIVE, { mode == FlightMode.PACKET })
42+
private val factor by setting("Bypass Factor", 1f, 0f..10f, .1f, { mode == FlightMode.PACKET })
43+
private val concealFactor by setting("Conceal Factor", 2f,0f..10f, .1f, { mode == FlightMode.PACKET })
44+
private val conceal by setting("Conceal", false, { mode == FlightMode.PACKET })
3645
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 })
4146

4247
private enum class FlightMode {
4348
PACKET, VANILLA, STATIC
4449
}
4550

51+
private enum class PacketType {
52+
POSITIVE, NEGATIVE, STRICT
53+
}
54+
4655
private enum class PacketMode {
47-
POSITIVE, NEGATIVE
56+
FAST, SETBACK
4857
}
4958

59+
private const val BASE_SPEED = .2873
60+
private const val CONCEAL_SPEED = .0624
61+
private const val SQRT_TWO_OVER_TWO = .707106781
62+
private const val ANTIKICK_AMOUNT = .03125
63+
64+
private val history = HashMap<Int, Vec3d>()
65+
private val filter = ArrayList<CPacketPlayer.Position>()
66+
private var tpID = -1
67+
private var ticksEnabled = 0
68+
5069
init {
5170
onDisable {
5271
runSafe {
72+
73+
tpID = -1
74+
ticksEnabled = 0
75+
player.noClip = false
76+
5377
player.capabilities?.apply {
5478
isFlying = false
5579
flySpeed = 0.05f
5680
}
81+
82+
}
83+
}
84+
85+
onEnable {
86+
runSafe {
87+
val position = CPacketPlayer.Position(.0, .0, .0, true)
88+
filter.add(position)
89+
connection.sendPacket(position)
5790
}
5891
}
5992

60-
safeListener<PlayerTravelEvent> {
93+
safeListener<PlayerMoveEvent> {
6194
when (mode) {
95+
96+
// uses the same concepts as https://gist.github.com/Doogie13/aa04c6a8eb496c1afdb9c675e2ebd91c
97+
// completely written from scratch, however
6298
FlightMode.PACKET -> {
63-
it.cancel()
6499

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
100+
player.noClip = true
101+
102+
// region Motion
103+
val concealing = world.collidesWithAnyBlock(player.entityBoundingBox) || conceal
104+
105+
var motionY: Double
106+
107+
var up = 0
108+
109+
// we must use else if to allow phasing
110+
if (mc.gameSettings.keyBindJump.isKeyDown)
111+
up++
112+
else if (mc.gameSettings.keyBindSneak.isKeyDown)
113+
up--
114+
115+
motionY =
116+
if (up == 0)
117+
.0
118+
else
119+
CONCEAL_SPEED * up.toDouble()
120+
121+
var motionXZ: Double = if (!MovementUtils.isInputting)
122+
.0
123+
else if (concealing)
124+
CONCEAL_SPEED
125+
else
126+
BASE_SPEED
127+
128+
if (motionY != .0 && motionXZ == BASE_SPEED)
129+
motionY = .0
130+
131+
if (motionXZ == CONCEAL_SPEED && motionY == CONCEAL_SPEED) {
132+
motionXZ *= SQRT_TWO_OVER_TWO
133+
motionY *= SQRT_TWO_OVER_TWO
80134
}
81135

82-
val posX = player.posX + (player.motionX * hShrinkAmount)
83-
val posY = player.posY + (player.motionY * vShrinkAmount)
84-
val posZ = player.posZ + (player.motionZ * hShrinkAmount)
136+
//endregion
137+
138+
//region packets
85139

86-
val invalidPacketOffset = when (packetMode) {
87-
PacketMode.POSITIVE -> 1000
88-
PacketMode.NEGATIVE -> -1000
140+
val calcFactor =
141+
if (hypot(motionXZ, motionY) < .0625)
142+
concealFactor
143+
else
144+
factor
145+
146+
var factorInt = floor(calcFactor).toInt()
147+
val diff = calcFactor - factorInt
148+
149+
if (++ticksEnabled % 10 < diff * 10)
150+
factorInt++
151+
152+
if (factorInt == 0) {
153+
player.setVelocity(.0, .0, .0)
154+
return@safeListener
89155
}
90156

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++))
157+
if (motionXZ == .0 && motionY == .0)
158+
factorInt = 1
159+
160+
val yaw = calcMoveYaw()
161+
162+
val baseX = -sin(yaw) * motionXZ
163+
val baseY = motionY
164+
val baseZ = cos(yaw) * motionXZ
165+
166+
var currentX = baseX
167+
var currentY = if (antiKick && ticksEnabled % 10 == 0) -ANTIKICK_AMOUNT else baseY
168+
var currentZ = baseZ
169+
170+
for (i in 1..(factorInt)) {
171+
172+
// should never happen
173+
if (i > 10)
174+
break
175+
176+
val moveVec = Vec3d(currentX, currentY, currentZ)
177+
178+
var yOffset: Double
179+
180+
//region bounds
181+
when (bounds) {
182+
183+
PacketType.STRICT -> {
184+
185+
var random = (Math.random() * 256) + 256
186+
187+
(random + player.posY > (if (player.dimension == -1) 127 else 255))
188+
random *= -1
189+
190+
yOffset = random
191+
192+
}
193+
194+
PacketType.POSITIVE -> {
195+
196+
yOffset = 1337.0
197+
198+
}
199+
200+
PacketType.NEGATIVE -> {
201+
202+
yOffset = -1337.0
203+
204+
}
205+
206+
}
207+
//endregion
208+
209+
//region sending
210+
211+
val boundsPacket = CPacketPlayer.Position(player.posX + moveVec.x, player.posY + moveVec.y + yOffset, player.posZ + moveVec.z, true)
212+
val movePacket = CPacketPlayer.Position(player.posX + moveVec.x, player.posY + moveVec.y, player.posZ + moveVec.z, true)
213+
214+
filter.add(movePacket)
215+
filter.add(boundsPacket)
216+
217+
connection.sendPacket(movePacket)
218+
connection.sendPacket(boundsPacket)
219+
220+
history[++tpID] = moveVec.add(player.positionVector)
221+
connection.sendPacket(CPacketConfirmTeleport(tpID))
222+
223+
currentX += baseX
224+
currentY += baseY
225+
currentZ += baseZ
226+
227+
//endregion
228+
95229
}
230+
231+
//endregion
232+
233+
if (packetMode == PacketMode.FAST)
234+
player.setVelocity(currentX - baseX, currentY - baseY, currentZ - baseZ)
235+
else
236+
player.setVelocity(.0, .0, .0)
237+
96238
}
239+
97240
FlightMode.STATIC -> {
98-
player.capabilities.isFlying = true
99-
player.capabilities.flySpeed = speed
100241

101-
player.motionX = 0.0
102-
player.motionY = -glideSpeed
103-
player.motionZ = 0.0
242+
var up = 0
243+
244+
if (mc.gameSettings.keyBindJump.isKeyDown)
245+
up++
246+
247+
if (mc.gameSettings.keyBindSneak.isKeyDown)
248+
up--
249+
250+
player.motionY = if (up == 0) -glideSpeed else speed * up.toDouble()
251+
252+
if (!MovementUtils.isInputting)
253+
return@safeListener
254+
255+
val yaw = calcMoveYaw()
256+
player.motionX -= sin(yaw) * speed
257+
player.motionZ += cos(yaw) * speed
104258

105-
if (mc.gameSettings.keyBindJump.isKeyDown) player.motionY += speed / 2.0f
106-
if (mc.gameSettings.keyBindSneak.isKeyDown) player.motionY -= speed / 2.0f
107259
}
260+
108261
FlightMode.VANILLA -> {
109262
player.capabilities.isFlying = true
110263
player.capabilities.flySpeed = speed / 11.11f
@@ -113,7 +266,9 @@ object Flight : Module(
113266
&& !mc.gameSettings.keyBindJump.isKeyDown
114267
&& !mc.gameSettings.keyBindSneak.isKeyDown) player.motionY = -glideSpeed
115268
}
269+
116270
}
271+
117272
}
118273

119274
listener<OnUpdateWalkingPlayerEvent> {
@@ -124,16 +279,65 @@ object Flight : Module(
124279
}
125280

126281
safeListener<PacketEvent.Receive> {
127-
if (mode != FlightMode.PACKET) return@safeListener
282+
283+
if (mode != FlightMode.PACKET)
284+
return@safeListener
285+
128286
when (it.packet) {
287+
129288
is SPacketPlayerPosLook -> {
289+
290+
val packet = it.packet
291+
val id = packet.teleportId
292+
293+
if (history.containsKey(id) && tpID != -1) {
294+
295+
val vec = history[id]
296+
297+
if (vec != null) {
298+
299+
if (vec.x == packet.x && vec.y == packet.y && vec.z == packet.z) {
300+
301+
if (packetMode != PacketMode.SETBACK)
302+
it.cancel()
303+
304+
history.remove(id)
305+
306+
player.connection.sendPacket(CPacketConfirmTeleport(id))
307+
308+
return@safeListener
309+
310+
}
311+
312+
}
313+
}
314+
130315
it.packet.playerPosLookYaw = player.rotationYaw
131316
it.packet.playerPosLookPitch = player.rotationPitch
317+
318+
player.connection.sendPacket(CPacketConfirmTeleport(id))
319+
320+
tpID = id
321+
132322
}
323+
133324
is SPacketCloseWindow -> {
134325
it.cancel()
135326
}
327+
136328
}
137329
}
330+
331+
safeListener<PacketEvent.Send> {
332+
333+
if (mode != FlightMode.PACKET || it.packet !is CPacketPlayer) return@safeListener
334+
335+
if (!filter.contains(it.packet))
336+
it.cancel()
337+
else
338+
filter.remove(it.packet)
339+
340+
}
341+
138342
}
139343
}

0 commit comments

Comments
 (0)
Please sign in to comment.