Skip to content

Commit 9800466

Browse files
committed
Block interaction tests
1 parent a69e0c9 commit 9800466

File tree

14 files changed

+262
-198
lines changed

14 files changed

+262
-198
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.lambda.mixin;
2+
3+
import com.lambda.interaction.RotationManager;
4+
import net.minecraft.client.render.GameRenderer;
5+
import org.spongepowered.asm.mixin.Mixin;
6+
import org.spongepowered.asm.mixin.injection.At;
7+
import org.spongepowered.asm.mixin.injection.Inject;
8+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
9+
10+
@Mixin(GameRenderer.class)
11+
public class GameRendererMixin {
12+
@Inject(method = "updateTargetedEntity", at = @At("HEAD"))
13+
private void onUpdateTargetedEntity(CallbackInfo ci) {
14+
RotationManager.updateInterpolated();
15+
}
16+
}

common/src/main/java/com/lambda/mixin/entity/ClientPlayerEntityMixin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import com.lambda.Lambda;
44
import com.lambda.event.EventFlow;
55
import com.lambda.event.events.MovementEvent;
6-
import com.lambda.manager.PlayerPacketManager;
7-
import com.lambda.manager.RotationManager;
8-
import net.minecraft.client.MinecraftClient;
6+
import com.lambda.interaction.PlayerPacketManager;
7+
import com.lambda.interaction.RotationManager;
98
import net.minecraft.client.network.ClientPlayerEntity;
109
import net.minecraft.entity.MovementType;
1110
import net.minecraft.util.math.Vec3d;

common/src/main/java/com/lambda/mixin/entity/EntityMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lambda.mixin.entity;
22

33
import com.lambda.Lambda;
4-
import com.lambda.manager.RotationManager;
4+
import com.lambda.interaction.RotationManager;
55
import com.lambda.util.math.Vec2d;
66
import net.minecraft.entity.Entity;
77
import net.minecraft.entity.MovementType;

common/src/main/java/com/lambda/mixin/entity/LivingEntityMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import com.lambda.Lambda;
44
import com.lambda.event.EventFlow;
55
import com.lambda.event.events.MovementEvent;
6-
import com.lambda.manager.RotationManager;
6+
import com.lambda.interaction.RotationManager;
77
import net.minecraft.entity.Entity;
88
import net.minecraft.entity.LivingEntity;
99
import net.minecraft.util.math.MathHelper;

common/src/main/kotlin/com/lambda/Loader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lambda
22

33
import com.lambda.Lambda.LOG
44
import com.lambda.command.CommandManager
5-
import com.lambda.manager.RotationManager
5+
import com.lambda.interaction.RotationManager
66
import com.lambda.module.ModuleRegistry
77
import kotlin.system.measureTimeMillis
88

Lines changed: 14 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
package com.lambda.event.events
22

3-
import com.lambda.config.RotationSettings
4-
import com.lambda.context.SafeContext
53
import com.lambda.event.Event
64
import com.lambda.event.cancellable.Cancellable
75
import com.lambda.event.cancellable.ICancellable
8-
import com.lambda.manager.RotationManager
96
import com.lambda.interaction.InteractionConfig
7+
import com.lambda.interaction.RotationManager
108
import com.lambda.interaction.rotation.IRotationConfig
11-
import com.lambda.interaction.rotation.Rotation
12-
import com.lambda.interaction.rotation.Rotation.Companion.distance
13-
import com.lambda.interaction.rotation.Rotation.Companion.rotationTo
14-
import com.lambda.interaction.VisibilityChecker.scanVisibleSurfaces
15-
import com.lambda.module.modules.RotationTest
9+
import com.lambda.interaction.rotation.RotationRequest
10+
import com.lambda.interaction.visibilty.VisibilityChecker.findRotation
1611
import com.lambda.threading.runSafe
17-
import com.lambda.util.math.VecUtils.distSq
1812
import com.lambda.util.world.raycast.RayCastUtils.blockResult
1913
import com.lambda.util.world.raycast.RayCastUtils.entityResult
2014
import net.minecraft.entity.LivingEntity
21-
import net.minecraft.util.Hand
22-
import net.minecraft.util.hit.HitResult
2315
import net.minecraft.util.math.BlockPos
24-
import net.minecraft.util.math.Box
2516
import net.minecraft.util.math.Direction
2617
import java.util.*
2718

