package dev.micle.loginprotection.events; import dev.micle.loginprotection.LoginProtection; import dev.micle.loginprotection.data.ProtectedPlayer; import dev.micle.loginprotection.network.NetworkManager; import dev.micle.loginprotection.network.client.InputPacket; import dev.micle.loginprotection.proxy.Proxy; import dev.micle.loginprotection.setup.Config; import net.minecraft.client.Minecraft; import net.minecraft.client.Options; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.InputEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; import org.lwjgl.glfw.GLFW; import java.util.Arrays; import java.util.List; @OnlyIn(Dist.CLIENT) public class OnClientInputEventHandler { private static boolean isPausePressed = false; private static boolean isDebugPressed = false; private static boolean isFullscreenPressed = false; private static boolean isTogglePerspectivePressed = false; private static boolean isSmoothCameraPressed = false; private static boolean isScreenshotPressed = false; private static boolean isSpectatorOutlinesPressed = false; private static boolean isAdvancementsPressed = false; private static boolean isPlayerListPressed = false; private static boolean isChatPressed = false; private static boolean isChatCommandPressed = false; private static boolean isChatEnterPressed = false; private static boolean isSocialInteractionsPressed = false; private static boolean isLoadHotbarActivatorPressed = false; private static boolean isSaveHotbarActivatorPressed = false; private static boolean isSwapOffhandPressed = false; private static boolean isInventoryPressed = false; private static boolean isDropItemPressed = false; private static boolean isUseItemPressed = false; private static boolean isPickBlockPressed = false; private static boolean isAttackPressed = false; private static boolean isMoveUpPressed = false; private static boolean isMoveRightPressed = false; private static boolean isMoveDownPressed = false; private static boolean isMoveLeftPressed = false; private static boolean isMoveSprintPressed = false; private static boolean isSneakPressed = false; private static boolean isJumpPressed = false; private static final Boolean[] isHotbarPressed = new Boolean[9]; public OnClientInputEventHandler() { // Initialize isHotbarPressed to false Arrays.fill(isHotbarPressed, false); } @SubscribeEvent public void KeyInputEvent(InputEvent.KeyInputEvent event) { handle(event.getAction(), event.getKey()); } @SubscribeEvent public void MouseInputEvent(InputEvent.MouseInputEvent event) { handle(event.getAction(), event.getButton()); } private static void handle(int action, int key) { Minecraft minecraft = Minecraft.getInstance(); if (LoginProtection.getProxy().getServer() == null || minecraft.screen != null) { return; } // Check if any significant keys were pressed and save their state if (action == GLFW.GLFW_PRESS || action == GLFW.GLFW_RELEASE) { Options keyBinds = minecraft.options; boolean isPressed = action == GLFW.GLFW_PRESS; List allowedKeys = (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.JOINING)) ? (List) Config.Server.LOGIN_KEY_ALLOW_LIST.get() : (List) Config.Server.AFK_KEY_ALLOW_LIST.get(); if (key == GLFW.GLFW_KEY_ESCAPE) { isPausePressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.PAUSE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == GLFW.GLFW_KEY_F3) { isDebugPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.DEBUG.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyFullscreen.getKey().getValue()) { isFullscreenPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.FULLSCREEN.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyTogglePerspective.getKey().getValue()) { isTogglePerspectivePressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.PERSPECTIVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySmoothCamera.getKey().getValue()) { isSmoothCameraPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SMOOTH_CAMERA.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyScreenshot.getKey().getValue()) { isScreenshotPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SCREENSHOT.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySpectatorOutlines.getKey().getValue()) { isSpectatorOutlinesPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SPECTATOR_OUTLINES.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyAdvancements.getKey().getValue()) { isAdvancementsPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.ADVANCEMENTS.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyPlayerList.getKey().getValue()) { isPlayerListPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.PLAYER_LIST.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyChat.getKey().getValue()) { isChatPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.CHAT.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyCommand.getKey().getValue()) { isChatCommandPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.CHAT.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == GLFW.GLFW_KEY_ENTER) { isChatEnterPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.CHAT.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySocialInteractions.getKey().getValue()) { isSocialInteractionsPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SOCIAL_INTERACTIONS.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyLoadHotbarActivator.getKey().getValue()) { isLoadHotbarActivatorPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.LOAD_HOTBAR_ACTIVATOR.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySaveHotbarActivator.getKey().getValue()) { isSaveHotbarActivatorPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SAVE_HOTBAR_ACTIVATOR.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySwapOffhand.getKey().getValue()) { isSwapOffhandPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SWAP_ITEM.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyInventory.getKey().getValue()) { isInventoryPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.INVENTORY.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyDrop.getKey().getValue()) { isDropItemPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.DROP_ITEM.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyUse.getKey().getValue()) { isUseItemPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.USE_ITEM.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyPickItem.getKey().getValue()) { isPickBlockPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.PICK_BLOCK.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyAttack.getKey().getValue()) { isAttackPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.ATTACK.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyUp.getKey().getValue()) { isMoveUpPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyRight.getKey().getValue()) { isMoveRightPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyDown.getKey().getValue()) { isMoveDownPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyLeft.getKey().getValue()) { isMoveLeftPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keySprint.getKey().getValue()) { isMoveSprintPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyShift.getKey().getValue()) { isSneakPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.SNEAK.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else if (key == keyBinds.keyJump.getKey().getValue()) { isJumpPressed = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.JUMP.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } else { for (int i = 0; i < isHotbarPressed.length; i++) { if (key == keyBinds.keyHotbarSlots[i].getKey().getValue()) { isHotbarPressed[i] = isPressed; if (!allowedKeys.contains(Config.Server.KEYS.HOTBAR.toString())) { Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); } } } } } // Check if any of the pressed keys are not allowed checkIfInputIsAllowed(); } public static void checkIfInputIsAllowed() { List allowedKeys = (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.JOINING)) ? (List) Config.Server.LOGIN_KEY_ALLOW_LIST.get() : (List) Config.Server.AFK_KEY_ALLOW_LIST.get(); if ((isPausePressed && !allowedKeys.contains(Config.Server.KEYS.PAUSE.toString())) || (isDebugPressed && !allowedKeys.contains(Config.Server.KEYS.DEBUG.toString())) || (isFullscreenPressed && !allowedKeys.contains(Config.Server.KEYS.FULLSCREEN.toString())) || (isTogglePerspectivePressed && !allowedKeys.contains(Config.Server.KEYS.PERSPECTIVE.toString())) || (isSmoothCameraPressed && !allowedKeys.contains(Config.Server.KEYS.SMOOTH_CAMERA.toString())) || (isScreenshotPressed && !allowedKeys.contains(Config.Server.KEYS.SCREENSHOT.toString())) || (isSpectatorOutlinesPressed && !allowedKeys.contains(Config.Server.KEYS.SPECTATOR_OUTLINES.toString())) || (isAdvancementsPressed && !allowedKeys.contains(Config.Server.KEYS.ADVANCEMENTS.toString())) || (isPlayerListPressed && !allowedKeys.contains(Config.Server.KEYS.PLAYER_LIST.toString())) || ((isChatPressed || isChatCommandPressed || isChatEnterPressed) && !allowedKeys.contains(Config.Server.KEYS.CHAT.toString())) || (isSocialInteractionsPressed && !allowedKeys.contains(Config.Server.KEYS.SOCIAL_INTERACTIONS.toString())) || (isLoadHotbarActivatorPressed && !allowedKeys.contains(Config.Server.KEYS.LOAD_HOTBAR_ACTIVATOR.toString())) || (isSaveHotbarActivatorPressed && !allowedKeys.contains(Config.Server.KEYS.SAVE_HOTBAR_ACTIVATOR.toString())) || (isSwapOffhandPressed && !allowedKeys.contains(Config.Server.KEYS.SWAP_ITEM.toString())) || (isInventoryPressed && !allowedKeys.contains(Config.Server.KEYS.INVENTORY.toString())) || (isDropItemPressed && !allowedKeys.contains(Config.Server.KEYS.DROP_ITEM.toString())) || (isUseItemPressed && !allowedKeys.contains(Config.Server.KEYS.USE_ITEM.toString())) || (isPickBlockPressed && !allowedKeys.contains(Config.Server.KEYS.PICK_BLOCK.toString())) || (isAttackPressed && !allowedKeys.contains(Config.Server.KEYS.ATTACK.toString())) || ((isMoveUpPressed || isMoveRightPressed || isMoveDownPressed || isMoveLeftPressed || isMoveSprintPressed) && !allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) || (isSneakPressed && !allowedKeys.contains(Config.Server.KEYS.SNEAK.toString())) || (isJumpPressed && !allowedKeys.contains(Config.Server.KEYS.JUMP.toString())) || (Arrays.stream(isHotbarPressed).anyMatch(b -> b) && !allowedKeys.contains(Config.Server.KEYS.HOTBAR.toString()))) { // Assign new last input tick Proxy.Client.setLastInputTick(LoginProtection.getProxy().getClientPlayer().tickCount); // If player is not active send an input packet if (!Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.ACTIVE)) { NetworkManager.getChannel().sendToServer(new InputPacket()); } } } }