From 0e3d7d98fe5cc07db1602c680a2596349be54b76 Mon Sep 17 00:00:00 2001 From: micle Date: Thu, 30 Sep 2021 23:25:13 +0100 Subject: [PATCH] Initial port of version 2.1.0 from the 1.16 branch. --- .../loginprotection/data/ProtectedPlayer.java | 2 +- .../data/ProtectedPlayers.java | 7 +- .../events/OnKeyPressEventHandler.java | 58 +++++- .../OnLivingSetAttackTargetEventHandler.java | 4 +- .../loginprotection/network/C2SKeyPress.java | 19 +- .../micle/loginprotection/setup/Config.java | 178 +++++++++++------- 6 files changed, 185 insertions(+), 83 deletions(-) diff --git a/src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java b/src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java index 624dace..a9af11a 100755 --- a/src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java +++ b/src/main/java/com/micle/loginprotection/data/ProtectedPlayer.java @@ -11,7 +11,7 @@ public class ProtectedPlayer { public ProtectedPlayer(UUID player_uuid) { this.player_uuid = player_uuid; - this.grace_period = (Config.POST_GRACE_DURATION.get() * 40); + this.grace_period = (Config.Server.POST_GRACE_DURATION.get() * 40); this.is_loading = true; } diff --git a/src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java b/src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java index edaef0b..104828d 100755 --- a/src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java +++ b/src/main/java/com/micle/loginprotection/data/ProtectedPlayers.java @@ -39,8 +39,10 @@ public class ProtectedPlayers { protected_players.remove(protected_player); ServerPlayer player = ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayer(player_uuid); - if (player == null || !Config.POST_GRACE_ENABLED.get()) { return; } - player.sendMessage(new TextComponent("Grace Period over!"), player_uuid); + if (player == null) { return; } + + if (!Config.Server.POST_GRACE_ENABLED.get()) { return; } + player.sendMessage(new TextComponent("[LoginProtection] Grace period ended!"), player_uuid); } public void updateGracePeriod(UUID player_uuid) { @@ -51,7 +53,6 @@ public class ProtectedPlayers { protected_player.setGracePeriod(grace_period); if (grace_period <= 0) { removePlayer(player_uuid); - System.out.println(protected_player.getPlayerUUID() + " is no longer being protected!"); } } } diff --git a/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java b/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java index 6f44862..6104af7 100755 --- a/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java +++ b/src/main/java/com/micle/loginprotection/events/OnKeyPressEventHandler.java @@ -2,19 +2,25 @@ 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 (checkKeyAllowed(instance, event.getKey())) { return; } + try { LoginProtection.INSTANCE.sendToServer(new C2SKeyPress()); } catch (NullPointerException ignored) { } @@ -23,10 +29,58 @@ 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 (checkKeyAllowed(instance, event.getKeyBinding().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.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PAUSE.toString()); } + if (key == GLFW.GLFW_KEY_F3) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.DEBUG.toString()); } + if (key == instance.options.keyFullscreen.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.FULLSCREEN.toString()); } + if (key == instance.options.keyTogglePerspective.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PERSPECTIVE.toString()); } + if (key == instance.options.keySmoothCamera.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SMOOTH_CAMERA.toString()); } + if (key == instance.options.keyScreenshot.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SCREENSHOT.toString()); } + if (key == instance.options.keySpectatorOutlines.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SPECTATOR_OUTLINES.toString()); } + if (key == instance.options.keyAdvancements.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.ADVANCEMENTS.toString()); } + if (key == instance.options.keyPlayerList.getKey().getValue()) { isAllowed = Config.Server.MAIN_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.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.CHAT.toString()); + } + if (key == instance.options.keySocialInteractions.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SOCIAL_INTERACTIONS.toString()); } + if (key == instance.options.keyLoadHotbarActivator.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.LOAD_HOTBAR_ACTIVATOR.toString()); } + if (key == instance.options.keySaveHotbarActivator.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SAVE_HOTBAR_ACTIVATOR.toString()); } + if (key == instance.options.keySwapOffhand.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SWAP_ITEM.toString()); } + if (key == instance.options.keyInventory.getKey().getValue()) { isAllowed = Config.Server.MAIN_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.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.HOTBAR.toString()); } + } + if (key == instance.options.keyDrop.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.DROP_ITEM.toString()); } + if (key == instance.options.keyUse.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.USE_ITEM.toString()); } + if (key == instance.options.keyPickItem.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.PICK_BLOCK.toString()); } + if (key == instance.options.keyAttack.getKey().getValue()) { isAllowed = Config.Server.MAIN_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.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.MOVE.toString()); + } + if (key == instance.options.keyShift.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.SNEAK.toString()); } + if (key == instance.options.keyJump.getKey().getValue()) { isAllowed = Config.Server.MAIN_KEY_ALLOW_LIST.get().contains(Config.Server.KEYS.JUMP.toString()); } + + return isAllowed; + } + } diff --git a/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java b/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java index 1349c29..c54ce95 100644 --- a/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java +++ b/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java @@ -15,8 +15,8 @@ public class OnLivingSetAttackTargetEventHandler { ProtectedPlayer player = LoginProtection.protected_players.getPlayer(target.getUUID()); if (player == null) { return; } - if (player.isLoading() && !Config.MAIN_IGNORE_PLAYER_ENABLED.get()) { return; } - if (!player.isLoading() && !Config.POST_GRACE_IGNORE_PLAYER_ENABLED.get()) { return; } + if (player.isLoading() && !Config.Server.MAIN_IGNORE_PLAYER_ENABLED.get()) { return; } + if (!player.isLoading() && !Config.Server.POST_GRACE_IGNORE_PLAYER_ENABLED.get()) { return; } ((Mob) event.getEntityLiving()).setTarget(null); } diff --git a/src/main/java/com/micle/loginprotection/network/C2SKeyPress.java b/src/main/java/com/micle/loginprotection/network/C2SKeyPress.java index 80cc816..93e3080 100755 --- a/src/main/java/com/micle/loginprotection/network/C2SKeyPress.java +++ b/src/main/java/com/micle/loginprotection/network/C2SKeyPress.java @@ -29,25 +29,26 @@ public class C2SKeyPress { if (LoginProtection.protected_players.getPlayer(sender.getUUID()) == null) { return; } if (!LoginProtection.protected_players.getPlayer(sender.getUUID()).isLoading()) { return; } - if (!Config.POST_GRACE_ENABLED.get()) { + if (!Config.Server.POST_GRACE_ENABLED.get()) { LoginProtection.protected_players.removePlayer(sender.getUUID()); + sender.sendMessage(new TextComponent("[LoginProtection] You are now seen as active."), sender.getUUID()); } else { LoginProtection.protected_players.getPlayer(sender.getUUID()).setLoading(false); - sender.sendMessage(new TextComponent("Grace Period started!"), sender.getUUID()); + sender.sendMessage(new TextComponent("[LoginProtection] Grace period started!"), sender.getUUID()); } if (sender.isInWater()) { - if (Config.POST_DROWN_ENABLED.get()) { + if (Config.Server.POST_DROWN_ENABLED.get()) { sender.setAirSupply(sender.getMaxAirSupply()); } - if (Config.POST_WATER_ENABLED.get()) { - sender.addEffect(new MobEffectInstance(MobEffects.WATER_BREATHING, Config.POST_WATER_DURATION.get()*20, 0)); + if (Config.Server.POST_WATER_ENABLED.get()) { + sender.addEffect(new MobEffectInstance(MobEffects.WATER_BREATHING, Config.Server.POST_WATER_DURATION.get()*20, 0)); } } - if (sender.isInLava() && Config.POST_LAVA_ENABLED.get()) { - sender.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, Config.POST_LAVA_DURATION.get()*20, 0)); + if (sender.isInLava() && Config.Server.POST_LAVA_ENABLED.get()) { + sender.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, Config.Server.POST_LAVA_DURATION.get()*20, 0)); } - if (sender.isOnFire() && Config.POST_FIRE_ENABLED.get()) { - sender.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, Config.POST_FIRE_DURATION.get()*20, 0)); + if (sender.isOnFire() && Config.Server.POST_FIRE_ENABLED.get()) { + sender.addEffect(new MobEffectInstance(MobEffects.FIRE_RESISTANCE, Config.Server.POST_FIRE_DURATION.get()*20, 0)); } }); context.setPacketHandled(true); diff --git a/src/main/java/com/micle/loginprotection/setup/Config.java b/src/main/java/com/micle/loginprotection/setup/Config.java index 2bf4044..54aa4aa 100644 --- a/src/main/java/com/micle/loginprotection/setup/Config.java +++ b/src/main/java/com/micle/loginprotection/setup/Config.java @@ -1,81 +1,127 @@ package com.micle.loginprotection.setup; +import com.google.common.collect.Lists; +import com.micle.loginprotection.LoginProtection; import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.fml.ModLoadingContext; +import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.config.ModConfig; +import org.apache.commons.lang3.tuple.Pair; -public class Config { - public static ForgeConfigSpec SERVER_CONFIG; +import java.util.List; - public static ForgeConfigSpec.BooleanValue POST_GRACE_ENABLED; - public static ForgeConfigSpec.BooleanValue POST_GRACE_IGNORE_PLAYER_ENABLED; - public static ForgeConfigSpec.IntValue POST_GRACE_DURATION; - public static ForgeConfigSpec.BooleanValue POST_DROWN_ENABLED; - public static ForgeConfigSpec.BooleanValue POST_WATER_ENABLED; - public static ForgeConfigSpec.IntValue POST_WATER_DURATION; - public static ForgeConfigSpec.BooleanValue POST_LAVA_ENABLED; - public static ForgeConfigSpec.IntValue POST_LAVA_DURATION; - public static ForgeConfigSpec.BooleanValue POST_FIRE_ENABLED; - public static ForgeConfigSpec.IntValue POST_FIRE_DURATION; - - public static ForgeConfigSpec.BooleanValue MAIN_IGNORE_PLAYER_ENABLED; +@Mod.EventBusSubscriber(modid = LoginProtection.MOD_ID, bus = Mod.EventBusSubscriber.Bus.MOD) +public final class Config { + public static final Server SERVER; + public static final ForgeConfigSpec SERVER_SPEC; + static { + Pair spec_pair = new ForgeConfigSpec.Builder().configure(Server::new); + SERVER = spec_pair.getLeft(); + SERVER_SPEC = spec_pair.getRight(); + } public static void init() { - initServer(); - - ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, SERVER_CONFIG); + ModLoadingContext.get().registerConfig(ModConfig.Type.SERVER, SERVER_SPEC); } - private static void initServer() { - ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder(); + public static class Server { + 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 + } - builder.comment("Main protection settings for protecting inactive (loading) players.").push("main"); - MAIN_IGNORE_PLAYER_ENABLED = builder - .comment("Whether mobs will ignore a protected player. (They will not attack/aggro)") - .define("ignorePlayerEnabled", true); - builder.pop(); + public static ForgeConfigSpec.BooleanValue POST_GRACE_ENABLED; + public static ForgeConfigSpec.BooleanValue POST_GRACE_IGNORE_PLAYER_ENABLED; + public static ForgeConfigSpec.IntValue POST_GRACE_DURATION; + public static ForgeConfigSpec.BooleanValue POST_DROWN_ENABLED; + public static ForgeConfigSpec.BooleanValue POST_WATER_ENABLED; + public static ForgeConfigSpec.IntValue POST_WATER_DURATION; + public static ForgeConfigSpec.BooleanValue POST_LAVA_ENABLED; + public static ForgeConfigSpec.IntValue POST_LAVA_DURATION; + public static ForgeConfigSpec.BooleanValue POST_FIRE_ENABLED; + public static ForgeConfigSpec.IntValue POST_FIRE_DURATION; - builder.comment("Additional protection settings that apply as soon as a player becomes active.").push("post"); - builder.push("grace_period"); - POST_GRACE_ENABLED = builder - .comment("Whether a player receives a grace period after becoming active or not.") - .define("graceEnabled", false); - POST_GRACE_DURATION = builder - .comment("Grace period duration in seconds.") - .defineInRange("graceDuration", 10, 1, Integer.MAX_VALUE/40); - POST_GRACE_IGNORE_PLAYER_ENABLED = builder - .comment("Whether mobs will ignore a player during their grace period.") - .define("graceIgnorePlayerEnabled", false); - builder.pop(); - builder.push("water_protection"); - POST_DROWN_ENABLED = builder - .comment("Whether a player's air supply gets refilled.") - .define("drownEnabled", true); - POST_WATER_ENABLED = builder - .comment("Whether a player receives water breating when in water.") - .define("waterEnabled", false); - POST_WATER_DURATION = builder - .comment("Water breathing duration in seconds.") - .defineInRange("waterDuration", 10, 1, Integer.MAX_VALUE/20); - builder.pop(); - builder.push("lava_protection"); - POST_LAVA_ENABLED = builder - .comment("Whether a player receives fire resistance when in lava.") - .define("lavaEnabled", true); - POST_LAVA_DURATION = builder - .comment("Fire resistance duration in seconds.") - .defineInRange("lavaDuration", 10, 1, Integer.MAX_VALUE/20); - builder.pop(); - builder.push("fire_protection"); - POST_FIRE_ENABLED = builder - .comment("Whether a player receives fire resistance when on fire.") - .define("fireEnabled", false); - POST_FIRE_DURATION = builder - .comment("Fire resistance duration in seconds.") - .defineInRange("fireDuration", 10, 1, Integer.MAX_VALUE/20); - builder.pop(); - builder.pop(); + public static ForgeConfigSpec.BooleanValue MAIN_IGNORE_PLAYER_ENABLED; + public static ForgeConfigSpec.ConfigValue> MAIN_KEY_ALLOW_LIST; - SERVER_CONFIG = builder.build(); + Server(ForgeConfigSpec.Builder builder) { + builder.comment("Main protection settings for protecting inactive (loading) players.").push("main"); + 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.toString(), KEYS.DEBUG.toString(), KEYS.FULLSCREEN.toString(), KEYS.PERSPECTIVE.toString(), KEYS.SMOOTH_CAMERA.toString(), + KEYS.SCREENSHOT.toString(), KEYS.SPECTATOR_OUTLINES.toString(), KEYS.ADVANCEMENTS.toString(), KEYS.PLAYER_LIST.toString(), KEYS.CHAT.toString(), KEYS.SOCIAL_INTERACTIONS.toString(), + KEYS.LOAD_HOTBAR_ACTIVATOR.toString(), KEYS.SAVE_HOTBAR_ACTIVATOR.toString(), KEYS.SWAP_ITEM.toString(), KEYS.HOTBAR.toString(), KEYS.PICK_BLOCK.toString()), o -> o instanceof String); + builder.pop(); + builder.pop(); + + builder.comment("Additional protection settings that apply as soon as a player becomes active.").push("post"); + builder.push("grace_period"); + POST_GRACE_ENABLED = builder + .comment("Whether a player receives a grace period after becoming active or not.") + .define("graceEnabled", false); + POST_GRACE_DURATION = builder + .comment("Grace period duration in seconds.") + .defineInRange("graceDuration", 10, 1, Integer.MAX_VALUE/40); + POST_GRACE_IGNORE_PLAYER_ENABLED = builder + .comment("Whether mobs will ignore a player during their grace period.") + .define("graceIgnorePlayerEnabled", false); + builder.pop(); + builder.push("water_protection"); + POST_DROWN_ENABLED = builder + .comment("Whether a player's air supply gets refilled.") + .define("drownEnabled", true); + POST_WATER_ENABLED = builder + .comment("Whether a player receives water breating when in water.") + .define("waterEnabled", false); + POST_WATER_DURATION = builder + .comment("Water breathing duration in seconds.") + .defineInRange("waterDuration", 10, 1, Integer.MAX_VALUE/20); + builder.pop(); + builder.push("lava_protection"); + POST_LAVA_ENABLED = builder + .comment("Whether a player receives fire resistance when in lava.") + .define("lavaEnabled", true); + POST_LAVA_DURATION = builder + .comment("Fire resistance duration in seconds.") + .defineInRange("lavaDuration", 10, 1, Integer.MAX_VALUE/20); + builder.pop(); + builder.push("fire_protection"); + POST_FIRE_ENABLED = builder + .comment("Whether a player receives fire resistance when on fire.") + .define("fireEnabled", false); + POST_FIRE_DURATION = builder + .comment("Fire resistance duration in seconds.") + .defineInRange("fireDuration", 10, 1, Integer.MAX_VALUE/20); + builder.pop(); + builder.pop(); + } } -} +} \ No newline at end of file