Ported 1.17.1-2.1.1 to 1.18.1.

This commit is contained in:
2022-01-19 16:47:55 +00:00
parent d6c7a9bc4b
commit 253696a3d9
13 changed files with 525 additions and 88 deletions

View File

@ -0,0 +1,89 @@
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.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;
}
}

View File

@ -0,0 +1,23 @@
package dev.micle.loginprotection.events;
import dev.micle.loginprotection.LoginProtection;
import dev.micle.loginprotection.data.ProtectedPlayer;
import dev.micle.loginprotection.setup.Config;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnLivingSetAttackTargetEventHandler {
@SubscribeEvent
public void LivingSetAttackTargetEvent(LivingSetAttackTargetEvent event) {
if (!(event.getTarget() instanceof Player target)) { return; }
ProtectedPlayer player = LoginProtection.protected_players.getPlayer(target.getUUID());
if (player == null) { 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);
}
}

View File

@ -0,0 +1,18 @@
package dev.micle.loginprotection.events;
import dev.micle.loginprotection.LoginProtection;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.living.LivingDamageEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnPlayerDamageEventHandler {
@SubscribeEvent
public void LivingDamageEvent(LivingDamageEvent event) {
if (!(event.getEntity() instanceof Player)) { return; }
Player player = (Player) event.getEntity();
if (LoginProtection.protected_players.getPlayer(player.getUUID()) != null) {
event.setCanceled(true);
}
}
}

View File

@ -0,0 +1,17 @@
package dev.micle.loginprotection.events;
import dev.micle.loginprotection.LoginProtection;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnPlayerJoinEventHandler {
@SubscribeEvent
public void EntityJoinWorldEvent(PlayerEvent.PlayerLoggedInEvent event) {
if (!(event.getEntity() instanceof Player)) { return; }
Player player = (Player) event.getEntity();
LoginProtection.protected_players.addPlayer(player.getUUID());
System.out.println(player.getUUID() + " (" + player.getDisplayName().getString() + ") is being protected!");
}
}

View File

@ -0,0 +1,16 @@
package dev.micle.loginprotection.events;
import dev.micle.loginprotection.LoginProtection;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnPlayerLeaveEventHandler {
@SubscribeEvent
public void PlayerLeaveEvent(PlayerEvent.PlayerLoggedOutEvent event) {
if (!(event.getEntity() instanceof Player)) { return; }
Player player = event.getPlayer();
LoginProtection.protected_players.removePlayer(player.getUUID());
}
}

View File

@ -0,0 +1,14 @@
package dev.micle.loginprotection.events;
import dev.micle.loginprotection.LoginProtection;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
public class OnPlayerTickEventHandler {
@SubscribeEvent
public void PlayerTickEvent(TickEvent.PlayerTickEvent event) {
if (LoginProtection.protected_players.getPlayer(event.player.getUUID()) == null) { return; }
LoginProtection.protected_players.updateGracePeriod(event.player.getUUID());
}
}