Renamed and reworked the OnClientInputEventHandler to prevent pointless packets from being sent and for it to work with the afk allowed keys too.
This commit is contained in:
@ -0,0 +1,78 @@
|
||||
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.proxy.Proxy;
|
||||
import dev.micle.loginprotection.setup.Config;
|
||||
import net.minecraft.client.KeyMapping;
|
||||
import net.minecraft.client.Minecraft;
|
||||
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.List;
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public class OnClientInputEventHandler {
|
||||
@SubscribeEvent
|
||||
public void KeyPressEvent(InputEvent.KeyInputEvent event) {
|
||||
handle(event.getKey());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void MouseClickEvent(InputEvent.ClickInputEvent event) {
|
||||
handle(event.getKeyMapping().getKey().getValue());
|
||||
}
|
||||
|
||||
private static void handle(int key) {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
if (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.ACTIVE) ||
|
||||
LoginProtection.getProxy().getServer() == null || minecraft.screen != null || !checkIfKeyAllowed(key)) {
|
||||
return;
|
||||
}
|
||||
|
||||
NetworkManager.getChannel().sendToServer(new ClientInputPacket());
|
||||
}
|
||||
|
||||
private static boolean checkIfKeyAllowed(int key) {
|
||||
Minecraft minecraft = Minecraft.getInstance();
|
||||
List<String> allowedKeys;
|
||||
if (Proxy.Client.getPlayerState().equals(ProtectedPlayer.State.JOINING)) {
|
||||
allowedKeys = (List<String>) Config.Server.LOGIN_KEY_ALLOW_LIST.get();
|
||||
} else {
|
||||
allowedKeys = (List<String>) Config.Server.AFK_KEY_ALLOW_LIST.get();
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
@ -1,89 +0,0 @@
|
||||
package dev.micle.loginprotection.events;
|
||||
|
||||
import dev.micle.loginprotection.LoginProtection;
|
||||
import dev.micle.loginprotection.network.C2SKeyPress;
|
||||
import dev.micle.loginprotection.setup.Config;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.multiplayer.ServerData;
|
||||
import net.minecraft.client.server.IntegratedServer;
|
||||
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) {
|
||||
Minecraft instance = Minecraft.getInstance();
|
||||
ServerData server_online = instance.getCurrentServer();
|
||||
IntegratedServer server_local = instance.getSingleplayerServer();
|
||||
if (server_online == null && server_local == null) { return; }
|
||||
if (Minecraft.getInstance().screen != null) { return; }
|
||||
if (checkKeyAllowed(instance, event.getKey())) { return; }
|
||||
|
||||
try {
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
} catch (NullPointerException ignored) { }
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
public void MouseClickEvent(InputEvent.ClickInputEvent event) {
|
||||
Minecraft instance = Minecraft.getInstance();
|
||||
ServerData server_online = instance.getCurrentServer();
|
||||
IntegratedServer server_local = instance.getSingleplayerServer();
|
||||
if (server_online == null && server_local == null) { return; }
|
||||
if (Minecraft.getInstance().screen != null) { return; }
|
||||
if (checkKeyAllowed(instance, event.getKeyMapping().getKey().getValue())) { return; }
|
||||
|
||||
try {
|
||||
LoginProtection.INSTANCE.sendToServer(new C2SKeyPress());
|
||||
} catch (NullPointerException ignored) { }
|
||||
}
|
||||
|
||||
@OnlyIn(Dist.CLIENT)
|
||||
private static boolean checkKeyAllowed(Minecraft instance, int key) {
|
||||
boolean isAllowed = false;
|
||||
|
||||
if (key == GLFW.GLFW_KEY_ESCAPE) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PAUSE.toString()); }
|
||||
if (key == GLFW.GLFW_KEY_F3) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.DEBUG.toString()); }
|
||||
if (key == instance.options.keyFullscreen.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.FULLSCREEN.toString()); }
|
||||
if (key == instance.options.keyTogglePerspective.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PERSPECTIVE.toString()); }
|
||||
if (key == instance.options.keySmoothCamera.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SMOOTH_CAMERA.toString()); }
|
||||
if (key == instance.options.keyScreenshot.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SCREENSHOT.toString()); }
|
||||
if (key == instance.options.keySpectatorOutlines.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SPECTATOR_OUTLINES.toString()); }
|
||||
if (key == instance.options.keyAdvancements.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.ADVANCEMENTS.toString()); }
|
||||
if (key == instance.options.keyPlayerList.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PLAYER_LIST.toString()); }
|
||||
if (key == instance.options.keyChat.getKey().getValue() ||
|
||||
key == instance.options.keyCommand.getKey().getValue() ||
|
||||
key == GLFW.GLFW_KEY_ENTER) {
|
||||
isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.CHAT.toString());
|
||||
}
|
||||
if (key == instance.options.keySocialInteractions.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SOCIAL_INTERACTIONS.toString()); }
|
||||
if (key == instance.options.keyLoadHotbarActivator.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.LOAD_HOTBAR_ACTIVATOR.toString()); }
|
||||
if (key == instance.options.keySaveHotbarActivator.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SAVE_HOTBAR_ACTIVATOR.toString()); }
|
||||
if (key == instance.options.keySwapOffhand.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SWAP_ITEM.toString()); }
|
||||
if (key == instance.options.keyInventory.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.INVENTORY.toString()); }
|
||||
for (int i = 0; i < instance.options.keyHotbarSlots.length; i++) {
|
||||
if (key == instance.options.keyHotbarSlots[i].getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.HOTBAR.toString()); }
|
||||
}
|
||||
if (key == instance.options.keyDrop.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.DROP_ITEM.toString()); }
|
||||
if (key == instance.options.keyUse.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.USE_ITEM.toString()); }
|
||||
if (key == instance.options.keyPickItem.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PICK_BLOCK.toString()); }
|
||||
if (key == instance.options.keyAttack.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.ATTACK.toString()); }
|
||||
if (key == instance.options.keyUp.getKey().getValue() ||
|
||||
key == instance.options.keyRight.getKey().getValue() ||
|
||||
key == instance.options.keyDown.getKey().getValue() ||
|
||||
key == instance.options.keyLeft.getKey().getValue() ||
|
||||
key == instance.options.keySprint.getKey().getValue()) {
|
||||
isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.MOVE.toString());
|
||||
}
|
||||
if (key == instance.options.keyShift.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SNEAK.toString()); }
|
||||
if (key == instance.options.keyJump.getKey().getValue()) { isAllowed = Config.Server.LOGIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.JUMP.toString()); }
|
||||
|
||||
return isAllowed;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user