Skip to content

Commit d3d5bfb

Browse files
committed
Movement hooks
1 parent e244027 commit d3d5bfb

File tree

5 files changed

+122
-2
lines changed

5 files changed

+122
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 net.minecraft.client.MinecraftClient;
7+
import net.minecraft.client.network.ClientPlayerEntity;
8+
import net.minecraft.entity.MovementType;
9+
import net.minecraft.util.math.Vec3d;
10+
import org.spongepowered.asm.mixin.Mixin;
11+
import org.spongepowered.asm.mixin.Shadow;
12+
import org.spongepowered.asm.mixin.injection.At;
13+
import org.spongepowered.asm.mixin.injection.Inject;
14+
import org.spongepowered.asm.mixin.injection.Redirect;
15+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
16+
17+
@Mixin(value = ClientPlayerEntity.class, priority = Integer.MAX_VALUE)
18+
public abstract class ClientPlayerEntityMixin extends EntityMixin {
19+
20+
@Shadow protected abstract void autoJump(float dx, float dz);
21+
22+
@Shadow public abstract boolean isUsingItem();
23+
24+
@Shadow private boolean autoJumpEnabled;
25+
26+
@Inject(method = "move", at = @At("HEAD"), cancellable = true)
27+
void onMove(MovementType movementType, Vec3d movement, CallbackInfo ci) {
28+
ClientPlayerEntity self = (ClientPlayerEntity) (Object) this;
29+
if (self != Lambda.getMc().player) return;
30+
31+
ci.cancel();
32+
33+
float prevX = (float) self.getX();
34+
float prevZ = (float) self.getZ();
35+
36+
EventFlow.post(new MovementEvent.Pre());
37+
super.move(movementType, self.getVelocity());
38+
EventFlow.post(new MovementEvent.Post());
39+
40+
float currX = (float) self.getX();
41+
float currZ = (float) self.getZ();
42+
43+
this.autoJump(currX - prevX, currZ - prevZ);
44+
}
45+
46+
@Redirect(method = "tickMovement", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerEntity;isUsingItem()Z"))
47+
boolean onSlowDown(ClientPlayerEntity entity) {
48+
if (EventFlow.post(new MovementEvent.SlowDown()).isCanceled()) return false;
49+
return this.isUsingItem();
50+
}
51+
52+
@Inject(method = "sendMovementPackets", at = @At(value = "HEAD"), cancellable = true)
53+
void sendBegin(CallbackInfo ci) {
54+
ci.cancel();
55+
// PlayerPacketManager.sendPlayerPackets();
56+
autoJumpEnabled = MinecraftClient.getInstance().options.getAutoJump().getValue();
57+
}
58+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.lambda.mixin.entity;
2+
3+
import net.minecraft.entity.Entity;
4+
import net.minecraft.entity.MovementType;
5+
import net.minecraft.util.math.Vec3d;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.Shadow;
8+
9+
@Mixin(Entity.class)
10+
public abstract class EntityMixin {
11+
@Shadow public void move(MovementType movementType, Vec3d movement) {}
12+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 net.minecraft.entity.LivingEntity;
7+
import net.minecraft.util.math.Vec3d;
8+
import org.spongepowered.asm.mixin.Mixin;
9+
import org.spongepowered.asm.mixin.Shadow;
10+
import org.spongepowered.asm.mixin.injection.At;
11+
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
13+
14+
@Mixin(LivingEntity.class)
15+
public abstract class LivingEntityMixin extends EntityMixin {
16+
17+
@Shadow
18+
protected abstract float getJumpVelocity();
19+
20+
@Inject(method = "jump", at = @At("HEAD"), cancellable = true)
21+
void onJump(CallbackInfo ci) {
22+
LivingEntity self = (LivingEntity) (Object) this;
23+
if (self != Lambda.getMc().player) return;
24+
ci.cancel();
25+
26+
float height = this.getJumpVelocity();
27+
MovementEvent.Jump event = EventFlow.post(new MovementEvent.Jump(height));
28+
29+
if (event.isCanceled()) return;
30+
31+
if (!self.isSprinting()) {
32+
Vec3d velocity = self.getVelocity();
33+
self.setVelocity(velocity.x, event.getHeight(), velocity.z);
34+
} else {
35+
// ToDo: Implement rotation system
36+
// Float yaw = RotationManager.getMovementYaw();
37+
// float f = ((yaw != null) ? yaw : self.getYaw()) * ((float)Math.PI / 180);
38+
// self.setVelocity(self.getVelocity().add(-MathHelper.sin(f) * 0.2f, 0.0, MathHelper.cos(f) * 0.2f));
39+
}
40+
41+
self.velocityDirty = true;
42+
}
43+
}

common/src/main/kotlin/com/lambda/event/events/MovementEvent.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ import com.lambda.event.cancellable.Cancellable
55
import com.lambda.event.cancellable.ICancellable
66

77
abstract class MovementEvent : Event {
8+
class Pre : MovementEvent()
9+
class Post : MovementEvent()
810
class ClipAtLedge : MovementEvent(), ICancellable by Cancellable()
11+
class Jump(var height: Double) : MovementEvent(), ICancellable by Cancellable()
12+
class SlowDown : Event, ICancellable by Cancellable()
913
}

common/src/main/resources/lambda.mixins.common.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
"ChatInputSuggestorMixin",
88
"ChatScreenMixin",
99
"ClientConnectionMixin",
10+
"KeyBindingMixin",
1011
"KeyboardMixin",
1112
"MinecraftClientMixin",
12-
"KeyBindingMixin",
13-
"PlayerEntityMixin"
13+
"PlayerEntityMixin",
14+
"entity.ClientPlayerEntityMixin",
15+
"entity.EntityMixin",
16+
"entity.LivingEntityMixin"
1417
],
1518
"injectors": {
1619
"defaultRequire": 1

0 commit comments

Comments
 (0)