Skip to content

Commit 66e9e42

Browse files
committed
Improve handling of toggle keys
Fixes toggle keys from other mods getting toggled every frame (gh-18)
1 parent 5a07453 commit 66e9e42

File tree

2 files changed

+39
-21
lines changed

2 files changed

+39
-21
lines changed

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

+37-21
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import me.pieking1215.invmove.module.VanillaModule16;
77
import net.minecraft.client.KeyMapping;
88
import net.minecraft.client.Minecraft;
9+
import net.minecraft.client.ToggleKeyMapping;
910
import net.minecraft.client.gui.screens.Screen;
1011
import net.minecraft.client.player.Input;
1112
import net.minecraft.network.chat.MutableComponent;
1213

13-
import javax.annotation.Nullable;
1414
import java.io.File;
1515
import java.lang.reflect.Field;
1616
import java.util.ArrayList;
1717
import java.util.Arrays;
1818
import java.util.HashMap;
1919
import java.util.List;
20+
import java.util.Map;
2021
import java.util.Optional;
2122

2223
public abstract class InvMove {
@@ -71,9 +72,10 @@ public static void registerModule(Module module) {
7172
// implementation
7273

7374
protected boolean wasSneaking = false;
74-
protected boolean wasShiftDown = false;
7575
protected boolean wasToggleMovementPressed = false;
7676

77+
protected Map<ToggleKeyMapping, Boolean> wasToggleKeyDown = new HashMap<>();
78+
7779
public final List<Module> modules = new ArrayList<>();
7880

7981
public InvMove() {
@@ -153,31 +155,43 @@ public void onInputUpdate(Input input){
153155
canMove = handleToggleMovementKey(Minecraft.getInstance().screen, canMove);
154156

155157
if(canMove){
156-
157158
// tick keybinds (since opening the ui unpresses all keys)
158159

159-
if (optionToggleCrouch()) {
160-
// TODO: think about doing this a better way
161-
162-
// save whether it was toggled
163-
boolean wasCrouchToggle = Minecraft.getInstance().options.keyShift.isDown;
160+
// edited implementation of KeyMapping.setAll()
161+
// using normal setAll breaks toggle keys so we have to do it manually
162+
// TODO: maybe it would be better to modify KeyboardHandler::keyPress to hook key presses instead of doing it this way
163+
for (KeyMapping k : KeyMapping.ALL.values()) {
164+
if (k.key.getType() == InputConstants.Type.KEYSYM && k.key.getValue() != InputConstants.UNKNOWN.getValue()) {
165+
166+
boolean raw = InputConstants.isKeyDown(Minecraft.getInstance().getWindow().getWindow(), k.key.getValue());
167+
168+
// if is a toggle key in toggle mode
169+
if (k instanceof ToggleKeyMapping && ((ToggleKeyMapping)k).needsToggle.getAsBoolean()) {
170+
// special handling for toggle keys
171+
172+
// manually handle toggling
173+
if (wasToggleKeyDown.containsKey(k)) {
174+
if (!wasToggleKeyDown.get(k) && raw) {
175+
// TODO: add a "boolean allowKey(KeyBinding);" method to Module instead of hardcoding only for sneak
176+
if (k == Minecraft.getInstance().options.keyShift) {
177+
if (InvMoveConfig.MOVEMENT.SNEAK.get() == InvMoveConfig.Movement.SneakMode.Pressed) {
178+
k.setDown(true);
179+
}
180+
} else {
181+
k.setDown(true);
182+
}
183+
}
184+
}
164185

165-
// make it not toggle, and see if the key was pressed
166-
setOptionToggleCrouch(false);
167-
KeyMapping.setAll();
168-
setOptionToggleCrouch(true);
186+
wasToggleKeyDown.put((ToggleKeyMapping) k, raw);
169187

170-
// manually toggle crouch
171-
boolean nowShift = Minecraft.getInstance().options.keyShift.isDown;
172-
if (InvMoveConfig.MOVEMENT.SNEAK.get() == InvMoveConfig.Movement.SneakMode.Pressed && !wasShiftDown && nowShift) {
173-
Minecraft.getInstance().options.keyShift.isDown = !wasCrouchToggle;
174-
} else {
175-
Minecraft.getInstance().options.keyShift.isDown = wasCrouchToggle;
188+
} else {
189+
// normal setAll behavior
190+
k.setDown(raw);
191+
}
176192
}
177-
wasShiftDown = nowShift;
178-
} else {
179-
KeyMapping.setAll();
180193
}
194+
181195
// Minecraft.getInstance().screen.passEvents = true;
182196

183197
// this is needed for compatibility with ItemPhysic
@@ -205,6 +219,8 @@ public void onInputUpdate(Input input){
205219
}
206220

207221
// tick movement
222+
// TODO: consider mixing into KeyboardInput::tick or KeyMapping::isDown instead of this for better compatibility
223+
// that would also fix swift sneak (gh-21)
208224
manualTickMovement(input, Minecraft.getInstance().player.isMovingSlowly(), Minecraft.getInstance().player.isSpectator());
209225

210226
// set sprinting using raw keybind data
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
accessWidener v1 named
22
accessible field net/minecraft/client/KeyMapping isDown Z
33
accessible field net/minecraft/client/KeyMapping key Lcom/mojang/blaze3d/platform/InputConstants$Key;
4+
accessible field net/minecraft/client/KeyMapping ALL Ljava/util/Map;
5+
accessible field net/minecraft/client/ToggleKeyMapping needsToggle Ljava/util/function/BooleanSupplier;
46
accessible class net/minecraft/client/gui/screens/inventory/MerchantScreen$TradeOfferButton

0 commit comments

Comments
 (0)