diff --git a/src/main/java/dev/micle/loginprotection/events/OnClientInputEventHandler.java b/src/main/java/dev/micle/loginprotection/events/OnClientInputEventHandler.java index ad1fde7..ee1e05a 100755 --- a/src/main/java/dev/micle/loginprotection/events/OnClientInputEventHandler.java +++ b/src/main/java/dev/micle/loginprotection/events/OnClientInputEventHandler.java @@ -2,77 +2,275 @@ package dev.micle.loginprotection.events; import dev.micle.loginprotection.LoginProtection; import dev.micle.loginprotection.data.ProtectedPlayer; -import dev.micle.loginprotection.network.ClientInputPacket; 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.KeyMapping; 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 { - @SubscribeEvent - public void KeyPressEvent(InputEvent.KeyInputEvent event) { - handle(event.getKey()); + 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 MouseClickEvent(InputEvent.ClickInputEvent event) { - handle(event.getKeyMapping().getKey().getValue()); + public void KeyInputEvent(InputEvent.KeyInputEvent event) { + handle(event.getAction(), event.getKey()); } - private static void handle(int key) { + @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 (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.ACTIVE) || - LoginProtection.getProxy().getServer() == null || minecraft.screen != null || checkIfKeyAllowed(key)) { + if (LoginProtection.getProxy().getServer() == null || minecraft.screen != null) { return; } - NetworkManager.getChannel().sendToServer(new ClientInputPacket()); - } + // 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(); - private static boolean checkIfKeyAllowed(int key) { - Minecraft minecraft = Minecraft.getInstance(); - List allowedKeys; - if (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.JOINING)) { - allowedKeys = (List) Config.Server.LOGIN_KEY_ALLOW_LIST.get(); - } else { - allowedKeys = (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); + } + } + } + } } - if (key == GLFW.GLFW_KEY_ESCAPE && allowedKeys.contains(Config.Server.KEYS.PAUSE.toString())) return true; - if (key == GLFW.GLFW_KEY_F3 && allowedKeys.contains(Config.Server.KEYS.DEBUG.toString())) return true; - if (key == minecraft.options.keyFullscreen.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.FULLSCREEN.toString())) return true; - if (key == minecraft.options.keyTogglePerspective.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.PERSPECTIVE.toString())) return true; - if (key == minecraft.options.keySmoothCamera.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SMOOTH_CAMERA.toString())) return true; - if (key == minecraft.options.keyScreenshot.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SCREENSHOT.toString())) return true; - if (key == minecraft.options.keySpectatorOutlines.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SPECTATOR_OUTLINES.toString())) return true; - if (key == minecraft.options.keyAdvancements.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.ADVANCEMENTS.toString())) return true; - if (key == minecraft.options.keyPlayerList.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.PLAYER_LIST.toString())) return true; - if ((key == minecraft.options.keyChat.getKey().getValue() || key == minecraft.options.keyCommand.getKey().getValue() || key == GLFW.GLFW_KEY_ENTER) && - allowedKeys.contains(Config.Server.KEYS.PLAYER_LIST.toString())) return true; - if (key == minecraft.options.keySocialInteractions.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SOCIAL_INTERACTIONS.toString())) return true; - if (key == minecraft.options.keyLoadHotbarActivator.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.LOAD_HOTBAR_ACTIVATOR.toString())) return true; - if (key == minecraft.options.keySaveHotbarActivator.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SAVE_HOTBAR_ACTIVATOR.toString())) return true; - if (key == minecraft.options.keySwapOffhand.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SWAP_ITEM.toString())) return true; - if (key == minecraft.options.keyInventory.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.INVENTORY.toString())) return true; - for (KeyMapping hotBarSlotKeyMapping : minecraft.options.keyHotbarSlots) - if (key == hotBarSlotKeyMapping.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.HOTBAR.toString())) return true; - if (key == minecraft.options.keyDrop.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.DROP_ITEM.toString())) return true; - if (key == minecraft.options.keyUse.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.USE_ITEM.toString())) return true; - if (key == minecraft.options.keyPickItem.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.PICK_BLOCK.toString())) return true; - if (key == minecraft.options.keyAttack.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.ATTACK.toString())) return true; - if ((key == minecraft.options.keyUp.getKey().getValue() || key == minecraft.options.keyRight.getKey().getValue() || - key == minecraft.options.keyDown.getKey().getValue() || key == minecraft.options.keyLeft.getKey().getValue() || - key == minecraft.options.keySprint.getKey().getValue()) && allowedKeys.contains(Config.Server.KEYS.MOVE.toString())) return true; - if (key == minecraft.options.keyShift.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.SNEAK.toString())) return true; - return key == minecraft.options.keyJump.getKey().getValue() && allowedKeys.contains(Config.Server.KEYS.JUMP.toString()); + // 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()); + } + } } }