Skip to content

Commit 1f3840b

Browse files
committed
use the players keybinds and use minecrafts bind handling to account for toggle binds
1 parent 1811bf1 commit 1f3840b

File tree

4 files changed

+70
-37
lines changed

4 files changed

+70
-37
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
import com.lambda.event.events.InventoryEvent;
2424
import com.lambda.event.events.TickEvent;
2525
import com.lambda.module.modules.player.Interact;
26+
import com.lambda.module.modules.player.InventoryMove;
2627
import net.minecraft.client.MinecraftClient;
2728
import net.minecraft.client.gui.screen.Screen;
2829
import net.minecraft.client.gui.screen.ingame.ScreenHandlerProvider;
2930
import net.minecraft.client.network.ClientPlayerInteractionManager;
31+
import net.minecraft.client.option.KeyBinding;
3032
import net.minecraft.util.thread.ThreadExecutor;
3133
import org.jetbrains.annotations.Nullable;
3234
import org.spongepowered.asm.mixin.Mixin;
@@ -91,6 +93,19 @@ private void onScreenRemove(@Nullable Screen screen, CallbackInfo ci) {
9193
}
9294
}
9395

96+
@Redirect(method = "setScreen", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/option/KeyBinding;unpressAll()V"))
97+
private void redirectUnPressAll() {
98+
if (InventoryMove.INSTANCE.isDisabled() || InventoryMove.hasInputOrNull(currentScreen)) {
99+
KeyBinding.unpressAll();
100+
return;
101+
}
102+
KeyBinding.KEYS_BY_ID.values().forEach(bind -> {
103+
if (!InventoryMove.isKeyMovementRelated(bind.boundKey.getCode())) {
104+
bind.reset();
105+
}
106+
});
107+
}
108+
94109
@Redirect(method = "doItemUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/network/ClientPlayerInteractionManager;isBreakingBlock()Z"))
95110
boolean injectMultiActon(ClientPlayerInteractionManager instance) {
96111
if (instance == null) return true;

common/src/main/java/com/lambda/mixin/input/KeyboardMixin.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919

2020
import com.lambda.event.EventFlow;
2121
import com.lambda.event.events.KeyboardEvent;
22+
import com.lambda.module.modules.player.InventoryMove;
2223
import net.minecraft.client.Keyboard;
24+
import net.minecraft.client.option.KeyBinding;
25+
import net.minecraft.client.util.InputUtil;
2326
import org.spongepowered.asm.mixin.Mixin;
2427
import org.spongepowered.asm.mixin.injection.At;
2528
import org.spongepowered.asm.mixin.injection.Inject;
2629
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
2730

31+
import static com.lambda.Lambda.getMc;
32+
2833
@Mixin(Keyboard.class)
2934
public class KeyboardMixin {
3035
@Inject(method = "onKey", at = @At("HEAD"))
@@ -33,6 +38,15 @@ private void onKey(long window, int key, int scancode, int action, int modifiers
3338
EventFlow.post(new KeyboardEvent.Press(key, scancode, action, modifiers));
3439
}
3540

41+
@Inject(method = "onKey", at = @At("RETURN"))
42+
private void onKeyTail(long window, int key, int scancode, int action, int modifiers, CallbackInfo ci) {
43+
if (InventoryMove.INSTANCE.isDisabled() || InventoryMove.hasInputOrNull(getMc().currentScreen)) return;
44+
if (InventoryMove.isKeyMovementRelated(key)) {
45+
InputUtil.Key fromCode = InputUtil.fromKeyCode(key, scancode);
46+
KeyBinding.setKeyPressed(fromCode, action != 0);
47+
}
48+
}
49+
3650
@Inject(method = "onChar", at = @At("HEAD"))
3751
private void onChar(long window, int codePoint, int modifiers, CallbackInfo ci) {
3852
char[] chars = Character.toChars(codePoint);

common/src/main/kotlin/com/lambda/module/modules/player/InventoryMove.kt

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717

1818
package com.lambda.module.modules.player
1919

20-
import com.lambda.event.events.MovementEvent
21-
import com.lambda.event.listener.SafeListener.Companion.listen
20+
import com.lambda.Lambda.mc
2221
import com.lambda.gui.LambdaScreen
2322
import com.lambda.interaction.request.rotation.Rotation
2423
import com.lambda.interaction.request.rotation.RotationConfig
@@ -28,57 +27,33 @@ import com.lambda.interaction.request.rotation.visibilty.lookAt
2827
import com.lambda.module.Module
2928
import com.lambda.module.tag.ModuleTag
3029
import com.lambda.util.KeyboardUtils.isKeyPressed
31-
import com.lambda.util.math.MathUtils.toDouble
3230
import com.lambda.util.math.MathUtils.toFloatSign
33-
import com.lambda.util.player.MovementUtils.buildMovementInput
34-
import com.lambda.util.player.MovementUtils.mergeFrom
3531
import net.minecraft.client.gui.screen.ChatScreen
3632
import net.minecraft.client.gui.screen.Screen
3733
import net.minecraft.client.gui.screen.ingame.AnvilScreen
3834
import net.minecraft.client.gui.screen.ingame.CommandBlockScreen
39-
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen
4035
import net.minecraft.client.gui.screen.ingame.SignEditScreen
41-
import net.minecraft.client.network.ClientPlayerEntity
42-
import org.lwjgl.glfw.GLFW.*
36+
import org.lwjgl.glfw.GLFW.GLFW_KEY_DOWN
37+
import org.lwjgl.glfw.GLFW.GLFW_KEY_KP_2
38+
import org.lwjgl.glfw.GLFW.GLFW_KEY_KP_4
39+
import org.lwjgl.glfw.GLFW.GLFW_KEY_KP_6
40+
import org.lwjgl.glfw.GLFW.GLFW_KEY_KP_8
41+
import org.lwjgl.glfw.GLFW.GLFW_KEY_LEFT
42+
import org.lwjgl.glfw.GLFW.GLFW_KEY_RIGHT
43+
import org.lwjgl.glfw.GLFW.GLFW_KEY_UP
4344

4445
object InventoryMove : Module(
4546
name = "InventoryMove",
4647
description = "Allows you to move with GUIs opened",
4748
defaultTags = setOf(ModuleTag.PLAYER, ModuleTag.MOVEMENT)
4849
) {
49-
private val speed by setting("Rotation Speed", 5, 1..20, 1, unit = "°/tick")
50+
private val arrowKeys by setting("Arrow Keys", false, "Allows rotating the players camera using the arrow keys")
51+
private val speed by setting("Rotation Speed", 5, 1..20, 1, unit = "°/tick") { arrowKeys }
5052
private val rotationConfig = RotationConfig.Instant(RotationMode.Lock)
5153

52-
/**
53-
* Whether the current screen has text inputs or is null
54-
*/
55-
val Screen?.hasInputOrNull: Boolean
56-
get() = this is ChatScreen ||
57-
this is SignEditScreen ||
58-
this is AnvilScreen ||
59-
this is CommandBlockScreen ||
60-
this is LambdaScreen ||
61-
this == null
62-
6354
init {
64-
listen<MovementEvent.InputUpdate>(20250415) { event ->
65-
if (mc.currentScreen.hasInputOrNull) return@listen
66-
67-
val forward = isKeyPressed(GLFW_KEY_W).toDouble() -
68-
isKeyPressed(GLFW_KEY_S).toDouble()
69-
70-
val strafe = isKeyPressed(GLFW_KEY_A).toDouble() -
71-
isKeyPressed(GLFW_KEY_D).toDouble()
72-
73-
val jump = isKeyPressed(GLFW_KEY_SPACE)
74-
val sneak = isKeyPressed(GLFW_KEY_LEFT_SHIFT)
75-
76-
player.isSprinting = isKeyPressed(GLFW_KEY_LEFT_CONTROL)
77-
event.input.mergeFrom(buildMovementInput(forward, strafe, jump, sneak))
78-
}
79-
8055
onRotate {
81-
if (mc.currentScreen.hasInputOrNull) return@onRotate
56+
if (!arrowKeys || hasInputOrNull(mc.currentScreen)) return@onRotate
8257

8358
val pitch = (isKeyPressed(GLFW_KEY_DOWN, GLFW_KEY_KP_2).toFloatSign() -
8459
isKeyPressed(GLFW_KEY_UP, GLFW_KEY_KP_8).toFloatSign()) * speed
@@ -90,4 +65,31 @@ object InventoryMove : Module(
9065
).requestBy(rotationConfig)
9166
}
9267
}
68+
69+
/**
70+
* Whether the current screen has text inputs or is null
71+
*/
72+
@JvmStatic
73+
fun hasInputOrNull(screen: Screen?) =
74+
screen is ChatScreen ||
75+
screen is SignEditScreen ||
76+
screen is AnvilScreen ||
77+
screen is CommandBlockScreen ||
78+
screen is LambdaScreen ||
79+
screen == null
80+
81+
@JvmStatic
82+
fun isKeyMovementRelated(key: Int): Boolean {
83+
val options = mc.options
84+
return when (key) {
85+
options.forwardKey.boundKey.code,
86+
options.backKey.boundKey.code,
87+
options.leftKey.boundKey.code,
88+
options.rightKey.boundKey.code,
89+
options.jumpKey.boundKey.code,
90+
options.sprintKey.boundKey.code,
91+
options.sneakKey.boundKey.code -> true
92+
else -> false
93+
}
94+
}
9395
}

common/src/main/resources/lambda.accesswidener

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ accessible field net/minecraft/client/MinecraftClient pausedTickDelta F
77
accessible field net/minecraft/client/MinecraftClient thread Ljava/lang/Thread;
88
accessible field net/minecraft/client/MinecraftClient uptimeInTicks J
99
accessible field net/minecraft/client/option/KeyBinding boundKey Lnet/minecraft/client/util/InputUtil$Key;
10+
accessible field net/minecraft/client/option/KeyBinding KEYS_BY_ID Ljava/util/Map;
11+
accessible method net/minecraft/client/option/KeyBinding reset ()V
1012

1113
# World
1214
accessible field net/minecraft/client/world/ClientWorld entityManager Lnet/minecraft/client/world/ClientEntityManager;

0 commit comments

Comments
 (0)