Skip to content

Commit 5d605e3

Browse files
committed
Rework MovementMixin & manualTickMovement to duplicate less vanilla code
Notably it actually calls KeyboardInput::tick instead of a copied version Fixes gh-21 Fixes gh-18
1 parent 88cef27 commit 5d605e3

File tree

7 files changed

+55
-35
lines changed

7 files changed

+55
-35
lines changed

common/build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ dependencies {
88
// which get remapped to the correct annotations on each platform.
99
// Do NOT use other classes from Fabric Loader.
1010
modImplementation "net.fabricmc:fabric-loader:$rootProject.fabric_loader_version"
11+
implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:${mixin_extras_version}"))
1112

1213
modApi("me.shedaniel.cloth:cloth-config:${get("cloth_version")}"){
1314
exclude(group: "net.fabricmc")

common/src/main/java/me/pieking1215/invmove/InvMove.java

+11-31
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public ResourceLocation parseResource(String path){
111111

112112
protected Map<ToggleKeyMapping, Boolean> wasToggleKeyDown = new HashMap<>();
113113

114+
protected boolean forceRawKeyDown = false;
115+
114116
public final List<Module> modules = new ArrayList<>();
115117

116118
public InvMove() {
@@ -179,7 +181,7 @@ private boolean handleToggleMovementKey(Screen screen, boolean couldMove) {
179181
return couldMove;
180182
}
181183

182-
public void onInputUpdate(Input input){
184+
public void onInputUpdate(Input input, boolean sneaking, float sneakSpeed){
183185
if(Minecraft.getInstance().player == null) {
184186
return;
185187
}
@@ -262,16 +264,7 @@ public void onInputUpdate(Input input){
262264
}
263265

264266
// tick movement
265-
// TODO: consider mixing into KeyboardInput::tick or KeyMapping::isDown instead of this for better compatibility
266-
// that would also fix swift sneak (gh-21)
267-
manualTickMovement(input, Minecraft.getInstance().player.isMovingSlowly(), Minecraft.getInstance().player.isSpectator());
268-
269-
// set sprinting using raw keybind data
270-
// edit: this is commented out to let vanilla handle it (requires an extra mixin on forge)
271-
// letting vanilla do it might fix some bugs with other mods that affect sprinting
272-
// if(!Minecraft.getInstance().player.isSprinting() && !Minecraft.getInstance().player.isCrouching()) {
273-
// Minecraft.getInstance().player.setSprinting(rawIsKeyDown(Minecraft.getInstance().options.keySprint));
274-
// }
267+
inputTickRaw(input, sneaking, sneakSpeed);
275268

276269
}else if(Minecraft.getInstance().screen != null){
277270
// we are in a screen that we can't move in
@@ -366,29 +359,16 @@ public static Field[] getDeclaredFieldsSuper(Class<?> aClass) {
366359
}
367360

368361
/**
369-
* Clone of Input.tick but uses raw keybind data
362+
* Calls Input.tick but forces using raw keybind data
370363
*/
371-
public void manualTickMovement(Input input, boolean slow, boolean noDampening) {
372-
373-
input.up = rawIsKeyDown(Minecraft.getInstance().options.keyUp);
374-
input.down = rawIsKeyDown(Minecraft.getInstance().options.keyDown);
375-
input.left = rawIsKeyDown(Minecraft.getInstance().options.keyLeft);
376-
input.right = rawIsKeyDown(Minecraft.getInstance().options.keyRight);
377-
input.forwardImpulse = input.up == input.down ? 0.0F : (float)(input.up ? 1 : -1);
378-
input.leftImpulse = input.left == input.right ? 0.0F : (float)(input.left ? 1 : -1);
379-
input.jumping = rawIsKeyDown(Minecraft.getInstance().options.keyJump) && InvMoveConfig.MOVEMENT.JUMP.get();
380-
381-
input.shiftKeyDown = rawIsKeyDown(Minecraft.getInstance().options.keyShift);
382-
if (!noDampening && (input.shiftKeyDown || slow)) {
383-
input.leftImpulse = (float)((double)input.leftImpulse * 0.3D);
384-
input.forwardImpulse = (float)((double)input.forwardImpulse * 0.3D);
385-
}
364+
public void inputTickRaw(Input input, boolean sneaking, float sneakSpeed) {
365+
forceRawKeyDown = true;
366+
input.tick(sneaking, sneakSpeed);
367+
forceRawKeyDown = false;
386368
}
387369

388-
public static boolean rawIsKeyDown(KeyMapping key){
389-
// this field is accesswidened
390-
// can't use the method because it has extra conditions in forge
391-
return key.isDown;
370+
public boolean shouldForceRawKeyDown() {
371+
return forceRawKeyDown;
392372
}
393373

394374
/**
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
package me.pieking1215.invmove.mixin.client;
22

3+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
4+
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
35
import me.pieking1215.invmove.InvMove;
6+
import net.minecraft.client.player.Input;
47
import net.minecraft.client.player.LocalPlayer;
8+
import net.minecraft.world.entity.player.Abilities;
59
import org.spongepowered.asm.mixin.Mixin;
610
import org.spongepowered.asm.mixin.injection.At;
711
import org.spongepowered.asm.mixin.injection.Inject;
12+
import org.spongepowered.asm.mixin.injection.Redirect;
813
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
14+
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
915

1016
@Mixin(LocalPlayer.class)
1117
public class MovementMixin {
12-
@Inject(
18+
@WrapOperation(
1319
method = "aiStep",
14-
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/tutorial/Tutorial;onInput(Lnet/minecraft/client/player/Input;)V")
20+
at = @At(value = "INVOKE", target = "Lnet/minecraft/client/player/Input;tick(ZF)V")
1521
)
16-
private void onInput(CallbackInfo info) {
22+
private void onTick(Input instance, boolean sneaking, float sneakSpeed, Operation<Void> original) {
23+
original.call(instance, sneaking, sneakSpeed);
24+
1725
//noinspection ConstantConditions
18-
InvMove.instance().onInputUpdate(((LocalPlayer)(Object)this).input);
26+
InvMove.instance().onInputUpdate(instance, sneaking, sneakSpeed);
1927
}
2028
}

gradle.properties

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ org.gradle.jvmargs=-Xmx3G
33
#org.gradle.parallel=true
44

55
fabric_loader_version = 0.16.7
6+
mixin_extras_version = 0.4.1
67

78
# Key mod properties
89
id = invmove

neoforge/build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ repositories {
3434
dependencies {
3535
neoForge "net.neoforged:neoforge:${get("neoforge")}"
3636

37+
implementation(annotationProcessor("io.github.llamalad7:mixinextras-common:${mixin_extras_version}"))
38+
implementation(include("io.github.llamalad7:mixinextras-neoforge:${mixin_extras_version}"))
39+
3740
modApi "me.shedaniel.cloth:cloth-config-neoforge:${get("cloth_version")}"
3841

3942
common(project(path: ":common:$stonecutter.current.project", configuration: 'namedElements')) { transitive false }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package me.pieking1215.invmove.neoforge.mixin.client;
2+
3+
import me.pieking1215.invmove.InvMove;
4+
import net.minecraft.client.KeyMapping;
5+
import net.minecraft.client.ToggleKeyMapping;
6+
import org.spongepowered.asm.mixin.Mixin;
7+
import org.spongepowered.asm.mixin.Shadow;
8+
import org.spongepowered.asm.mixin.injection.At;
9+
import org.spongepowered.asm.mixin.injection.Inject;
10+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
11+
12+
@Mixin(
13+
value = {KeyMapping.class, ToggleKeyMapping.class}
14+
)
15+
public class KeyMappingIsDownMixin {
16+
@Inject(
17+
method = "isDown",
18+
at = @At("HEAD"),
19+
cancellable = true
20+
)
21+
private void overrideIsDown(CallbackInfoReturnable<Boolean> cir) {
22+
if (InvMove.instance().shouldForceRawKeyDown()) {
23+
cir.setReturnValue(((KeyMapping)(Object)this).isDown);
24+
}
25+
}
26+
}

neoforge/src/main/resources/invmove-neoforge.mixins.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"package": "me.pieking1215.invmove.neoforge.mixin",
55
"compatibilityLevel": "JAVA_16",
66
"client": [
7+
"client.KeyMappingIsDownMixin",
78
"client.SprintKeyDownMixin"
89
],
910
"mixins": [

0 commit comments

Comments
 (0)