Skip to content

Commit 8d37fe6

Browse files
rfresh2Avanatiker
andauthored
SafeWalk Compatibility (#480)
* SafeWalk Compatibility * Optimized mc code --------- Co-authored-by: Constructor <[email protected]>
1 parent b0bca0e commit 8d37fe6

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

src/main/java/com/lambda/mixin/entity/MixinEntity.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.lambda.mixin.entity;
22

3-
import com.lambda.client.module.modules.movement.SafeWalk;
43
import com.lambda.client.module.modules.movement.Step;
54
import com.lambda.client.module.modules.movement.Velocity;
65
import com.lambda.client.module.modules.player.Freecam;
@@ -22,30 +21,13 @@ public abstract class MixinEntity {
2221
@Shadow private int entityId;
2322

2423
@Shadow private AxisAlignedBB boundingBox;
25-
private boolean modifiedSneaking = false;
2624
float storedStepHeight = -1;
2725

2826
@Inject(method = "applyEntityCollision", at = @At("HEAD"), cancellable = true)
2927
public void applyEntityCollisionHead(Entity entityIn, CallbackInfo ci) {
3028
Velocity.handleApplyEntityCollision((Entity) (Object) this, entityIn, ci);
3129
}
3230

33-
@Inject(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;isSneaking()Z", ordinal = 0, shift = At.Shift.BEFORE))
34-
public void moveInvokeIsSneakingPre(MoverType type, double x, double y, double z, CallbackInfo ci) {
35-
if (SafeWalk.shouldSafewalk(this.entityId)) {
36-
modifiedSneaking = true;
37-
SafeWalk.setSneaking(true);
38-
}
39-
}
40-
41-
@Inject(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;isSneaking()Z", ordinal = 0, shift = At.Shift.AFTER))
42-
public void moveInvokeIsSneakingPost(MoverType type, double x, double y, double z, CallbackInfo ci) {
43-
if (modifiedSneaking) {
44-
modifiedSneaking = false;
45-
SafeWalk.setSneaking(false);
46-
}
47-
}
48-
4931
// Makes the camera guy instead of original player turn around when we move mouse
5032
@Inject(method = "turn", at = @At("HEAD"), cancellable = true)
5133
public void turn(float yaw, float pitch, CallbackInfo ci) {

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

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,66 @@
11
package com.lambda.client.module.modules.movement
22

3+
import com.lambda.client.event.events.PlayerMoveEvent
34
import com.lambda.client.module.Category
45
import com.lambda.client.module.Module
56
import com.lambda.client.module.modules.player.Scaffold
67
import com.lambda.client.util.BaritoneUtils
78
import com.lambda.client.util.EntityUtils.flooredPosition
8-
import com.lambda.client.util.Wrapper
99
import com.lambda.client.util.math.VectorUtils.toVec3d
1010
import com.lambda.client.util.threads.runSafeR
11-
import com.lambda.mixin.entity.MixinEntity
11+
import com.lambda.client.util.threads.safeListener
1212

13-
/**
14-
* @see MixinEntity.moveInvokeIsSneakingPre
15-
* @see MixinEntity.moveInvokeIsSneakingPost
16-
*/
1713
object SafeWalk : Module(
1814
name = "SafeWalk",
1915
description = "Keeps you from walking off edges",
20-
category = Category.MOVEMENT
16+
category = Category.MOVEMENT,
17+
alwaysListening = true
2118
) {
22-
private val checkFallDist by setting("Check Fall Distance", true, description = "Check fall distance from edge")
19+
private val checkFallDist by setting("Safe Fall Allowed", false, description = "Check fall distance from edge")
2320

2421
init {
25-
onToggle {
26-
BaritoneUtils.settings?.assumeSafeWalk?.value = it
22+
safeListener<PlayerMoveEvent> { event ->
23+
if ((isEnabled || (Scaffold.isEnabled && Scaffold.safeWalk))
24+
&& player.onGround
25+
&& !BaritoneUtils.isPathing
26+
&& if (checkFallDist) !isEdgeSafe else true) {
27+
/**
28+
* Code here is from net.minecraft.Entity::move
29+
* Cannot do a mixin on this method's sneak section due to mixin compatibility issues with Future (and possibly other clients)
30+
*/
31+
32+
var x = event.x
33+
var z = event.z
34+
35+
var boundingBox = player.entityBoundingBox.offset(0.0, (-player.stepHeight).toDouble(), 0.0)
36+
37+
while (x != 0.0 && world.getCollisionBoxes(player, boundingBox.offset(x, 0.0, 0.0)).isEmpty()) {
38+
x = updateCoordinate(x)
39+
}
40+
41+
boundingBox = boundingBox.offset(x, 0.0, 0.0)
42+
43+
while (z != 0.0 && world.getCollisionBoxes(player, boundingBox.offset(0.0, 0.0, z)).isEmpty()) {
44+
z = updateCoordinate(z)
45+
}
46+
47+
event.x = x
48+
event.z = z
49+
}
2750
}
2851
}
2952

30-
@JvmStatic
31-
fun shouldSafewalk(entityID: Int) =
32-
(Wrapper.player?.let { !it.isSneaking && it.entityId == entityID } ?: false)
33-
&& (isEnabled || Scaffold.isEnabled && Scaffold.safeWalk)
34-
&& (!checkFallDist && !BaritoneUtils.isPathing || !isEdgeSafe)
35-
36-
@JvmStatic
37-
fun setSneaking(state: Boolean) {
38-
Wrapper.player?.movementInput?.sneak = state
53+
private fun updateCoordinate(coordinate: Double): Double {
54+
return if (coordinate < 0.05 && coordinate >= -0.05) {
55+
0.0
56+
} else if (coordinate > 0.0) {
57+
coordinate - 0.05
58+
} else {
59+
coordinate + 0.05
60+
}
3961
}
4062

63+
4164
private val isEdgeSafe: Boolean
4265
get() = runSafeR {
4366
val pos = player.flooredPosition.toVec3d(0.5, 0.0, 0.5)

src/main/kotlin/com/lambda/client/module/modules/player/Scaffold.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import com.lambda.client.manager.managers.PlayerPacketManager.sendPlayerPacket
1212
import com.lambda.client.mixin.extension.syncCurrentPlayItem
1313
import com.lambda.client.module.Category
1414
import com.lambda.client.module.Module
15-
import com.lambda.client.module.modules.player.Scaffold.isUsableItem
1615
import com.lambda.client.setting.settings.impl.collection.CollectionSetting
1716
import com.lambda.client.util.EntityUtils.flooredPosition
1817
import com.lambda.client.util.MovementUtils.speed
@@ -27,7 +26,6 @@ import com.lambda.client.util.world.PlaceInfo
2726
import com.lambda.client.util.world.getNeighbour
2827
import com.lambda.client.util.world.isFullBox
2928
import com.lambda.client.util.world.placeBlock
30-
import com.lambda.mixin.entity.MixinEntity
3129
import net.minecraft.block.Block
3230
import net.minecraft.block.state.IBlockState
3331
import net.minecraft.init.Blocks
@@ -42,10 +40,6 @@ import net.minecraftforge.fml.common.gameevent.TickEvent
4240
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent
4341
import java.util.concurrent.ConcurrentHashMap
4442

45-
/**
46-
* @see MixinEntity.moveInvokeIsSneakingPre
47-
* @see MixinEntity.moveInvokeIsSneakingPost
48-
*/
4943
object Scaffold : Module(
5044
name = "Scaffold",
5145
description = "Places blocks under you",

0 commit comments

Comments
 (0)