From e7bf92e3c5648ba656d32f32044af9419e99ea1c Mon Sep 17 00:00:00 2001 From: Pedro Mendes Date: Mon, 25 Nov 2024 23:21:17 -0300 Subject: [PATCH] feat: Lock player on login mode with event listener --- .../altart/telegrambridge/AuthManager.java | 25 +++ .../altart/telegrambridge/TelegramBridge.java | 5 + .../events/RestrictionEvent.java | 155 ++++++++++++++++++ 3 files changed, 185 insertions(+) create mode 100644 src/main/java/org/altart/telegrambridge/AuthManager.java create mode 100644 src/main/java/org/altart/telegrambridge/events/RestrictionEvent.java diff --git a/src/main/java/org/altart/telegrambridge/AuthManager.java b/src/main/java/org/altart/telegrambridge/AuthManager.java new file mode 100644 index 0000000..a9ddfc5 --- /dev/null +++ b/src/main/java/org/altart/telegrambridge/AuthManager.java @@ -0,0 +1,25 @@ +package org.altart.telegrambridge; + +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + +public class AuthManager { + private final List loggedPlayers = new ArrayList<>(); + + public boolean isLogged(Player player) { + String nickname = player.getName(); + return loggedPlayers.contains(nickname); + } + + public void login(String nickname) { + if (!loggedPlayers.contains(nickname)) { + loggedPlayers.add(nickname); + } + } + + public void logout(String nickname) { + loggedPlayers.remove(nickname); + } +} diff --git a/src/main/java/org/altart/telegrambridge/TelegramBridge.java b/src/main/java/org/altart/telegrambridge/TelegramBridge.java index e806a7c..407bc7e 100644 --- a/src/main/java/org/altart/telegrambridge/TelegramBridge.java +++ b/src/main/java/org/altart/telegrambridge/TelegramBridge.java @@ -7,6 +7,7 @@ import org.altart.telegrambridge.database.SQLite; import org.altart.telegrambridge.events.ChatEvent; import org.altart.telegrambridge.events.GameEvent; +import org.altart.telegrambridge.events.RestrictionEvent; import org.altart.telegrambridge.utils.Format; import org.bukkit.Bukkit; import org.bukkit.command.PluginCommand; @@ -26,6 +27,7 @@ public final class TelegramBridge extends JavaPlugin { public static Config config; public static Translations translations; public static SQLite database; + public static AuthManager authManager; public static TelegramBot telegramBot; private BotSession botSession; @@ -37,6 +39,7 @@ public void onEnable() { config = new Config(); translations = new Translations(config.lang); database = new SQLite(); + authManager = new AuthManager(); if (Objects.equals(config.botToken, "YOUR_BOT_TOKEN") || Objects.equals(config.chats.get(0).id, "YOUR_CHAT_ID")) { log.severe("Please set your bot token and chat id in the config file"); @@ -55,6 +58,8 @@ public void onEnable() { Bukkit.getPluginManager().registerEvents(new ChatEvent(), plugin); Bukkit.getPluginManager().registerEvents(new GameEvent(), plugin); + Bukkit.getPluginManager().registerEvents(new RestrictionEvent(authManager), plugin); + try { Objects.requireNonNull(getCommand("tbreload")).setExecutor(new ReloadCommand()); PluginCommand replyCommand = Objects.requireNonNull(getCommand("tbreply")); diff --git a/src/main/java/org/altart/telegrambridge/events/RestrictionEvent.java b/src/main/java/org/altart/telegrambridge/events/RestrictionEvent.java new file mode 100644 index 0000000..e45d3eb --- /dev/null +++ b/src/main/java/org/altart/telegrambridge/events/RestrictionEvent.java @@ -0,0 +1,155 @@ +package org.altart.telegrambridge.events; + +import org.altart.telegrambridge.AuthManager; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.entity.EntityDamageByBlockEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.player.*; + +import java.util.UUID; + +public class RestrictionEvent implements Listener { + private final AuthManager authManager; + + public RestrictionEvent(AuthManager authManager) { + this.authManager = authManager; + } + + @EventHandler(ignoreCancelled = true) + public void onLoginBlockBreak(BlockBreakEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginBlockPlace(BlockPlaceEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginDisplayCommands(PlayerCommandSendEvent event) { + Player player = event.getPlayer(); + if (!authManager.isLogged(player)) { + event.getCommands().clear(); + } + } + + @EventHandler(ignoreCancelled = true) + public void onLoginCommand(PlayerCommandPreprocessEvent event) { + Player player = event.getPlayer(); + UUID uuid = player.getUniqueId(); + if (!authManager.isLogged(player)) { + event.setCancelled(true); + } + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + if (authManager.isLogged(player)) return; + + event.setUseInteractedBlock(Event.Result.DENY); + event.setUseItemInHand(Event.Result.DENY); + event.setCancelled(true); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInteractEntity(PlayerInteractEntityEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInteractEntity(PlayerInteractAtEntityEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInventoryClick(InventoryClickEvent event) { + Player player = (Player) event.getWhoClicked(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInventorySwap(PlayerSwapHandItemsEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginInventoryOpen(InventoryOpenEvent event) { + Player player = (Player) event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginItemDamage(PlayerItemDamageEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginItemPickup(PlayerPickupItemEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginItemDrop(PlayerDropItemEvent event) { + Player player = event.getPlayer(); + event.setCancelled(!authManager.isLogged(player)); + } + + @EventHandler(ignoreCancelled = true) + public void onLoginDamage(EntityDamageEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + event.setCancelled(!authManager.isLogged(player)); + } + } + + @EventHandler(ignoreCancelled = true) + public void onLoginDamage(EntityDamageByEntityEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + event.setCancelled(!authManager.isLogged(player)); + } + } + + @EventHandler(ignoreCancelled = true) + public void onLoginDamage(EntityDamageByBlockEvent event) { + if (event.getEntity() instanceof Player) { + Player player = (Player) event.getEntity(); + event.setCancelled(!authManager.isLogged(player)); + } + } + + @EventHandler(ignoreCancelled = true) + public void onLoginMove(PlayerMoveEvent event) { + Player player = event.getPlayer(); + if (authManager.isLogged(player)) return; + + Location to = event.getTo(); + if (to == null) { + event.setCancelled(true); + return; + } + + Location from = event.getFrom(); + + if (from.getX() != to.getX() || from.getY() != to.getY() || from.getZ() != to.getZ()) { + event.setCancelled(true); + } + } +}