Skip to content

Commit f368fce

Browse files
authored
Merge pull request #3 from Avanatiker/feature/movement
Movement and Rotation engine
2 parents 012430f + 9800466 commit f368fce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1827
-29
lines changed

common/src/main/java/com/lambda/mixin/ClientConnectionMixin.java

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

33
import com.lambda.event.EventFlow;
4+
import com.lambda.event.events.ConnectionEvent;
45
import com.lambda.event.events.PacketEvent;
56
import io.netty.channel.ChannelHandlerContext;
67
import net.minecraft.network.ClientConnection;
78
import net.minecraft.network.NetworkSide;
9+
import net.minecraft.network.listener.PacketListener;
810
import net.minecraft.network.packet.Packet;
11+
import net.minecraft.network.packet.c2s.handshake.ConnectionIntent;
12+
import net.minecraft.text.Text;
913
import org.spongepowered.asm.mixin.Final;
1014
import org.spongepowered.asm.mixin.Mixin;
1115
import org.spongepowered.asm.mixin.Shadow;
@@ -53,4 +57,20 @@ private void receivingPacketPost(
5357

5458
EventFlow.post(new PacketEvent.Receive.Post(packet));
5559
}
60+
61+
@Inject(method = "connect(Ljava/lang/String;ILnet/minecraft/network/listener/PacketListener;Lnet/minecraft/network/packet/c2s/handshake/ConnectionIntent;)V", at = @At("HEAD"))
62+
private void onConnect(
63+
String address,
64+
int port,
65+
PacketListener listener,
66+
ConnectionIntent intent,
67+
CallbackInfo ci
68+
) {
69+
EventFlow.post(new ConnectionEvent.Connect(address, port, listener, intent));
70+
}
71+
72+
@Inject(method = "disconnect(Lnet/minecraft/text/Text;)V", at = @At("HEAD"))
73+
private void onDisconnect(Text reason, CallbackInfo ci) {
74+
EventFlow.post(new ConnectionEvent.Disconnect(reason));
75+
}
5676
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.lambda.mixin;
2+
3+
import com.lambda.util.DebugInfoHud;
4+
import net.minecraft.client.gui.hud.DebugHud;
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.CallbackInfoReturnable;
9+
10+
import java.util.List;
11+
12+
@Mixin(DebugHud.class)
13+
public class DebugHudMixin {
14+
@Inject(method = "getRightText", at = @At(value = "TAIL"))
15+
private void onGetRightText(CallbackInfoReturnable<List<String>> cir) {
16+
DebugInfoHud.addDebugInfo(cir.getReturnValue());
17+
}
18+
}
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/MinecraftClientMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ boolean injectMultiActon(ClientPlayerInteractionManager instance) {
5151
void injectFastPlace(CallbackInfo ci) {
5252
if (!Interact.INSTANCE.isEnabled()) return;
5353

54-
Lambda.INSTANCE.getMc().itemUseCooldown = Interact.getPlaceDelay();
54+
Lambda.getMc().itemUseCooldown = Interact.getPlaceDelay();
5555
}
5656
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.lambda.mixin.entity;
2+
3+
import com.lambda.Lambda;
4+
import com.lambda.event.EventFlow;
5+
import com.lambda.event.events.MovementEvent;
6+
import com.lambda.interaction.PlayerPacketManager;
7+
import com.lambda.interaction.RotationManager;
8+
import net.minecraft.client.network.ClientPlayerEntity;
9+
import net.minecraft.entity.MovementType;
10+
import net.minecraft.util.math.Vec3d;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.Shadow;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.Redirect;
16+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
17+
18+
import java.util.Objects;
19+
20+
@Mixin(value = ClientPlayerEntity.class, priority = Integer.MAX_VALUE)
21+
public abstract class ClientPlayerEntityMixin extends EntityMixin {
22+
23+
@Shadow protected abstract void autoJump(float dx, float dz);
24+
25+
@Shadow public abstract boolean isUsingItem();
26+
27+
@Shadow private boolean autoJumpEnabled;
28+
29+
@Inject(method = "move", at = @At("HEAD"), cancellable = true)
30+
void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
31+
ClientPlayerEntity self = (ClientPlayerEntity) (Object) this;
32+
if (self != Lambda.getMc().player) return;
33+
34+
ci.cancel();
35+
36+
float prevX = (float) self.getX();
37+
float prevZ = (float) self.getZ();
38+
39+
EventFlow.post(new MovementEvent.Pre());
40+
super.move(movementType, self.getVelocity());
41+
EventFlow.post(new MovementEvent.Post());
42+
43+
float currX = (float) self.getX();
44+
float currZ = (float) self.getZ();
45+
46+
this.autoJump(currX - prevX, currZ - prevZ);
47+
}
48+
49+
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z"))
50+
boolean onSlowDown(ClientPlayerEntity entity) {
51+
if (EventFlow.post(new MovementEvent.SlowDown()).isCanceled()) return false;
52+
return this.isUsingItem();
53+
}
54+
55+
@Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/input/Input;tick(ZF)V", shift = At.Shift.AFTER))
56+
void processMovement(CallbackInfo ci) {
57+
RotationManager.BaritoneProcessor.processPlayerMovement();
58+
}
59+
60+
@Inject(method = "sendMovementPackets", at = @At(value = "HEAD"), cancellable = true)
61+
void sendBegin(CallbackInfo ci) {
62+
ci.cancel();
63+
PlayerPacketManager.sendPlayerPackets();
64+
autoJumpEnabled = Lambda.getMc().options.getAutoJump().getValue();
65+
}
66+
67+
@Redirect(method = "tickNewAi", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getYaw()F"))
68+
float fixHeldItemYaw(ClientPlayerEntity instance) {
69+
return Objects.requireNonNullElse(RotationManager.getHandYaw(), instance.getYaw());
70+
}
71+
72+
@Redirect(method = "tickNewAi", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;getPitch()F"))
73+
float fixHeldItemPitch(ClientPlayerEntity instance) {
74+
return Objects.requireNonNullElse(RotationManager.getHandPitch(), instance.getPitch());
75+
}
76+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.lambda.mixin.entity;
2+
3+
import com.lambda.Lambda;
4+
import com.lambda.interaction.RotationManager;
5+
import com.lambda.util.math.Vec2d;
6+
import net.minecraft.entity.Entity;
7+
import net.minecraft.entity.MovementType;
8+
import net.minecraft.util.math.Vec3d;
9+
import org.spongepowered.asm.mixin.Mixin;
10+
import org.spongepowered.asm.mixin.Shadow;
11+
import org.spongepowered.asm.mixin.injection.At;
12+
import org.spongepowered.asm.mixin.injection.Redirect;
13+
14+
@Mixin(Entity.class)
15+
public abstract class EntityMixin {
16+
@Shadow public void move(MovementType movementType, Vec3d movement) {}
17+
18+
@Shadow public abstract float getYaw();
19+
20+
@Redirect(method = "updateVelocity", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getYaw()F"))
21+
public float velocityYaw(Entity entity) {
22+
if ((Object) this != Lambda.getMc().player) return getYaw();
23+
24+
Float y = RotationManager.getMovementYaw();
25+
if (y == null) return getYaw();
26+
27+
return y;
28+
}
29+
30+
@Redirect(method = "getRotationVec", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getYaw(F)F"))
31+
float fixDirectionYaw(Entity entity, float tickDelta) {
32+
Vec2d rot = RotationManager.getRotationForVector(tickDelta);
33+
if (entity != Lambda.getMc().player || rot == null) return entity.getYaw(tickDelta);
34+
35+
return (float) rot.getX();
36+
}
37+
38+
@Redirect(method = "getRotationVec", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getPitch(F)F"))
39+
float fixDirectionPitch(Entity entity, float tickDelta) {
40+
Vec2d rot = RotationManager.getRotationForVector(tickDelta);
41+
if (entity != Lambda.getMc().player || rot == null) return entity.getPitch(tickDelta);
42+
43+
return (float) rot.getY();
44+
}
45+
46+
@Redirect(method = "getRotationVector()Lnet/minecraft/util/math/Vec3d;", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getYaw()F"))
47+
float fixDirectionYaw2(Entity entity) {
48+
Vec2d rot = RotationManager.getRotationForVector(1.0);
49+
if (entity != Lambda.getMc().player || rot == null) return entity.getYaw();
50+
51+
return (float) rot.getX();
52+
}
53+
54+
@Redirect(method = "getRotationVector()Lnet/minecraft/util/math/Vec3d;", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/Entity;getPitch()F"))
55+
float fixDirectionPitch2(Entity entity) {
56+
Vec2d rot = RotationManager.getRotationForVector(1.0);
57+
if (entity != Lambda.getMc().player || rot == null) return entity.getPitch();
58+
59+
return (float) rot.getY();
60+
}
61+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.lambda.mixin.entity;
2+
3+
import com.lambda.Lambda;
4+
import com.lambda.event.EventFlow;
5+
import com.lambda.event.events.MovementEvent;
6+
import com.lambda.interaction.RotationManager;
7+
import net.minecraft.entity.Entity;
8+
import net.minecraft.entity.LivingEntity;
9+
import net.minecraft.util.math.MathHelper;
10+
import net.minecraft.util.math.Vec3d;
11+
import org.spongepowered.asm.mixin.Mixin;
12+
import org.spongepowered.asm.mixin.Shadow;
13+
import org.spongepowered.asm.mixin.injection.At;
14+
import org.spongepowered.asm.mixin.injection.Inject;
15+
import org.spongepowered.asm.mixin.injection.Redirect;
16+
import org.spongepowered.asm.mixin.injection.Slice;
17+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
18+
19+
@Mixin(LivingEntity.class)
20+
public abstract class LivingEntityMixin extends EntityMixin {
21+
22+
@Shadow
23+
protected abstract float getJumpVelocity();
24+
25+
@Inject(method = "jump", at = @At("HEAD"), cancellable = true)
26+
void onJump(CallbackInfo ci) {
27+
LivingEntity self = (LivingEntity) (Object) this;
28+
if (self != Lambda.getMc().player) return;
29+
ci.cancel();
30+
31+
float height = this.getJumpVelocity();
32+
MovementEvent.Jump event = EventFlow.post(new MovementEvent.Jump(height));
33+
34+
if (event.isCanceled()) return;
35+
36+
Vec3d vec3d = self.getVelocity();
37+
self.setVelocity(vec3d.x, event.getHeight(), vec3d.z);
38+
39+
if (self.isSprinting()) {
40+
Float yaw = RotationManager.getMovementYaw();
41+
float f = ((yaw != null) ? yaw : self.getYaw()) * ((float)Math.PI / 180);
42+
self.setVelocity(self.getVelocity().add(-MathHelper.sin(f) * 0.2f, 0.0, MathHelper.cos(f) * 0.2f));
43+
}
44+
45+
self.velocityDirty = true;
46+
}
47+
48+
@Inject(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;isImmobile()Z"))
49+
void onTravelH(CallbackInfo ci) {
50+
Entity self = (Entity) (Object) this;
51+
if (self != Lambda.getMc().player) return;
52+
53+
RotationManager.update();
54+
}
55+
56+
@Redirect(method = "travel", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getPitch()F"))
57+
private float hookModifyFallFlyingPitch(LivingEntity entity) {
58+
Float pitch = RotationManager.getMovementPitch();
59+
if (entity != Lambda.getMc().player || pitch == null) return entity.getPitch();
60+
61+
return pitch;
62+
}
63+
64+
@Redirect(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getYaw()F"), slice = @Slice(to = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getYaw()F", ordinal = 1)))
65+
private float rotBody(LivingEntity entity) {
66+
if ((Object) this != Lambda.getMc().player) {
67+
return entity.getYaw();
68+
}
69+
70+
Float yaw = RotationManager.getRenderYaw();
71+
return (yaw == null) ? entity.getYaw() : yaw;
72+
}
73+
74+
@Redirect(method = "turnHead", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/LivingEntity;getYaw()F"))
75+
private float rotHead(LivingEntity entity) {
76+
if ((Object) this != Lambda.getMc().player) {
77+
return entity.getYaw();
78+
}
79+
80+
Float yaw = RotationManager.getRenderYaw();
81+
return (yaw == null) ? entity.getYaw() : yaw;
82+
}
83+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.lambda
22

33
interface Loadable {
4-
fun load(): String
4+
fun load() = this::class.simpleName?.let { "Loaded $it" } ?: "Loaded"
55
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package com.lambda
22

33
import com.lambda.Lambda.LOG
44
import com.lambda.command.CommandManager
5+
import com.lambda.interaction.RotationManager
56
import com.lambda.module.ModuleRegistry
67
import kotlin.system.measureTimeMillis
78

89
object Loader {
910
private val loadables = listOf(
1011
ModuleRegistry,
11-
CommandManager
12+
CommandManager,
13+
RotationManager
1214
)
1315

1416
fun initialize() {

common/src/main/kotlin/com/lambda/command/CommandManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ object CommandManager : Configurable(LambdaConfig), Loadable {
2929

3030
val prefix by setting("prefix", ';')
3131

32-
private val commands = mutableSetOf<LambdaCommand>()
32+
val commands = mutableSetOf<LambdaCommand>()
3333
private val dispatcher by lazy { CommandDispatcher<CommandSource>() }
3434
private const val ERROR_PADDING = 10
3535

0 commit comments

Comments
 (0)