Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import com.lambda.Lambda;
import com.lambda.event.EventFlow;
import com.lambda.event.events.ClientEvent;
import com.lambda.event.events.ScreenEvent;
import com.lambda.event.events.ScreenHandlerEvent;
import com.lambda.event.events.TickEvent;
import com.lambda.interaction.RotationManager;
import com.lambda.event.events.*;
import com.lambda.module.modules.player.Interact;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
Expand All @@ -27,7 +23,6 @@ public class MinecraftClientMixin {
@Inject(method = "tick", at = @At("HEAD"))
void onTickPre(CallbackInfo ci) {
EventFlow.post(new TickEvent.Pre());
RotationManager.update();
}

@Inject(method = "tick", at = @At("RETURN"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Objects;

Expand Down Expand Up @@ -67,11 +68,22 @@ boolean isSprinting(ClientPlayerEntity entity) {
return EventFlow.post(new MovementEvent.Sprint(entity.isSprinting())).getSprint();
}

@Inject(method = "isSneaking", at = @At(value = "HEAD"), cancellable = true)
void redirectSneaking(CallbackInfoReturnable<Boolean> cir) {
ClientPlayerEntity self = (ClientPlayerEntity) (Object) this;
if (self != Lambda.getMc().player) return;

if (self.input == null) return;
cir.setReturnValue(EventFlow.post(new MovementEvent.Sneak(self.input.sneaking)).getSneak());
}

@Inject(method = "sendMovementPackets", at = @At(value = "HEAD"), cancellable = true)
void sendBegin(CallbackInfo ci) {
ci.cancel();
PlayerPacketManager.sendPlayerPackets();
autoJumpEnabled = Lambda.getMc().options.getAutoJump().getValue();

RotationManager.update();
}

@Inject(method = "tick", at = @At(value = "HEAD"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class InteractionSettings(
vis: () -> Boolean = { true },
) : InteractionConfig {
override val reach by c.setting("Reach", defaultReach, 0.1..10.0, 0.1, "Players reach / range", " blocks", vis)
override val useRayCast by c.setting("Raycast", false, "Verify hit vector with ray casting (for very strict ACs)", vis)
override val resolution by c.setting("Resolution", 10, 1..30, 1, "How many raycast checks per surface (will be squared)") { vis() && useRayCast }
override val useRayCast by c.setting("Raycast", true, "Verify hit vector with ray casting (for very strict ACs)", vis)
override val resolution by c.setting("Resolution", 20, 1..40, 1, "How many raycast checks per surface (will be squared)") { vis() && useRayCast }
override val swingHand by c.setting("Swing Hand", true, "Swing hand on interactions", vis)
}
2 changes: 2 additions & 0 deletions common/src/main/kotlin/com/lambda/config/groups/Targeting.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.lambda.util.math.VecUtils.distSq
import com.lambda.util.world.entitySearch
import net.minecraft.client.network.ClientPlayerEntity
import net.minecraft.entity.LivingEntity
import net.minecraft.entity.decoration.ArmorStandEntity
import net.minecraft.entity.mob.MobEntity
import net.minecraft.entity.passive.PassiveEntity

Expand Down Expand Up @@ -89,6 +90,7 @@ abstract class Targeting(
!players && entity.isPlayer -> false
!animals && entity is PassiveEntity -> false
!hostiles && entity is MobEntity -> false
entity is ArmorStandEntity -> false

!invisible && entity.isInvisibleTo(player) -> false
!dead && entity.isDead -> false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ abstract class MovementEvent : Event {
) : MovementEvent()

class Sprint(var sprint: Boolean) : MovementEvent()
class Sneak(var sneak: Boolean) : MovementEvent()

class ClipAtLedge(
var clip: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ abstract class PlayerPacketEvent : Event {
var isSneaking: Boolean,
) : PlayerPacketEvent(), ICancellable by Cancellable()

class Post(
class Post : PlayerPacketEvent()

class Send(
val packet: PlayerMoveC2SPacket,
) : PlayerPacketEvent(), ICancellable by Cancellable()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.lambda.core.Loadable
import com.lambda.event.EventFlow.post
import com.lambda.event.EventFlow.postChecked
import com.lambda.event.events.PlayerPacketEvent
import com.lambda.interaction.rotation.Rotation
import com.lambda.threading.runSafe
import com.lambda.util.collections.LimitedOrderedSet
import com.lambda.util.math.VecUtils.approximate
Expand All @@ -15,9 +16,17 @@ import com.lambda.util.extension.component2
import com.lambda.util.extension.component3
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket
import net.minecraft.network.packet.c2s.play.PlayerMoveC2SPacket.*
import net.minecraft.util.math.Vec3d

object PlayerPacketManager : Loadable {
val configurations = LimitedOrderedSet<PlayerPacketEvent.Pre>(100)

var lastPosition = Vec3d.ZERO
var lastRotation = Rotation.ZERO
var lastSprint = false
var lastSneak = false
var lastOnGround = false

private var sendTicks = 0

@JvmStatic
Expand All @@ -36,11 +45,10 @@ object PlayerPacketManager : Loadable {
}

private fun SafeContext.updatePlayerPackets(new: PlayerPacketEvent.Pre) {
val previous = configurations.lastOrNull() ?: new
configurations.add(new)

reportSprint(previous, new)
reportSneak(previous, new)
reportSprint(lastSprint, new.isSprinting)
reportSneak(lastSneak, new.isSneaking)

if (mc.cameraEntity != player) return

Expand All @@ -63,9 +71,9 @@ object PlayerPacketManager : Loadable {
return
}

val updatePosition = position.approximate(previous.position, 2.0E-4) || ++sendTicks >= 20
val updatePosition = position.approximate(lastPosition, 2.0E-4) || ++sendTicks >= 20
// has to be different in float precision
val updateRotation = !rotation.equalFloat(previous.rotation)
val updateRotation = !rotation.equalFloat(lastRotation)

val (x, y, z) = position

Expand All @@ -82,46 +90,57 @@ object PlayerPacketManager : Loadable {
LookAndOnGround(yaw, pitch, onGround)
}

previous.onGround != onGround -> {
lastOnGround != onGround -> {
OnGroundOnly(onGround)
}

else -> null
}

if (updatePosition) {
sendTicks = 0
}

packet?.let {
PlayerPacketEvent.Post(it).postChecked {
PlayerPacketEvent.Send(it).postChecked {
connection.sendPacket(this.packet)

if (updatePosition) {
sendTicks = 0
lastPosition = position
}

if (updateRotation) {
lastRotation = rotation
}

lastOnGround = onGround
}
}

PlayerPacketEvent.Post().post()
}

private fun SafeContext.reportSprint(previous: PlayerPacketEvent.Pre, new: PlayerPacketEvent.Pre) {
if (previous.isSprinting == new.isSprinting) return
fun SafeContext.reportSprint(previous: Boolean, new: Boolean) {
if (previous == new) return

val state = if (new.isSprinting) {
val state = if (new) {
ClientCommandC2SPacket.Mode.START_SPRINTING
} else {
ClientCommandC2SPacket.Mode.STOP_SPRINTING
}

connection.sendPacket(ClientCommandC2SPacket(player, state))
lastSprint = new
}

private fun SafeContext.reportSneak(previous: PlayerPacketEvent.Pre, new: PlayerPacketEvent.Pre) {
if (previous.isSneaking == new.isSneaking) return
fun SafeContext.reportSneak(previous: Boolean, new: Boolean) {
if (previous == new) return

val state = if (new.isSneaking) {
val state = if (new) {
ClientCommandC2SPacket.Mode.PRESS_SHIFT_KEY
} else {
ClientCommandC2SPacket.Mode.RELEASE_SHIFT_KEY
}

connection.sendPacket(ClientCommandC2SPacket(player, state))
lastSneak = new
}
}

Expand Down
15 changes: 6 additions & 9 deletions common/src/main/kotlin/com/lambda/interaction/RotationManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ object RotationManager : Loadable {
fun Any.requestRotation(
priority: Int = 0,
alwaysListen: Boolean = false,
onUpdate: SafeContext.() -> RotationContext?,
onReceive: SafeContext.() -> Unit
onUpdate: SafeContext.(lastContext: RotationContext?) -> RotationContext?,
onReceive: SafeContext.() -> Unit = {}
) {
var lastCtx: RotationContext? = null

this.listener<RotationEvent.Update>(priority, alwaysListen) { event ->
val rotationContext = onUpdate()
val rotationContext = onUpdate(event.context)

rotationContext?.let {
event.context = it
Expand Down Expand Up @@ -162,23 +162,20 @@ object RotationManager : Loadable {
@JvmStatic
val movementYaw: Float?
get() {
val config = currentContext?.config ?: return null
if (config.rotationMode == RotationMode.SILENT) return null
if (currentContext?.config?.rotationMode == RotationMode.SILENT) return null
return currentRotation.yaw.toFloat()
}

@JvmStatic
val movementPitch: Float?
get() {
val config = currentContext?.config ?: return null
if (config.rotationMode == RotationMode.SILENT) return null
if (currentContext?.config?.rotationMode == RotationMode.SILENT) return null
return currentRotation.pitch.toFloat()
}

@JvmStatic
fun getRotationForVector(deltaTime: Double): Vec2d? {
val config = currentContext?.config ?: return null
if (config.rotationMode == RotationMode.SILENT) return null
if (currentContext?.config?.rotationMode == RotationMode.SILENT) return null

val rot = lerp(deltaTime, prevRotation, currentRotation)
return Vec2d(rot.yaw, rot.pitch)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.lambda.interaction.blockplace

import com.lambda.context.SafeContext
import com.lambda.interaction.blockplace.PlaceInteraction.canPlaceAt
import com.lambda.interaction.blockplace.PlaceInteraction.isClickable
import com.lambda.interaction.visibilty.VisibilityChecker.getVisibleSurfaces
import com.lambda.util.BlockUtils.blockState
import com.lambda.util.math.VecUtils.distSq
import com.lambda.util.math.VecUtils.getHitVec
import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Box
import net.minecraft.util.math.Direction
import net.minecraft.util.math.Vec3d
import java.util.*
import kotlin.collections.Collection

class PlaceFinder(
private val basePos: BlockPos,
private val maxAttempts: Int,
range: Double,
private val eyes: Vec3d,
private val visibleCheck: Boolean,
private val sides: Set<Direction>
) {
private val rangeSq = range * range

private val Collection<PlaceInfo>.selectClosest get() = minByOrNull { it.eyeDistanceSq }
private val Collection<PlaceInfo>.selectBest get() = this.let { infos ->
if (infos.isEmpty()) return@let null
val lowestStepAmount = infos.minOf { it.placeSteps }
infos.filter { it.placeSteps == lowestStepAmount }.selectClosest
}

companion object {
fun SafeContext.buildPlaceInfo(
basePos: BlockPos,
maxAttempts: Int = 4,
range: Double = 3.25,
eyes: Vec3d = player.eyePos,
visibleCheck: Boolean = true,
sides: Set<Direction> = EnumSet.allOf(Direction::class.java)
) = PlaceFinder(basePos, maxAttempts, range, eyes, visibleCheck, sides).build(this)
}

private fun build(
ctx: SafeContext,
pos: BlockPos = basePos,
attempts: Int = 0
): PlaceInfo? = with(ctx) {
if (sides.isEmpty()) return null
if (!canPlaceAt(pos)) return null

sides.mapNotNull { checkSide(pos, it, attempts) }.selectClosest?.let {
return it
}

if (attempts > maxAttempts) return null

return sides.mapNotNull { side ->
build(this, pos.offset(side), attempts + 1)
}.selectBest
}

private fun SafeContext.checkSide(pos: BlockPos, side: Direction, attempts: Int): PlaceInfo? {
val clickPos = pos.offset(side)
val clickSide = side.opposite

val hitVec = clickPos.getHitVec(clickSide)
val distSq = eyes distSq hitVec

if (distSq > rangeSq) return null
if (clickPos.blockState(world).isClickable) return null

if (visibleCheck) {
val box = Box(clickPos)
val visible = box.getVisibleSurfaces(eyes)
if (clickSide !in visible) return null
}

return PlaceInfo(clickPos, clickSide, pos, hitVec, distSq, attempts)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.lambda.interaction.blockplace

import net.minecraft.util.math.BlockPos
import net.minecraft.util.math.Direction
import net.minecraft.util.math.Vec3d

data class PlaceInfo(
val clickPos: BlockPos,
val clickSide: Direction,
val placedPos: BlockPos,
val hitVec: Vec3d,

val eyeDistanceSq: Double,
val placeSteps: Int
)
Loading