Skip to content

Commit 28c9945

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

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
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: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -17,77 +17,47 @@
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
23-
import com.lambda.interaction.request.rotation.Rotation
24-
import com.lambda.interaction.request.rotation.RotationConfig
25-
import com.lambda.interaction.request.rotation.RotationManager.onRotate
26-
import com.lambda.interaction.request.rotation.RotationMode
27-
import com.lambda.interaction.request.rotation.visibilty.lookAt
2822
import com.lambda.module.Module
2923
import com.lambda.module.tag.ModuleTag
30-
import com.lambda.util.KeyboardUtils.isKeyPressed
31-
import com.lambda.util.math.MathUtils.toDouble
32-
import com.lambda.util.math.MathUtils.toFloatSign
33-
import com.lambda.util.player.MovementUtils.buildMovementInput
34-
import com.lambda.util.player.MovementUtils.mergeFrom
3524
import net.minecraft.client.gui.screen.ChatScreen
3625
import net.minecraft.client.gui.screen.Screen
3726
import net.minecraft.client.gui.screen.ingame.AnvilScreen
3827
import net.minecraft.client.gui.screen.ingame.CommandBlockScreen
39-
import net.minecraft.client.gui.screen.ingame.CreativeInventoryScreen
4028
import net.minecraft.client.gui.screen.ingame.SignEditScreen
41-
import net.minecraft.client.network.ClientPlayerEntity
42-
import org.lwjgl.glfw.GLFW.*
4329

4430
object InventoryMove : Module(
4531
name = "InventoryMove",
4632
description = "Allows you to move with GUIs opened",
4733
defaultTags = setOf(ModuleTag.PLAYER, ModuleTag.MOVEMENT)
4834
) {
4935
private val speed by setting("Rotation Speed", 5, 1..20, 1, unit = "°/tick")
50-
private val rotationConfig = RotationConfig.Instant(RotationMode.Lock)
5136

5237
/**
5338
* Whether the current screen has text inputs or is null
5439
*/
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-
63-
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-
80-
onRotate {
81-
if (mc.currentScreen.hasInputOrNull) return@onRotate
82-
83-
val pitch = (isKeyPressed(GLFW_KEY_DOWN, GLFW_KEY_KP_2).toFloatSign() -
84-
isKeyPressed(GLFW_KEY_UP, GLFW_KEY_KP_8).toFloatSign()) * speed
85-
val yaw = (isKeyPressed(GLFW_KEY_RIGHT, GLFW_KEY_KP_6).toFloatSign() -
86-
isKeyPressed(GLFW_KEY_LEFT, GLFW_KEY_KP_4).toFloatSign()) * speed
87-
88-
lookAt(
89-
Rotation(player.yaw + yaw, (player.pitch + pitch).coerceIn(-90f, 90f))
90-
).requestBy(rotationConfig)
40+
@JvmStatic
41+
fun hasInputOrNull(screen: Screen?) =
42+
screen is ChatScreen ||
43+
screen is SignEditScreen ||
44+
screen is AnvilScreen ||
45+
screen is CommandBlockScreen ||
46+
screen is LambdaScreen ||
47+
screen == null
48+
49+
@JvmStatic
50+
fun isKeyMovementRelated(key: Int): Boolean {
51+
val options = mc.options
52+
return when (key) {
53+
options.forwardKey.boundKey.code,
54+
options.backKey.boundKey.code,
55+
options.leftKey.boundKey.code,
56+
options.rightKey.boundKey.code,
57+
options.jumpKey.boundKey.code,
58+
options.sprintKey.boundKey.code,
59+
options.sneakKey.boundKey.code -> true
60+
else -> false
9161
}
9262
}
9363
}

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)