InventoryMove module

InventoryMove: Minecraft 26.3 cheat module, full source for every implementation (2 clients: Combatant, LiquidBounce (Kotlin)).

2 implementationsMOVEMENT

Full module source: 2 implementations

Every implementation below is the complete module file, verbatim. Expand a client to read its code.

CombatantMOVEMENTsrc/main/java/combatant/client/features/module/modules/movement/InventoryMove.javaJava · 501 lines · 19.1 KB
Settingsmodecancel_server_closetext_fields_disablemovement_togglesallowed_screensblocked_screens
InventoryMove.javaJava · Minecraft 26.2 · 501 lines · 19.1 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of the Combatant Client distribution.
3
 * Combatant modifications copyright (c) 2026 pivosos2007.
4
 *
5
 * Portions of this file are based on InvMove
6
 * (https://github.com/PieKing1215/InvMove).
7
 * Copyright (c) PieKing1215 and contributors.
8
 *
9
 * Original portions remain under LGPLv3.
10
 * Combatant modifications are licensed under GPLv3.
11
 * See THIRD_PARTY_NOTICES.md for details.
12
 */
13
14
package combatant.client.features.module.modules.movement;
15
16
import com.mojang.blaze3d.platform.InputConstants;
17
import combatant.client.util.screen.ClientScreen;
18
import net.minecraft.client.KeyMapping;
19
import net.minecraft.client.Minecraft;
20
import net.minecraft.client.gui.components.EditBox;
21
import net.minecraft.client.gui.screens.PauseScreen;
22
import net.minecraft.client.gui.screens.Screen;
23
import net.minecraft.client.gui.screens.inventory.*;
24
import net.minecraft.network.protocol.Packet;
25
import net.minecraft.network.protocol.game.ClientboundContainerClosePacket;
26
import net.minecraft.network.protocol.game.ServerboundContainerClickPacket;
27
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
28
import net.minecraft.world.entity.player.Input;
29
import combatant.client.config.values.BooleanMapValue;
30
import combatant.client.config.values.BooleanValue;
31
import combatant.client.config.values.EnumValue;
32
import combatant.client.config.values.SetValue;
33
import combatant.client.events.EventHandler;
34
import combatant.client.events.impl.MovementInputEvent;
35
import combatant.client.events.impl.PacketEvent;
36
import combatant.client.features.gui.clickgui.ClickGuiEditorScreen;
37
import combatant.client.features.gui.clickgui.ClickGuiPickerScreen;
38
import combatant.client.features.gui.clickgui.ClickGuiRenderer;
39
import combatant.client.features.gui.clickgui.ClickGuiScreen;
40
import combatant.client.features.gui.clickgui.settings.TextListSetting;
41
import combatant.client.features.gui.clickgui.layout.screen.settings.implement.relations.RelationsComponent;
42
import combatant.client.features.module.Module;
43
import combatant.client.features.module.ModuleCategory;
44
import combatant.client.features.module.ModuleInfo;
45
import combatant.client.mixins.accessors.InputAccessor;
46
import combatant.client.util.screen.ScreenCatalog;
47
48
import java.util.ArrayList;
49
import java.util.LinkedHashSet;
50
import java.util.List;
51
import java.util.Set;
52
53
/**
54
 * Inventory movement and per-screen policy system for Combatant.
55
 * <p>
56
 * Credit:
57
 * Behavior design and feature scope were adapted from the InvMove mod by pieking1215.
58
 */
59
//todo Description
60
@ModuleInfo(
61
        id = "inventorymove",
62
        displayName = "InventoryMove",
63
        category = ModuleCategory.MOVEMENT
64
)
65
public class InventoryMove extends Module {
66
67
    private static final long STOP_TIMEOUT_MS = 100L;
68
    private static final long RESUME_DELAY_MS = 40L;
69
    private static final double STOP_VELOCITY = 0.03;
70
    private static final String TOGGLE_JUMP = "Jump";
71
    private static final String TOGGLE_SNEAK = "Sneak";
72
    private static final String TOGGLE_SPRINT = "Sprint";
73
    private final Minecraft mc = Minecraft.getInstance();
74
    private final EnumValue<Mode> mode = enumSetting("mode", "mode", Mode.NORMAL, Mode.values());
75
    private final BooleanValue cancelServerClose =
76
            visibleWhen(bool("cancel_server_close", true), () -> mode.get() == Mode.GRIM);
77
    private final BooleanValue textFieldDisablesMovement = bool("text_fields_disable", true);
78
    private final BooleanMapValue movementToggles = group("movement_toggles", defaultMovementToggles());
79
    private final SetValue allowedScreens =
80
            textList("allowed_screens", "allowed_screens", TextListSetting.PickerMode.SCREENS, defaultAllowedScreens());
81
    private final SetValue blockedScreens =
82
            textList("blocked_screens", "blocked_screens", TextListSetting.PickerMode.SCREENS, defaultBlockedScreens());
83
    private final List<ServerboundContainerClickPacket> heldPackets = new ArrayList<>();
84
    private GrimPhase grimPhase = GrimPhase.IDLE;
85
    private long phaseStartedAt = 0L;
86
    private int pendingCloseSyncId = -1;
87
    private boolean flushingPackets = false;
88
    private boolean allowClosePacket = false;
89
    private boolean movementBlocked = false;
90
91
    private static Set<String> defaultAllowedScreens() {
92
        LinkedHashSet<String> screens = new LinkedHashSet<>();
93
        screens.add(AbstractContainerScreen.class.getName());
94
        screens.add(PauseScreen.class.getName());
95
        screens.add(ScreenCatalog.CLICK_GUI);
96
        screens.add(ScreenCatalog.CLICK_GUI_EDITOR);
97
        screens.add(ScreenCatalog.CLICK_GUI_PICKER);
98
        screens.add("pivosos2007.gui.clickgui.ClickGuiScreen");
99
        screens.add("pivosos2007.gui.clickgui.ClickGuiPickerScreen");
100
        screens.add("pivosos2007.gui.clickgui.ClickGuiEditorScreen");
101
        screens.add("pivosos2007.features.gui.clickgui.ClickGuiScreen");
102
        screens.add("pivosos2007.features.gui.clickgui.ClickGuiEditorScreen");
103
        screens.add("pivosos2007.features.gui.clickgui.ClickGuiPickerScreen");
104
        screens.add(CreativeModeInventoryScreen.class.getName());
105
        screens.add(InventoryScreen.class.getName());
106
        screens.add(ContainerScreen.class.getName());
107
        return screens;
108
    }
109
110
    private static java.util.Map<String, Boolean> defaultMovementToggles() {
111
        java.util.LinkedHashMap<String, Boolean> toggles = new java.util.LinkedHashMap<>();
112
        toggles.put(TOGGLE_JUMP, true);
113
        toggles.put(TOGGLE_SNEAK, true);
114
        toggles.put(TOGGLE_SPRINT, true);
115
        return toggles;
116
    }
117
118
    private static Set<String> defaultBlockedScreens() {
119
        return new LinkedHashSet<>(Set.of(
120
                AbstractSignEditScreen.class.getName(),
121
                AbstractCommandBlockEditScreen.class.getName(),
122
                StructureBlockEditScreen.class.getName(),
123
                AnvilScreen.class.getName(),
124
                BookEditScreen.class.getName(),
125
                BookSignScreen.class.getName()
126
        ));
127
    }
128
129
    @Override
130
    public void onEnable() {
131
        resetGrimState();
132
        syncMovementKeysFromPhysical();
133
    }
134
135
    @Override
136
    public void onDisable() {
137
        resetGrimState();
138
        syncMovementKeysFromPhysical();
139
    }
140
141
    @Override
142
    public void onTick() {
143
        if (ClientScreen.current() != null) {
144
            ScreenCatalog.registerSeen(ClientScreen.current());
145
        }
146
147
        if (mc.player == null) return;
148
149
        if (mode.get() == Mode.NORMAL) {
150
            tickNormal();
151
            return;
152
        }
153
154
        tickGrim();
155
    }
156
157
    @EventHandler
158
    public void onMovementInput(MovementInputEvent event) {
159
        if (mc.player == null || ClientScreen.current() == null) {
160
            return;
161
        }
162
163
        if (mode.get() == Mode.NORMAL) {
164
            applyMovementToEvent(event, isMovementAllowed(ClientScreen.current()));
165
            return;
166
        }
167
168
        if (movementBlocked || isBlockingGrimPhase()) {
169
            clearMovementEvent(event);
170
            return;
171
        }
172
173
        applyMovementToEvent(event, isMovementAllowed(ClientScreen.current()));
174
    }
175
176
    private boolean isBlockingGrimPhase() {
177
        return grimPhase == GrimPhase.STOPPING
178
                || grimPhase == GrimPhase.FLUSHING
179
                || grimPhase == GrimPhase.CLOSING;
180
    }
181
182
    private void applyMovementToEvent(MovementInputEvent event, boolean canMove) {
183
        if (!canMove || mc.getWindow() == null) {
184
            clearMovementEvent(event);
185
            return;
186
        }
187
188
        event.setForward(isPhysicallyPressed(mc.options.keyUp));
189
        event.setBackward(isPhysicallyPressed(mc.options.keyDown));
190
        event.setLeft(isPhysicallyPressed(mc.options.keyLeft));
191
        event.setRight(isPhysicallyPressed(mc.options.keyRight));
192
        event.setJump(movementToggles.get(TOGGLE_JUMP) && isPhysicallyPressed(mc.options.keyJump));
193
        event.setSneak(movementToggles.get(TOGGLE_SNEAK) && isPhysicallyPressed(mc.options.keyShift));
194
        event.setSprint(movementToggles.get(TOGGLE_SPRINT) && isPhysicallyPressed(mc.options.keySprint));
195
    }
196
197
    private void clearMovementEvent(MovementInputEvent event) {
198
        event.setForward(false);
199
        event.setBackward(false);
200
        event.setLeft(false);
201
        event.setRight(false);
202
        event.setJump(false);
203
        event.setSprint(false);
204
        event.setSneak(false);
205
206
        if (mc.player != null && mc.player.input != null) {
207
            ((InputAccessor) mc.player.input).setPlayerInput(new Input(false, false, false, false, false, false, false));
208
        }
209
    }
210
211
    @EventHandler
212
    public void onPacketSend(PacketEvent.Send event) {
213
        if (mode.get() != Mode.GRIM || mc.player == null) return;
214
215
        Packet<?> packet = event.getPacket();
216
        if (packet instanceof ServerboundContainerClickPacket click) {
217
            if (flushingPackets) return;
218
            if (shouldBufferClickPacket(click)) {
219
                heldPackets.add(click);
220
                event.cancel();
221
            }
222
            return;
223
        }
224
225
        if (packet instanceof ServerboundContainerClosePacket close) {
226
            if (allowClosePacket) return;
227
            if (requestClose(close.getContainerId())) {
228
                event.cancel();
229
            }
230
        }
231
    }
232
233
    @EventHandler
234
    public void onPacketReceive(PacketEvent.Receive event) {
235
        if (mode.get() != Mode.GRIM || !cancelServerClose.get()) return;
236
        Packet<?> packet = event.getPacket();
237
        if (packet instanceof ClientboundContainerClosePacket close && close.getContainerId() == 0) {
238
            event.cancel();
239
        }
240
    }
241
242
    public boolean shouldCancelHandledScreenClose(AbstractContainerScreen<?> screen) {
243
        return mode.get() == Mode.GRIM && requestClose(syncId(screen));
244
    }
245
246
    public boolean shouldCancelCloseHandledScreen() {
247
        if (mode.get() != Mode.GRIM || mc.player == null) return false;
248
        if (!(ClientScreen.current() instanceof AbstractContainerScreen<?>)) return false;
249
        return requestClose(mc.player.containerMenu.containerId);
250
    }
251
252
    private void tickNormal() {
253
        applyMovementForCurrentScreen(isMovementAllowed(ClientScreen.current()));
254
    }
255
256
    private void tickGrim() {
257
        boolean grimManaged = shouldManageCurrentHandledScreen();
258
259
        if (grimManaged) {
260
            if (grimPhase == GrimPhase.IDLE) {
261
                grimPhase = GrimPhase.ACTIVE;
262
            }
263
            if (grimPhase == GrimPhase.ACTIVE) {
264
                movementBlocked = false;
265
                syncMovementKeysFromPhysical();
266
            }
267
        } else if (grimPhase == GrimPhase.ACTIVE) {
268
            resetGrimState();
269
        }
270
271
        if (!grimManaged && grimPhase == GrimPhase.IDLE) {
272
            movementBlocked = false;
273
            applyMovementForCurrentScreen(isMovementAllowed(ClientScreen.current()));
274
            return;
275
        }
276
277
        long elapsed = System.currentTimeMillis() - phaseStartedAt;
278
        switch (grimPhase) {
279
            case STOPPING -> {
280
                movementBlocked = true;
281
                releaseMovementKeys();
282
                if (isPlayerStopped() || elapsed >= STOP_TIMEOUT_MS) {
283
                    grimPhase = heldPackets.isEmpty() ? GrimPhase.CLOSING : GrimPhase.FLUSHING;
284
                    phaseStartedAt = System.currentTimeMillis();
285
                }
286
            }
287
            case FLUSHING -> {
288
                movementBlocked = true;
289
                releaseMovementKeys();
290
                flushHeldPackets();
291
                grimPhase = GrimPhase.CLOSING;
292
                phaseStartedAt = System.currentTimeMillis();
293
            }
294
            case CLOSING -> {
295
                movementBlocked = true;
296
                releaseMovementKeys();
297
                closeInventoryNow();
298
                grimPhase = GrimPhase.RESUMING;
299
                phaseStartedAt = System.currentTimeMillis();
300
            }
301
            case RESUMING -> {
302
                movementBlocked = false;
303
                syncMovementKeysFromPhysical();
304
                if (elapsed >= RESUME_DELAY_MS) {
305
                    resetGrimState();
306
                    if (shouldManageCurrentHandledScreen()) {
307
                        grimPhase = GrimPhase.ACTIVE;
308
                    } else {
309
                        applyMovementForCurrentScreen(isMovementAllowed(ClientScreen.current()));
310
                    }
311
                }
312
            }
313
            default -> {
314
            }
315
        }
316
    }
317
318
    private boolean requestClose(int syncId) {
319
        if (!shouldManageCurrentHandledScreen()) return false;
320
        if (syncId < 0) return false;
321
        if (heldPackets.isEmpty() && !hasGrimInventoryMovementRisk()) return false;
322
        if (grimPhase != GrimPhase.ACTIVE && grimPhase != GrimPhase.IDLE) return true;
323
324
        pendingCloseSyncId = syncId;
325
        movementBlocked = hasGrimInventoryMovementRisk();
326
        grimPhase = movementBlocked ? GrimPhase.STOPPING : (heldPackets.isEmpty() ? GrimPhase.CLOSING : GrimPhase.FLUSHING);
327
        phaseStartedAt = System.currentTimeMillis();
328
        return true;
329
    }
330
331
    private void flushHeldPackets() {
332
        if (heldPackets.isEmpty() || mc.getConnection() == null) return;
333
        flushingPackets = true;
334
        try {
335
            for (ServerboundContainerClickPacket packet : heldPackets) {
336
                mc.getConnection().send(packet);
337
            }
338
        } finally {
339
            heldPackets.clear();
340
            flushingPackets = false;
341
        }
342
    }
343
344
    private void closeInventoryNow() {
345
        if (mc.player == null || mc.getConnection() == null) return;
346
        if (pendingCloseSyncId >= 0) {
347
            allowClosePacket = true;
348
            try {
349
                mc.getConnection().send(new ServerboundContainerClosePacket(pendingCloseSyncId));
350
            } finally {
351
                allowClosePacket = false;
352
            }
353
        }
354
        mc.player.clientSideCloseContainer();
355
        pendingCloseSyncId = -1;
356
    }
357
358
    private boolean shouldBufferClickPacket(ServerboundContainerClickPacket packet) {
359
        return shouldManageCurrentHandledScreen()
360
                && packet.containerId() == syncId(ClientScreen.current())
361
                && (!heldPackets.isEmpty() || hasGrimInventoryMovementRisk() || grimPhase == GrimPhase.STOPPING);
362
    }
363
364
    private boolean shouldManageCurrentHandledScreen() {
365
        return syncId(ClientScreen.current()) >= 0 && isMovementAllowed(ClientScreen.current());
366
    }
367
368
    private boolean isMovementAllowed(Screen screen) {
369
        if (screen == null || mc.player == null) return false;
370
        if (isClickGuiTextInputActive(screen)) return false;
371
        if (textFieldDisablesMovement.get() && hasActiveTextField(screen)) return false;
372
        if (ScreenCatalog.matches(screen, blockedScreens.get())) return false;
373
        if (isSafeVanillaNonInventoryScreen(screen)) return true;
374
        return ScreenCatalog.matches(screen, allowedScreens.get());
375
    }
376
377
    private boolean isSafeVanillaNonInventoryScreen(Screen screen) {
378
        if (screen == null || screen instanceof AbstractContainerScreen<?>) return false;
379
380
        String name = screen.getClass().getName();
381
        return name.startsWith("net.minecraft.client.gui.screen.");
382
    }
383
384
    private boolean isClickGuiTextInputActive(Screen screen) {
385
        if (!(screen instanceof ClickGuiScreen
386
                || screen instanceof ClickGuiEditorScreen
387
                || screen instanceof ClickGuiPickerScreen)) {
388
            return false;
389
        }
390
        return ClickGuiRenderer.isBlockingModuleKeybinds() || RelationsComponent.isMovementInputBlocked();
391
    }
392
393
    private boolean hasActiveTextField(Screen screen) {
394
        if (screen == null) return false;
395
        return screen.getFocused() instanceof EditBox field && field.canConsumeInput();
396
    }
397
398
    private int syncId(Screen screen) {
399
        if (!(screen instanceof AbstractContainerScreen<?> handled)) return -1;
400
        return handled.getMenu().containerId;
401
    }
402
403
    private boolean isPlayerStopped() {
404
        if (mc.player == null) return true;
405
        return Math.abs(mc.player.getDeltaMovement().x) < STOP_VELOCITY
406
                && Math.abs(mc.player.getDeltaMovement().z) < STOP_VELOCITY;
407
    }
408
409
    private void resetGrimState() {
410
        heldPackets.clear();
411
        grimPhase = GrimPhase.IDLE;
412
        phaseStartedAt = 0L;
413
        pendingCloseSyncId = -1;
414
        flushingPackets = false;
415
        allowClosePacket = false;
416
        movementBlocked = false;
417
    }
418
419
    private void applyMovementForCurrentScreen(boolean canMove) {
420
        if (ClientScreen.current() == null) return;
421
        if (canMove) {
422
            syncMovementKeysFromPhysical();
423
        } else {
424
            releaseMovementKeys();
425
        }
426
    }
427
428
    private void syncMovementKeysFromPhysical() {
429
        if (mc.player == null || mc.getWindow() == null) return;
430
        syncKey(mc.options.keyUp, true);
431
        syncKey(mc.options.keyDown, true);
432
        syncKey(mc.options.keyLeft, true);
433
        syncKey(mc.options.keyRight, true);
434
        syncKey(mc.options.keyJump, movementToggles.get(TOGGLE_JUMP));
435
        syncKey(mc.options.keyShift, movementToggles.get(TOGGLE_SNEAK));
436
        syncKey(mc.options.keySprint, movementToggles.get(TOGGLE_SPRINT));
437
    }
438
439
    private void releaseMovementKeys() {
440
        mc.options.keyUp.setDown(false);
441
        mc.options.keyDown.setDown(false);
442
        mc.options.keyLeft.setDown(false);
443
        mc.options.keyRight.setDown(false);
444
        mc.options.keyJump.setDown(false);
445
        mc.options.keyShift.setDown(false);
446
        mc.options.keySprint.setDown(false);
447
    }
448
449
    private void syncKey(KeyMapping key, boolean allowed) {
450
        key.setDown(allowed && isPhysicallyPressed(key));
451
    }
452
453
    private boolean isPhysicallyPressed(KeyMapping key) {
454
        InputConstants.Key boundKey = InputConstants.getKey(key.saveString());
455
        return InputConstants.isKeyDown(mc.getWindow(), boundKey.getValue());
456
    }
457
458
    private boolean hasPhysicalMovementInput() {
459
        return mc.getWindow() != null
460
                && (isPhysicallyPressed(mc.options.keyUp)
461
                || isPhysicallyPressed(mc.options.keyDown)
462
                || isPhysicallyPressed(mc.options.keyLeft)
463
                || isPhysicallyPressed(mc.options.keyRight)
464
                || (movementToggles.get(TOGGLE_JUMP) && isPhysicallyPressed(mc.options.keyJump))
465
                || (movementToggles.get(TOGGLE_SNEAK) && isPhysicallyPressed(mc.options.keyShift)));
466
    }
467
468
    private boolean hasGrimInventoryMovementRisk() {
469
        if (mc.player == null) return false;
470
        if (mc.player.isSprinting() && (!mc.player.isSwimming() || mc.player.onGround())) return true;
471
        if (hasPhysicalGrimMovingInput()) return true;
472
        if (mc.player.input == null || mc.player.input.keyPresses == null) return false;
473
474
        Input input = mc.player.input.keyPresses;
475
        return input.forward() || input.backward() || input.left() || input.right() || input.jump();
476
    }
477
478
    private boolean hasPhysicalGrimMovingInput() {
479
        return mc.getWindow() != null
480
                && (isPhysicallyPressed(mc.options.keyUp)
481
                || isPhysicallyPressed(mc.options.keyDown)
482
                || isPhysicallyPressed(mc.options.keyLeft)
483
                || isPhysicallyPressed(mc.options.keyRight)
484
                || (movementToggles.get(TOGGLE_JUMP) && isPhysicallyPressed(mc.options.keyJump)));
485
    }
486
487
    public enum Mode {
488
        NORMAL,
489
        GRIM
490
    }
491
492
    private enum GrimPhase {
493
        IDLE,
494
        ACTIVE,
495
        STOPPING,
496
        FLUSHING,
497
        CLOSING,
498
        RESUMING
499
    }
500
}
501
LiquidBounce (Kotlin)MOVEMENTsrc/main/kotlin/net/ccbluex/liquidbounce/features/module/modules/movement/inventorymove/ModuleInventoryMove.ktKotlin · 178 lines · 7.5 KB

Allows you to walk while an inventory is opened.

SettingsBehaviorPassthroughSneak
ModuleInventoryMove.ktKotlin · Minecraft 26.2 · 178 lines · 7.5 KB · complete file, verbatimOriginal ↗
1
/*
2
 * This file is part of LiquidBounce (https://github.com/CCBlueX/LiquidBounce)
3
 *
4
 * Copyright (c) 2015 - 2026 CCBlueX
5
 *
6
 * LiquidBounce is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * LiquidBounce is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with LiquidBounce. If not, see <https://www.gnu.org/licenses/>.
18
 */
19
package net.ccbluex.liquidbounce.features.module.modules.movement.inventorymove
20
21
import com.mojang.blaze3d.platform.InputConstants
22
import net.ccbluex.fastutil.fastIterable
23
import net.ccbluex.fastutil.referenceBooleanArrayMapOf
24
import net.ccbluex.liquidbounce.config.types.list.Tagged
25
import net.ccbluex.liquidbounce.event.events.KeyboardKeyEvent
26
import net.ccbluex.liquidbounce.event.events.MovementInputEvent
27
import net.ccbluex.liquidbounce.event.events.PacketEvent
28
import net.ccbluex.liquidbounce.event.events.PlayerContainerInputEvent
29
import net.ccbluex.liquidbounce.event.handler
30
import net.ccbluex.liquidbounce.features.module.ClientModule
31
import net.ccbluex.liquidbounce.features.module.ModuleCategories
32
import net.ccbluex.liquidbounce.features.module.modules.movement.inventorymove.features.InventoryMoveBlinkFeature
33
import net.ccbluex.liquidbounce.features.module.modules.movement.inventorymove.features.InventoryMoveSneakControlFeature
34
import net.ccbluex.liquidbounce.features.module.modules.movement.inventorymove.features.InventoryMoveSprintControlFeature
35
import net.ccbluex.liquidbounce.features.module.modules.movement.inventorymove.features.InventoryMoveTimerFeature
36
import net.ccbluex.liquidbounce.features.module.modules.render.ModuleClickGui
37
import net.ccbluex.liquidbounce.utils.inventory.InventoryManager
38
import net.ccbluex.liquidbounce.utils.inventory.isInInventoryScreen
39
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FINAL_DECISION
40
import net.ccbluex.liquidbounce.utils.kotlin.EventPriorityConvention.FIRST_PRIORITY
41
import net.ccbluex.liquidbounce.utils.movement.DirectionalInput
42
import net.ccbluex.liquidbounce.utils.network.isC2SContainerPacket
43
import net.ccbluex.liquidbounce.utils.network.sendCloseInventory
44
import net.ccbluex.liquidbounce.utils.network.sendPacketSilently
45
import net.minecraft.client.KeyMapping
46
import net.minecraft.client.gui.components.EditBox
47
import net.minecraft.client.gui.components.MultiLineEditBox
48
import net.minecraft.client.gui.screens.Screen
49
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen
50
import net.minecraft.client.gui.screens.inventory.InventoryScreen
51
import net.minecraft.client.input.KeyEvent
52
import net.minecraft.network.protocol.Packet
53
import net.minecraft.world.entity.player.Input
54
55
/**
56
 * InventoryMove module
57
 *
58
 * Allows you to walk while an inventory is opened.
59
 */
60
61
object ModuleInventoryMove : ClientModule("InventoryMove", ModuleCategories.MOVEMENT) {
62
63
    private val behavior by enumChoice("Behavior", Behaviour.NORMAL).also(::tagBy)
64
65
    @Suppress("unused")
66
    enum class Behaviour(override val tag: String, val handleScreens: (Screen) -> Boolean) : Tagged {
67
        NORMAL("Normal", { !it.isInEditBox() && !ModuleClickGui.isInSearchBar }),
68
        // disable clicks while moving
69
        SAFE("Safe", { NORMAL.handleScreens(it) && (it !is AbstractContainerScreen<*> || it is InventoryScreen) }),
70
        // stop in inventory
71
        UNDETECTABLE("Undetectable", { NORMAL.handleScreens(it) && it !is AbstractContainerScreen<*> }),
72
        // stop input on inventory action
73
        STOP_ON_ACTION("StopOnAction", { NORMAL.handleScreens(it) })
74
    }
75
76
    private fun Screen.isInEditBox() = when (this.focused) {
77
        is EditBox, is MultiLineEditBox -> true
78
        else -> false
79
    }
80
81
    private val passthroughSneak by boolean("PassthroughSneak", false)
82
83
    // states of movement keys, using mc.options.<key>.isPressed doesn't work for some reason
84
    private val movementKeys =
85
        referenceBooleanArrayMapOf(
86
            mc.options.keyUp, false,
87
            mc.options.keyLeft, false,
88
            mc.options.keyDown, false,
89
            mc.options.keyRight, false,
90
            mc.options.keyJump, false,
91
            mc.options.keyShift, false,
92
        )
93
94
    init {
95
        tree(InventoryMoveSprintControlFeature)
96
        tree(InventoryMoveSneakControlFeature)
97
        tree(InventoryMoveTimerFeature)
98
        tree(InventoryMoveBlinkFeature)
99
    }
100
101
    @JvmStatic
102
    fun shouldHandleInputs(key: KeyMapping, screen: Screen? = mc.gui.screen()): Boolean {
103
        if (!running) return false
104
        val screen = screen ?: return true
105
106
        when (key) {
107
            mc.options.keyShift -> passthroughSneak
108
            mc.options.keyJump, mc.options.keyUp, mc.options.keyDown, mc.options.keyLeft, mc.options.keyRight,
109
            mc.options.keySprint -> true
110
            else -> false
111
        }.also { if (!it) return false }
112
113
        // If we are in a handled screen, we should handle the inputs only if the undetectable option is not enabled
114
        return behavior.handleScreens(screen)
115
    }
116
117
    @JvmStatic
118
    fun shouldHandleInputs(event: KeyEvent) = KeyMapping.MAP[InputConstants.getKey(event)]
119
        ?.any(::shouldHandleInputs)
120
        ?: false
121
122
    @Suppress("unused")
123
    private val keyHandler = handler<KeyboardKeyEvent> { event ->
124
        if (behavior !== Behaviour.SAFE) return@handler
125
126
        val key = movementKeys.keys.find { it.matches(KeyEvent(event.keyCode, event.scanCode, event.mods)) }
127
            ?: return@handler
128
        val pressed = shouldHandleInputs(key) && !event.isReleased
129
        movementKeys.put(key, pressed)
130
131
        if (isInInventoryScreen && InventoryManager.isInventoryOpenServerSide && pressed) {
132
            network.sendCloseInventory()
133
        }
134
    }
135
136
    private val delayedContainerPackets = mutableListOf<Packet<*>>()
137
138
    override fun onDisabled() {
139
        delayedContainerPackets.clear()
140
        super.onDisabled()
141
    }
142
143
    @Suppress("unused")
144
    private val movementInputHandler = handler<MovementInputEvent>(FINAL_DECISION) { event ->
145
        if (delayedContainerPackets.isEmpty()) return@handler
146
147
        val packetsSnapshot = delayedContainerPackets.toTypedArray()
148
        delayedContainerPackets.clear()
149
        event.sneak = false
150
        event.jump = false
151
        event.directionalInput = DirectionalInput.NONE
152
        // `schedule` will force the Runnable to be run in next loop
153
        mc.schedule { packetsSnapshot.forEach(::sendPacketSilently) }
154
    }
155
156
    @Suppress("unused")
157
    private val playerContainerInputHandler = handler<PlayerContainerInputEvent> { event ->
158
        if (behavior !== Behaviour.SAFE) return@handler
159
160
        if (event.containerId != 0) return@handler
161
        if (movementKeys.fastIterable().any { it.booleanValue && shouldHandleInputs(it.key) }) event.cancelEvent()
162
    }
163
164
    @Suppress("unused")
165
    private val packetHandler = handler<PacketEvent>(FIRST_PRIORITY) { event ->
166
        if (behavior !== Behaviour.STOP_ON_ACTION) return@handler
167
168
        val packet = event.packet
169
        if (packet.isC2SContainerPacket() && player.input.keyPresses != Input.EMPTY) {
170
            event.cancelEvent()
171
            // Here only be called from render thread because [packet] is c2s
172
            delayedContainerPackets += packet
173
        }
174
    }
175
176
177
}
178