|
6 | 6 | import me.pieking1215.invmove.module.VanillaModule16;
|
7 | 7 | import net.minecraft.client.KeyMapping;
|
8 | 8 | import net.minecraft.client.Minecraft;
|
| 9 | +import net.minecraft.client.ToggleKeyMapping; |
9 | 10 | import net.minecraft.client.gui.screens.Screen;
|
10 | 11 | import net.minecraft.client.player.Input;
|
11 | 12 | import net.minecraft.network.chat.MutableComponent;
|
12 | 13 |
|
13 |
| -import javax.annotation.Nullable; |
14 | 14 | import java.io.File;
|
15 | 15 | import java.lang.reflect.Field;
|
16 | 16 | import java.util.ArrayList;
|
17 | 17 | import java.util.Arrays;
|
18 | 18 | import java.util.HashMap;
|
19 | 19 | import java.util.List;
|
| 20 | +import java.util.Map; |
20 | 21 | import java.util.Optional;
|
21 | 22 |
|
22 | 23 | public abstract class InvMove {
|
@@ -71,9 +72,10 @@ public static void registerModule(Module module) {
|
71 | 72 | // implementation
|
72 | 73 |
|
73 | 74 | protected boolean wasSneaking = false;
|
74 |
| - protected boolean wasShiftDown = false; |
75 | 75 | protected boolean wasToggleMovementPressed = false;
|
76 | 76 |
|
| 77 | + protected Map<ToggleKeyMapping, Boolean> wasToggleKeyDown = new HashMap<>(); |
| 78 | + |
77 | 79 | public final List<Module> modules = new ArrayList<>();
|
78 | 80 |
|
79 | 81 | public InvMove() {
|
@@ -153,31 +155,43 @@ public void onInputUpdate(Input input){
|
153 | 155 | canMove = handleToggleMovementKey(Minecraft.getInstance().screen, canMove);
|
154 | 156 |
|
155 | 157 | if(canMove){
|
156 |
| - |
157 | 158 | // tick keybinds (since opening the ui unpresses all keys)
|
158 | 159 |
|
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 | + } |
164 | 185 |
|
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); |
169 | 187 |
|
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 | + } |
176 | 192 | }
|
177 |
| - wasShiftDown = nowShift; |
178 |
| - } else { |
179 |
| - KeyMapping.setAll(); |
180 | 193 | }
|
| 194 | + |
181 | 195 | // Minecraft.getInstance().screen.passEvents = true;
|
182 | 196 |
|
183 | 197 | // this is needed for compatibility with ItemPhysic
|
@@ -205,6 +219,8 @@ public void onInputUpdate(Input input){
|
205 | 219 | }
|
206 | 220 |
|
207 | 221 | // 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) |
208 | 224 | manualTickMovement(input, Minecraft.getInstance().player.isMovingSlowly(), Minecraft.getInstance().player.isSpectator());
|
209 | 225 |
|
210 | 226 | // set sprinting using raw keybind data
|
|
0 commit comments