From 1b41667a4dcc370b69f373e393bcbf1f99842e1c Mon Sep 17 00:00:00 2001 From: rfresh2 <89827146+rfresh2@users.noreply.github.com> Date: Sun, 29 Jan 2023 22:51:42 -0800 Subject: [PATCH 1/2] SafeWalk Compatibility --- .../com/lambda/mixin/entity/MixinEntity.java | 23 ------ .../module/modules/movement/SafeWalk.kt | 75 +++++++++++++------ .../client/module/modules/player/Scaffold.kt | 6 -- 3 files changed, 54 insertions(+), 50 deletions(-) diff --git a/src/main/java/com/lambda/mixin/entity/MixinEntity.java b/src/main/java/com/lambda/mixin/entity/MixinEntity.java index ef23be53d..b276d1882 100644 --- a/src/main/java/com/lambda/mixin/entity/MixinEntity.java +++ b/src/main/java/com/lambda/mixin/entity/MixinEntity.java @@ -1,13 +1,10 @@ package com.lambda.mixin.entity; -import com.lambda.client.module.modules.movement.SafeWalk; import com.lambda.client.module.modules.movement.Velocity; import com.lambda.client.module.modules.player.Freecam; import com.lambda.client.module.modules.player.ViewLock; import net.minecraft.entity.Entity; -import net.minecraft.entity.MoverType; import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -15,31 +12,11 @@ @Mixin(value = Entity.class, priority = Integer.MAX_VALUE) public abstract class MixinEntity { - @Shadow private int entityId; - - private boolean modifiedSneaking = false; - @Inject(method = "applyEntityCollision", at = @At("HEAD"), cancellable = true) public void applyEntityCollisionHead(Entity entityIn, CallbackInfo ci) { Velocity.handleApplyEntityCollision((Entity) (Object) this, entityIn, ci); } - @Inject(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;isSneaking()Z", ordinal = 0, shift = At.Shift.BEFORE)) - public void moveInvokeIsSneakingPre(MoverType type, double x, double y, double z, CallbackInfo ci) { - if (SafeWalk.shouldSafewalk(this.entityId)) { - modifiedSneaking = true; - SafeWalk.setSneaking(true); - } - } - - @Inject(method = "move", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;isSneaking()Z", ordinal = 0, shift = At.Shift.AFTER)) - public void moveInvokeIsSneakingPost(MoverType type, double x, double y, double z, CallbackInfo ci) { - if (modifiedSneaking) { - modifiedSneaking = false; - SafeWalk.setSneaking(false); - } - } - // Makes the camera guy instead of original player turn around when we move mouse @Inject(method = "turn", at = @At("HEAD"), cancellable = true) public void turn(float yaw, float pitch, CallbackInfo ci) { diff --git a/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt b/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt index cfa2226fc..b731da409 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt @@ -1,41 +1,74 @@ package com.lambda.client.module.modules.movement +import com.lambda.client.event.events.PlayerMoveEvent import com.lambda.client.module.Category import com.lambda.client.module.Module import com.lambda.client.module.modules.player.Scaffold import com.lambda.client.util.BaritoneUtils import com.lambda.client.util.EntityUtils.flooredPosition -import com.lambda.client.util.Wrapper import com.lambda.client.util.math.VectorUtils.toVec3d import com.lambda.client.util.threads.runSafeR -import com.lambda.mixin.entity.MixinEntity +import com.lambda.client.util.threads.safeListener -/** - * @see MixinEntity.moveInvokeIsSneakingPre - * @see MixinEntity.moveInvokeIsSneakingPost - */ object SafeWalk : Module( name = "SafeWalk", description = "Keeps you from walking off edges", - category = Category.MOVEMENT + category = Category.MOVEMENT, + alwaysListening = true ) { - private val checkFallDist by setting("Check Fall Distance", true, description = "Check fall distance from edge") + private val checkFallDist by setting("Safe Fall Allowed", false, description = "Check fall distance from edge") init { - onToggle { - BaritoneUtils.settings?.assumeSafeWalk?.value = it - } - } + safeListener { event -> + if ((isEnabled || (Scaffold.isEnabled && Scaffold.safeWalk)) + && player.onGround + && !BaritoneUtils.isPathing + && if (checkFallDist) !isEdgeSafe else true) { + /** + * Code here is from net.minecraft.Entity::move + * Cannot do a mixin on this method's sneak section due to mixin compatibility issues with Future (and possibly other clients) + */ - @JvmStatic - fun shouldSafewalk(entityID: Int) = - (Wrapper.player?.let { !it.isSneaking && it.entityId == entityID } ?: false) - && (isEnabled || Scaffold.isEnabled && Scaffold.safeWalk) - && (!checkFallDist && !BaritoneUtils.isPathing || !isEdgeSafe) - - @JvmStatic - fun setSneaking(state: Boolean) { - Wrapper.player?.movementInput?.sneak = state + var x = event.x + var z = event.z + while (x != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(x, (-player.stepHeight).toDouble(), 0.0)).isEmpty()) { + if (x < 0.05 && x >= -0.05) { + x = 0.0 + } else if (x > 0.0) { + x -= 0.05 + } else { + x += 0.05 + } + } + while (z != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(0.0, (-player.stepHeight).toDouble(), z)).isEmpty()) { + if (z < 0.05 && z >= -0.05) { + z = 0.0 + } else if (z > 0.0) { + z -= 0.05 + } else { + z += 0.05 + } + } + while (x != 0.0 && z != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(x, (-player.stepHeight).toDouble(), z)).isEmpty()) { + if (x < 0.05 && x >= -0.05) { + x = 0.0 + } else if (x > 0.0) { + x -= 0.05 + } else { + x += 0.05 + } + if (z < 0.05 && z >= -0.05) { + z = 0.0 + } else if (z > 0.0) { + z -= 0.05 + } else { + z += 0.05 + } + } + event.x = x + event.z = z + } + } } private val isEdgeSafe: Boolean diff --git a/src/main/kotlin/com/lambda/client/module/modules/player/Scaffold.kt b/src/main/kotlin/com/lambda/client/module/modules/player/Scaffold.kt index 979610a49..4d4dbab86 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/player/Scaffold.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/player/Scaffold.kt @@ -12,7 +12,6 @@ import com.lambda.client.manager.managers.PlayerPacketManager.sendPlayerPacket import com.lambda.client.mixin.extension.syncCurrentPlayItem import com.lambda.client.module.Category import com.lambda.client.module.Module -import com.lambda.client.module.modules.player.Scaffold.isUsableItem import com.lambda.client.setting.settings.impl.collection.CollectionSetting import com.lambda.client.util.EntityUtils.flooredPosition import com.lambda.client.util.MovementUtils.speed @@ -27,7 +26,6 @@ import com.lambda.client.util.world.PlaceInfo import com.lambda.client.util.world.getNeighbour import com.lambda.client.util.world.isFullBox import com.lambda.client.util.world.placeBlock -import com.lambda.mixin.entity.MixinEntity import net.minecraft.block.Block import net.minecraft.block.state.IBlockState import net.minecraft.init.Blocks @@ -42,10 +40,6 @@ import net.minecraftforge.fml.common.gameevent.TickEvent import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent import java.util.concurrent.ConcurrentHashMap -/** - * @see MixinEntity.moveInvokeIsSneakingPre - * @see MixinEntity.moveInvokeIsSneakingPost - */ object Scaffold : Module( name = "Scaffold", description = "Places blocks under you", From f99fc16c0bfb33e2282b7b4397f7304f93c20c2e Mon Sep 17 00:00:00 2001 From: Constructor Date: Wed, 1 Feb 2023 04:07:48 +0100 Subject: [PATCH 2/2] Optimized mc code --- .../module/modules/movement/SafeWalk.kt | 54 ++++++++----------- 1 file changed, 22 insertions(+), 32 deletions(-) diff --git a/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt b/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt index b731da409..38d68e8da 100644 --- a/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt +++ b/src/main/kotlin/com/lambda/client/module/modules/movement/SafeWalk.kt @@ -31,46 +31,36 @@ object SafeWalk : Module( var x = event.x var z = event.z - while (x != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(x, (-player.stepHeight).toDouble(), 0.0)).isEmpty()) { - if (x < 0.05 && x >= -0.05) { - x = 0.0 - } else if (x > 0.0) { - x -= 0.05 - } else { - x += 0.05 - } - } - while (z != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(0.0, (-player.stepHeight).toDouble(), z)).isEmpty()) { - if (z < 0.05 && z >= -0.05) { - z = 0.0 - } else if (z > 0.0) { - z -= 0.05 - } else { - z += 0.05 - } + + var boundingBox = player.entityBoundingBox.offset(0.0, (-player.stepHeight).toDouble(), 0.0) + + while (x != 0.0 && world.getCollisionBoxes(player, boundingBox.offset(x, 0.0, 0.0)).isEmpty()) { + x = updateCoordinate(x) } - while (x != 0.0 && z != 0.0 && world.getCollisionBoxes(player, player.entityBoundingBox.offset(x, (-player.stepHeight).toDouble(), z)).isEmpty()) { - if (x < 0.05 && x >= -0.05) { - x = 0.0 - } else if (x > 0.0) { - x -= 0.05 - } else { - x += 0.05 - } - if (z < 0.05 && z >= -0.05) { - z = 0.0 - } else if (z > 0.0) { - z -= 0.05 - } else { - z += 0.05 - } + + boundingBox = boundingBox.offset(x, 0.0, 0.0) + + while (z != 0.0 && world.getCollisionBoxes(player, boundingBox.offset(0.0, 0.0, z)).isEmpty()) { + z = updateCoordinate(z) } + event.x = x event.z = z } } } + private fun updateCoordinate(coordinate: Double): Double { + return if (coordinate < 0.05 && coordinate >= -0.05) { + 0.0 + } else if (coordinate > 0.0) { + coordinate - 0.05 + } else { + coordinate + 0.05 + } + } + + private val isEdgeSafe: Boolean get() = runSafeR { val pos = player.flooredPosition.toVec3d(0.5, 0.0, 0.5)