@@ -37,109 +28,39 @@ abstract class RotationEvent : Event {
3728
}
3829
}
3930

40-
private fun SafeContext.lookAt(
41-
rotationConfig: IRotationConfig,
42-
interact: InteractionConfig,
43-
boxes: List<Box>,
44-
priority: Int = 0,
45-
sides: Set<Direction> = emptySet(),
46-
hitCheck: HitResult.() -> Boolean,
47-
) {
48-
val eye = player.getCameraPosVec(mc.tickDelta)
49-
50-
if (boxes.any { it.contains(eye) }) {
51-
stay(priority, rotationConfig)
52-
return
53-
}
54-
55-
val currentRotation = RotationManager.currentRotation
56-
val currentCast = currentRotation.rayCast(
57-
interact.reach,
58-
interact.rayCastMask,
59-
eye
60-
)
61-
val check = currentCast?.let { it.hitCheck() } ?: false
62-
63-
// Slowdown or freeze if looking correct
64-
(rotationConfig as? RotationSettings)?.slowdownIf(check) ?: run {
65-
if (check) stay(priority, rotationConfig)
66-
return
67-
}
68-
69-
val reachSq = interact.reach * interact.reach
70-
71-
var closestRotation: Rotation? = null
72-
var rotationDist = 0.0
73-
74-
boxes.forEach { box ->
75-
scanVisibleSurfaces(box, sides, interact.resolution) { vec ->
76-
if (eye distSq vec > reachSq) return@scanVisibleSurfaces
77-
78-
val newRotation = eye.rotationTo(vec)
79-
80-
val cast = newRotation.rayCast(
81-
interact.reach,
82-
interact.rayCastMask,
83-
eye
84-
) ?: return@scanVisibleSurfaces
85-
if (!cast.hitCheck()) return@scanVisibleSurfaces
86-
87-
val dist = newRotation.distance(currentRotation)
88-
if (dist >= rotationDist && closestRotation != null) return@scanVisibleSurfaces
89-
90-
rotationDist = dist
91-
closestRotation = newRotation
92-
}
93-
}
94-
95-
// Rotate to selected point
96-
closestRotation?.let { rotation ->
97-
requests.add(RotationRequest(priority, rotationConfig, rotation))
98-
}
99-
}
100-
101-
fun lookAt(
31+
fun lookAtEntity(
10232
rotationConfig: IRotationConfig,
10333
interactionConfig: InteractionConfig,
10434
entity: LivingEntity,
10535
priority: Int = 0,
10636
) {
10737
runSafe {
108-
lookAt(rotationConfig, interactionConfig, listOf(entity.boundingBox), priority) {
38+
findRotation(rotationConfig, interactionConfig, listOf(entity.boundingBox), priority) {
10939
entityResult?.entity == entity
40+
}?.let {
41+
requests.add(it)
11042
}
11143
}
11244
}
11345

114-
fun lookAt(
46+
fun lookAtBlock(
11547
rotationConfig: IRotationConfig,
11648
interactionConfig: InteractionConfig,
11749
blockPos: BlockPos,
118-
sides: Set<Direction>,
50+
sides: Set<Direction> = emptySet(),
11951
priority: Int = 0,
12052
) {
12153
runSafe {
12254
val state = world.getBlockState(blockPos)
12355
val voxelShape = state.getOutlineShape(world, blockPos)
12456
val boundingBoxes = voxelShape.boundingBoxes.map { it.offset(blockPos) }
125-
lookAt(rotationConfig, interactionConfig, boundingBoxes, priority, sides) {
126-
blockResult?.blockPos == blockPos && blockResult?.side in sides
57+
findRotation(rotationConfig, interactionConfig, boundingBoxes, priority, sides) {
58+
blockResult?.blockPos == blockPos && (blockResult?.side in sides || sides.isEmpty())
59+
}?.let {
60+
requests.add(it)
12761
}
12862
}
12963
}
130-
131-
private fun stay(priority: Int = 0, config: IRotationConfig) {
132-
requests.add(RotationRequest(priority, config, RotationManager.currentRotation))
133-
}
134-
135-
data class RotationRequest(
136-
val priority: Int,
137-
val config: IRotationConfig,
138-
val rotation: Rotation,
139-
) : Comparable<RotationRequest> {
140-
override fun compareTo(other: RotationRequest) =
141-
priority.compareTo(other.priority)
142-
}
14364
}
144-
class Post : RotationEvent()
65+
class Post(val request: RotationRequest) : RotationEvent()
14566
}

common/src/main/kotlin/com/lambda/manager/PlayerPacketManager.kt renamed to common/src/main/kotlin/com/lambda/interaction/PlayerPacketManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lambda.manager
1+
package com.lambda.interaction
22

33
import com.lambda.Loadable
44
import com.lambda.context.SafeContext

common/src/main/kotlin/com/lambda/manager/RotationManager.kt renamed to common/src/main/kotlin/com/lambda/interaction/RotationManager.kt

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.lambda.manager
1+
package com.lambda.interaction
22

33
import com.lambda.Lambda.LOG
44
import com.lambda.Lambda.mc
@@ -11,15 +11,14 @@ import com.lambda.event.events.RotationEvent
1111
import com.lambda.event.events.TickEvent
1212
import com.lambda.event.listener.SafeListener.Companion.listener
1313
import com.lambda.event.listener.UnsafeListener.Companion.unsafeListener
14-
import com.lambda.interaction.rotation.Rotation
14+
import com.lambda.interaction.rotation.*
1515
import com.lambda.interaction.rotation.Rotation.Companion.angleDifference
1616
import com.lambda.interaction.rotation.Rotation.Companion.fixSensitivity
1717
import com.lambda.interaction.rotation.Rotation.Companion.interpolate
18-
import com.lambda.interaction.rotation.RotationContext
19-
import com.lambda.interaction.rotation.RotationMode
2018
import com.lambda.module.modules.client.Baritone
2119
import com.lambda.threading.runOnGameThread
2220
import com.lambda.threading.runSafe
21+
import com.lambda.util.Communication.info
2322
import com.lambda.util.math.MathUtils.lerp
2423
import com.lambda.util.math.MathUtils.toRadian
2524
import com.lambda.util.math.Vec2d
@@ -34,7 +33,7 @@ object RotationManager : Loadable {
3433
var currentRotation = Rotation.ZERO
3534
private var prevRotation = Rotation.ZERO
3635

37-
var currentContext: RotationContext? = null
36+
var currentRequest: RotationRequest? = null
3837

3938
private var keepTicks = 0
4039
private var pauseTicks = 0
@@ -43,11 +42,25 @@ object RotationManager : Loadable {
4342
fun update() =
4443
runSafe {
4544
EventFlow.post(RotationEvent.Pre()) {
46-
rotate()
45+
rotate()?.let {
46+
EventFlow.post(RotationEvent.Post(it))
47+
}
4748
}
48-
EventFlow.post(RotationEvent.Post())
4949
}
5050

51+
@JvmStatic
52+
fun updateInterpolated() = runSafe {
53+
// if (currentRequest == null) return@runSafe
54+
// val interpolation = interpolate(prevRotation, currentRotation, mc.tickDelta.toDouble())
55+
//
56+
// val rot = interpolation.fixSensitivity(prevRotation)
57+
//
58+
// if (currentRequest?.config?.rotationMode == RotationMode.LOCK) {
59+
// player.yaw = rot.yaw.toFloat()
60+
// player.pitch = rot.pitch.toFloat()
61+
// }
62+
}
63+
5164
init {
5265
listener<PacketEvent.Send.Post> { event ->
5366
val packet = event.packet
@@ -69,75 +82,87 @@ object RotationManager : Loadable {
6982
(keepTicks--).coerceAtLeast(0)
7083
(pauseTicks--).coerceAtLeast(0)
7184

72-
val resetTicks = currentContext?.config?.resetTicks ?: 0
85+
val resetTicks = currentRequest?.config?.resetTicks ?: 0
7386
if (keepTicks + resetTicks < 0 || pauseTicks >= 0) {
74-
currentContext = null
87+
currentRequest = null
7588
}
7689

77-
requests.firstOrNull()?.let {
78-
currentContext = RotationContext(it.config, it.rotation)
79-
keepTicks = it.config.keepTicks
90+
var chosenRequest: RotationRequest? = null
91+
92+
requests.firstOrNull()?.let { request ->
93+
chosenRequest = request
94+
currentRequest = request
95+
keepTicks = request.config.keepTicks
8096
}
8197

8298
currentRotation = Rotation(player.yaw, player.pitch)
8399

84-
val context = currentContext ?: return@runSafe
100+
val context = currentRequest ?: return@runSafe chosenRequest
85101
val rotationTo = if (keepTicks >= 0) context.rotation else currentRotation
86102

87103
var speedMultiplier = (context.config as? RotationSettings)?.speedMultiplier ?: 1.0
88104
if (keepTicks < 0) speedMultiplier = 1.0
89105

90106
val turnSpeed = context.config.turnSpeed * speedMultiplier
91107

92-
currentRotation = interpolate(prevRotation, rotationTo, turnSpeed)
93-
.fixSensitivity(prevRotation)
108+
val interpolation = interpolate(prevRotation, rotationTo, turnSpeed)
109+
110+
currentRotation = interpolation.fixSensitivity(prevRotation)
94111

95112
if (context.config.rotationMode == RotationMode.LOCK) {
96113
player.yaw = currentRotation.yaw.toFloat()
97114
player.pitch = currentRotation.pitch.toFloat()
98115
}
116+
117+
chosenRequest?.let { request ->
118+
if (request.rotation.fixSensitivity(prevRotation) == currentRotation) {
119+
request.isPending = false
120+
}
121+
}
122+
123+
return@runSafe chosenRequest
99124
}
100125

101126
private fun reset(rotation: Rotation) {
102127
prevRotation = rotation
103128
currentRotation = rotation
104129

105-
currentContext = null
130+
currentRequest = null
106131
pauseTicks = 3
107132
}
108133

109134
private val smoothRotation get() =
110135
lerp(prevRotation, currentRotation, mc.partialTicks)
111136

112137
@JvmStatic val lockRotation get() =
113-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) smoothRotation else null
138+
if (currentRequest?.config?.rotationMode == RotationMode.LOCK) smoothRotation else null
114139

115140
@JvmStatic val renderYaw get() =
116-
if (currentContext?.config == null) null else smoothRotation.yaw.toFloat()
141+
if (currentRequest?.config == null) null else smoothRotation.yaw.toFloat()
117142

118143
@JvmStatic val renderPitch get() =
119-
if (currentContext?.config == null) null else smoothRotation.pitch.toFloat()
144+
if (currentRequest?.config == null) null else smoothRotation.pitch.toFloat()
120145

121146
@JvmStatic val handYaw get() =
122-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.yaw.toFloat() else null
147+
if (currentRequest?.config?.rotationMode == RotationMode.LOCK) currentRotation.yaw.toFloat() else null
123148

124149
@JvmStatic val handPitch get() =
125-
if (currentContext?.config?.rotationMode == RotationMode.LOCK) currentRotation.pitch.toFloat() else null
150+
if (currentRequest?.config?.rotationMode == RotationMode.LOCK) currentRotation.pitch.toFloat() else null
126151

127152
@JvmStatic val movementYaw: Float? get() {
128-
val config = currentContext?.config ?: return null
153+
val config = currentRequest?.config ?: return null
129154
if (config.rotationMode == RotationMode.SILENT) return null
130155
return currentRotation.yaw.toFloat()
131156
}
132157

133158
@JvmStatic val movementPitch: Float? get() {
134-
val config = currentContext?.config ?: return null
159+
val config = currentRequest?.config ?: return null
135160
if (config.rotationMode == RotationMode.SILENT) return null
136161
return currentRotation.pitch.toFloat()
137162
}
138163

139164
@JvmStatic fun getRotationForVector(deltaTime: Double): Vec2d? {
140-
val config = currentContext?.config ?: return null
165+
val config = currentRequest?.config ?: return null
141166
if (config.rotationMode == RotationMode.SILENT) return null
142167

143168
val rot = lerp(prevRotation, currentRotation, deltaTime)
@@ -167,7 +192,7 @@ object RotationManager : Loadable {
167192

168193
@JvmStatic
169194
fun processPlayerMovement() = runSafe {
170-
val config = currentContext?.config ?: return@runSafe
195+
val config = currentRequest?.config ?: return@runSafe
171196

172197
val input = player.input
173198
val handledByBaritone = input !is KeyboardInput

0 commit comments

Comments
 (0)