From f655a5af03137311f53fd81a2ef5df3cc9b3ef22 Mon Sep 17 00:00:00 2001 From: ColdDev Date: Mon, 24 Oct 2022 01:25:17 +0100 Subject: [PATCH] feat(listeners): add some more events relating to vanish --- src/main/java/uranium/Main.java | 12 +-- .../uranium/listeners/CommandListener.java | 57 ++++++++++++++ .../uranium/listeners/PlayerListener.java | 76 +++++++++++++++++-- src/main/resources/config.yml | 4 +- 4 files changed, 138 insertions(+), 11 deletions(-) create mode 100644 src/main/java/uranium/listeners/CommandListener.java diff --git a/src/main/java/uranium/Main.java b/src/main/java/uranium/Main.java index 859b6e4..9f6c618 100644 --- a/src/main/java/uranium/Main.java +++ b/src/main/java/uranium/Main.java @@ -6,6 +6,8 @@ import org.bukkit.entity.Player; import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.messaging.PluginMessageListener; +import uranium.listeners.ChatListener; +import uranium.listeners.CommandListener; import uranium.listeners.PlayerListener; import uranium.user.*; @@ -13,6 +15,9 @@ public class Main extends JavaPlugin implements PluginMessageListener { public void onEnable() { setupConfig(); + setupListeners(); + setupCommands(); + setupTasks(); setupPluginChannels(); } @@ -23,6 +28,8 @@ public class Main extends JavaPlugin implements PluginMessageListener { private void setupListeners() { getServer().getPluginManager().registerEvents(new PlayerListener(this), this); + getServer().getPluginManager().registerEvents(new ChatListener(this), this); + getServer().getPluginManager().registerEvents(new CommandListener(this), this); } private void setupCommands() { @@ -45,11 +52,6 @@ public class Main extends JavaPlugin implements PluginMessageListener { getLogger().info("Done!"); } - public void reloadConfig() { - super.reloadConfig(); - - } - @SuppressWarnings("UnstableApiUsage") public void onPluginMessageReceived(String channel, Player player, byte[] message) { if (!channel.equals("BungeeCord")) return; diff --git a/src/main/java/uranium/listeners/CommandListener.java b/src/main/java/uranium/listeners/CommandListener.java new file mode 100644 index 0000000..9be4f11 --- /dev/null +++ b/src/main/java/uranium/listeners/CommandListener.java @@ -0,0 +1,57 @@ +package uranium.listeners; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import uranium.Main; +import uranium.user.*; +import uranium.util.StringUtil; + +public class CommandListener implements Listener { + + private final Main plugin; + + public CommandListener(Main plugin) { + this.plugin = plugin; + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void onPlayerPreprocessCommand(PlayerCommandPreprocessEvent event) { + if (!plugin.getConfig().getBoolean("override-ipban")) return; + String message = event.getMessage().substring(1); + String[] args = message.split("\\s+"); + String command = args[0]; + + if (command.equalsIgnoreCase("ipban") + || command.equalsIgnoreCase("banip") + || command.equalsIgnoreCase("ban-ip")) { + args[0] = "ubanip"; + event.setMessage("/" + String.join(" ", args)); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) + public void onPlayerPreprocessCommand2(PlayerCommandPreprocessEvent event) { + User user = UserManager.getUser(event.getPlayer()); + String message = event.getMessage(); + + if (!StringUtil.validateMessage(message)) { + event.setCancelled(true); + user.sendMessage("&cYour command contained blacklisted words."); + } + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) + public void onPlayerPreprocessCommand3(PlayerCommandPreprocessEvent event) { + User user = UserManager.getUser(event.getPlayer()); + String message = event.getMessage(); + + if (user.hasPermission("uranium.commandspy")) return; + UserManager.getUsers().forEach((u) -> { + if (u.isCommandSpy()) + u.sendMessage(plugin.getConfig().getString("prefix") + "&e" + user.getName() + ": " + message); + }); + } + +} \ No newline at end of file diff --git a/src/main/java/uranium/listeners/PlayerListener.java b/src/main/java/uranium/listeners/PlayerListener.java index fa1172d..af7375a 100644 --- a/src/main/java/uranium/listeners/PlayerListener.java +++ b/src/main/java/uranium/listeners/PlayerListener.java @@ -1,14 +1,23 @@ package uranium.listeners; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.block.Chest; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerKickEvent; -import org.bukkit.event.player.PlayerLoginEvent; -import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.event.player.*; +import org.bukkit.inventory.DoubleChestInventory; +import org.bukkit.inventory.Inventory; import uranium.Main; import uranium.user.User; import uranium.user.UserManager; +import uranium.util.StringUtil; public class PlayerListener implements Listener { @@ -23,7 +32,10 @@ public class PlayerListener implements Listener { if (!plugin.getConfig().getBoolean("chat-filter.enabled")) return; String username = event.getPlayer().getName(); - + if (!StringUtil.validateUsername(username)) { + event.setResult(PlayerLoginEvent.Result.KICK_OTHER); + event.setKickMessage(StringUtil.cc("&cYour username contains blacklisted words.")); + } } @EventHandler @@ -47,4 +59,58 @@ public class PlayerListener implements Listener { UserManager.removeUser(event.getPlayer()); } + @EventHandler(ignoreCancelled = true) + public void onPlayerPickupItem(PlayerPickupItemEvent event) { + User user = UserManager.getUser(event.getPlayer()); + if (user == null) return; // Somehow this can occur + if (!user.isVanished()) return; + event.setCancelled(true); + } + + @EventHandler + public void onPlayerDropItem(PlayerDropItemEvent event) { + User user = UserManager.getUser(event.getPlayer()); + if (user == null) return; // Somehow this can occur + if (!user.isVanished()) return; + event.setCancelled(true); + user.sendMessage("&cYou can't do that while vanished."); + } + + @EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST) + public void onPlayerInteract(PlayerInteractEvent event) { + if (!(event.getAction() == Action.RIGHT_CLICK_BLOCK)) return; + User user = UserManager.getUser(event.getPlayer()); + if (!user.isVanished()) return; + + if (event.getClickedBlock().getType() == Material.CHEST + || event.getClickedBlock().getType() == Material.TRAPPED_CHEST) { + event.setCancelled(true); + Chest chest = (Chest) event.getClickedBlock().getState(); + Inventory inv = Bukkit.createInventory(null, chest.getInventory() instanceof DoubleChestInventory ? 54 : 27, chest.getInventory().getTitle()); + inv.setContents(chest.getInventory().getContents()); + user.getPlayer().openInventory(inv); + user.sendMessage(plugin.getConfig().getString("prefix") + "Silently opening this chest."); + } + } + + @EventHandler(ignoreCancelled = true) + public void onInventoryClick(InventoryClickEvent event) { + User user = UserManager.getUser((Player) event.getWhoClicked()); + if (!user.isVanished()) return; + + if (event.getInventory().getType() == InventoryType.CHEST) + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerDamageEntity(EntityDamageByEntityEvent event) { + if (!(event.getEntity() instanceof Player) || !(event.getDamager() instanceof Player)) return; + User user = UserManager.getUser((Player) event.getDamager()); + + if (user.isVanished()) { + event.setCancelled(true); + user.sendMessage("&cYou can't do that while vanished."); + } + } + } \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fc6ce68..ffa4122 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -35,4 +35,6 @@ suspicious-domains: webhook-url: '' # Should we override the regular /banip, /ipban & /ban-ip commands with Uranium's version? -override-ipban: true \ No newline at end of file +override-ipban: true +# Main prefix +prefix: '&8[&aUranium&8] &7' \ No newline at end of file