From 80eebcf308f6ff13978f60761456305f3f4cd581 Mon Sep 17 00:00:00 2001 From: micle Date: Thu, 30 Sep 2021 17:47:14 +0100 Subject: [PATCH] Implemented configurable keybinding options that will allow players perform certain actions while protected without becoming active. --- .../events/OnKeyPressEventHandler.java | 96 ++++++++++++++++++- .../micle/loginprotection/setup/Config.java | 40 ++++++++ 2 files changed, 134 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java b/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java index 6f44862..760368f 100755 --- a/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java +++ b/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java @@ -2,19 +2,66 @@ package com.micle.loginprotection.events; import com.micle.loginprotection.LoginProtection; import com.micle.loginprotection.network.C2SKeyPress; +import com.micle.loginprotection.setup.Config; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ServerData; 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; public class OnKeyPressEventHandler { @SubscribeEvent @OnlyIn(Dist.CLIENT) public void KeyPressEvent(InputEvent.KeyInputEvent event) { - ServerData server = Minecraft.getInstance().getCurrentServer(); + Minecraft instance = Minecraft.getInstance(); + ServerData server = instance.getCurrentServer(); if (server == null) { return; } + if (Minecraft.getInstance().screen != null) { return; } + + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PAUSE) && event.getKey() == GLFW.GLFW_KEY_ESCAPE) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.DEBUG) && event.getKey() == GLFW.GLFW_KEY_F3) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.FULLSCREEN) && event.getKey() == instance.options.keyFullscreen.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PERSPECTIVE) && event.getKey() == instance.options.keyTogglePerspective.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SMOOTH_CAMERA) && event.getKey() == instance.options.keySmoothCamera.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SCREENSHOT) && event.getKey() == instance.options.keyScreenshot.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SPECTATOR_OUTLINES) && event.getKey() == instance.options.keySpectatorOutlines.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.ADVANCEMENTS) && event.getKey() == instance.options.keyAdvancements.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PLAYER_LIST) && event.getKey() == instance.options.keyPlayerList.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.CHAT)) { + if (event.getKey() == instance.options.keyChat.getKey().getValue() || + event.getKey() == instance.options.keyCommand.getKey().getValue() || + event.getKey() == GLFW.GLFW_KEY_ENTER) { + return; + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SOCIAL_INTERACTIONS) && event.getKey() == instance.options.keySocialInteractions.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.LOAD_HOTBAR_ACTIVATOR) && event.getKey() == instance.options.keyLoadHotbarActivator.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SAVE_HOTBAR_ACTIVATOR) && event.getKey() == instance.options.keySaveHotbarActivator.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SWAP_ITEM) && event.getKey() == instance.options.keySwapOffhand.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.INVENTORY) && event.getKey() == instance.options.keyInventory.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.HOTBAR)) { + for (int i = 0; i < instance.options.keyHotbarSlots.length; i++) { + if (event.getKey() == instance.options.keyHotbarSlots[i].getKey().getValue()) { return; } + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.DROP_ITEM) && event.getKey() == instance.options.keyDrop.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.USE_ITEM) && event.getKey() == instance.options.keyUse.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PICK_BLOCK) && event.getKey() == instance.options.keyPickItem.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.ATTACK) && event.getKey() == instance.options.keyAttack.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.MOVE)) { + if (event.getKey() == instance.options.keyUp.getKey().getValue() || + event.getKey() == instance.options.keyRight.getKey().getValue() || + event.getKey() == instance.options.keyDown.getKey().getValue() || + event.getKey() == instance.options.keyLeft.getKey().getValue() || + event.getKey() == instance.options.keySprint.getKey().getValue()) { + return; + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SNEAK) && event.getKey() == instance.options.keyShift.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.JUMP) && event.getKey() == instance.options.keyJump.getKey().getValue()) { return; } + try { LoginProtection.INSTANCE.sendToServer(new C2SKeyPress()); } catch (NullPointerException ignored) { } @@ -23,8 +70,53 @@ public class OnKeyPressEventHandler { @SubscribeEvent @OnlyIn(Dist.CLIENT) public void MouseClickEvent(InputEvent.ClickInputEvent event) { - ServerData server = Minecraft.getInstance().getCurrentServer(); + Minecraft instance = Minecraft.getInstance(); + ServerData server = instance.getCurrentServer(); if (server == null) { return; } + if (Minecraft.getInstance().screen != null) { return; } + + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PAUSE) && event.getKeyBinding().getKey().getValue() == GLFW.GLFW_KEY_ESCAPE) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.DEBUG) && event.getKeyBinding().getKey().getValue() == GLFW.GLFW_KEY_F3) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.FULLSCREEN) && event.getKeyBinding().getKey().getValue() == instance.options.keyFullscreen.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PERSPECTIVE) && event.getKeyBinding().getKey().getValue() == instance.options.keyTogglePerspective.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SMOOTH_CAMERA) && event.getKeyBinding().getKey().getValue() == instance.options.keySmoothCamera.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SCREENSHOT) && event.getKeyBinding().getKey().getValue() == instance.options.keyScreenshot.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SPECTATOR_OUTLINES) && event.getKeyBinding().getKey().getValue() == instance.options.keySpectatorOutlines.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.ADVANCEMENTS) && event.getKeyBinding().getKey().getValue() == instance.options.keyAdvancements.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PLAYER_LIST) && event.getKeyBinding().getKey().getValue() == instance.options.keyPlayerList.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.CHAT)) { + if (event.getKeyBinding().getKey().getValue() == instance.options.keyChat.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == instance.options.keyCommand.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == GLFW.GLFW_KEY_ENTER) { + return; + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SOCIAL_INTERACTIONS) && event.getKeyBinding().getKey().getValue() == instance.options.keySocialInteractions.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.LOAD_HOTBAR_ACTIVATOR) && event.getKeyBinding().getKey().getValue() == instance.options.keyLoadHotbarActivator.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SAVE_HOTBAR_ACTIVATOR) && event.getKeyBinding().getKey().getValue() == instance.options.keySaveHotbarActivator.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SWAP_ITEM) && event.getKeyBinding().getKey().getValue() == instance.options.keySwapOffhand.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.INVENTORY) && event.getKeyBinding().getKey().getValue() == instance.options.keyInventory.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.HOTBAR)) { + for (int i = 0; i < instance.options.keyHotbarSlots.length; i++) { + if (event.getKeyBinding().getKey().getValue() == instance.options.keyHotbarSlots[i].getKey().getValue()) { return; } + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.DROP_ITEM) && event.getKeyBinding().getKey().getValue() == instance.options.keyDrop.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.USE_ITEM) && event.getKeyBinding().getKey().getValue() == instance.options.keyUse.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.PICK_BLOCK) && event.getKeyBinding().getKey().getValue() == instance.options.keyPickItem.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.ATTACK) && event.getKeyBinding().getKey().getValue() == instance.options.keyAttack.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.MOVE)) { + if (event.getKeyBinding().getKey().getValue() == instance.options.keyUp.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == instance.options.keyRight.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == instance.options.keyDown.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == instance.options.keyLeft.getKey().getValue() || + event.getKeyBinding().getKey().getValue() == instance.options.keySprint.getKey().getValue()) { + return; + } + } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.SNEAK) && event.getKeyBinding().getKey().getValue() == instance.options.keyShift.getKey().getValue()) { return; } + if (Config.MAIN_KEY_ALLOW_LIST.get().contains(Config.KEYS.JUMP) && event.getKeyBinding().getKey().getValue() == instance.options.keyJump.getKey().getValue()) { return; } + try { LoginProtection.INSTANCE.sendToServer(new C2SKeyPress()); } catch (NullPointerException ignored) { } diff --git a/src/main/java/com/micle/loginprotection/setup/Config.java b/src/main/java/com/micle/loginprotection/setup/Config.java index 2bf4044..b191140 100644 --- a/src/main/java/com/micle/loginprotection/setup/Config.java +++ b/src/main/java/com/micle/loginprotection/setup/Config.java @@ -1,10 +1,39 @@ package com.micle.loginprotection.setup; +import com.google.common.collect.Lists; import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.config.ModConfig; +import java.util.List; + public class Config { + public enum KEYS { + PAUSE, + DEBUG, + FULLSCREEN, + PERSPECTIVE, + SMOOTH_CAMERA, + SCREENSHOT, + SPECTATOR_OUTLINES, + ADVANCEMENTS, + PLAYER_LIST, + CHAT, + SOCIAL_INTERACTIONS, + LOAD_HOTBAR_ACTIVATOR, + SAVE_HOTBAR_ACTIVATOR, + SWAP_ITEM, + INVENTORY, + HOTBAR, + DROP_ITEM, + USE_ITEM, + PICK_BLOCK, + ATTACK, + MOVE, + SNEAK, + JUMP + } + public static ForgeConfigSpec SERVER_CONFIG; public static ForgeConfigSpec.BooleanValue POST_GRACE_ENABLED; @@ -19,6 +48,7 @@ public class Config { public static ForgeConfigSpec.IntValue POST_FIRE_DURATION; public static ForgeConfigSpec.BooleanValue MAIN_IGNORE_PLAYER_ENABLED; + public static ForgeConfigSpec.ConfigValue> MAIN_KEY_ALLOW_LIST; public static void init() { initServer(); @@ -33,6 +63,16 @@ public class Config { MAIN_IGNORE_PLAYER_ENABLED = builder .comment("Whether mobs will ignore a protected player. (They will not attack/aggro)") .define("ignorePlayerEnabled", true); + builder.push("allow_keys"); + MAIN_KEY_ALLOW_LIST = builder + .comment("Allowed keys players can press without becoming active.\n" + + "Available values: PAUSE, DEBUG, FULLSCREEN, PERSPECTIVE, SMOOTH_CAMERA, SCREENSHOT, SPECTATOR_OUTLINES,\n" + + "ADVANCEMENTS, PLAYER_LIST, CHAT, SOCIAL_INTERACTIONS, LOAD_HOTBAR_ACTIVATOR, SAVE_HOTBAR_ACTIVATOR,\n" + + "SWAP_ITEM, INVENTORY, HOTBAR, DROP_ITEM, USE_ITEM, PICK_BLOCK, ATTACK, MOVE, SNEAK, JUMP") + .defineList("allowedKeys", Lists.newArrayList(KEYS.PAUSE, KEYS.DEBUG, KEYS.FULLSCREEN, KEYS.PERSPECTIVE, KEYS.SMOOTH_CAMERA, + KEYS.SCREENSHOT, KEYS.SPECTATOR_OUTLINES, KEYS.ADVANCEMENTS, KEYS.PLAYER_LIST, KEYS.CHAT, KEYS.SOCIAL_INTERACTIONS, + KEYS.LOAD_HOTBAR_ACTIVATOR, KEYS.SAVE_HOTBAR_ACTIVATOR, KEYS.SWAP_ITEM, KEYS.HOTBAR, KEYS.PICK_BLOCK), o -> o instanceof KEYS); + builder.pop(); builder.pop(); builder.comment("Additional protection settings that apply as soon as a player becomes active.").push("post");