From 7121528eded1cd7224220f6233f9eb759756b6cc Mon Sep 17 00:00:00 2001 From: Denis Indenbom <59681620+denisindenbom@users.noreply.github.com> Date: Mon, 20 Dec 2021 09:56:14 +0300 Subject: [PATCH] added blocking of console commands if the player is not authorized --- .../cyberauth/listeners/PlayerListener.java | 32 ++++++++++++++++--- src/main/resources/plugin.yml | 2 +- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/denisindenbom/cyberauth/listeners/PlayerListener.java b/src/main/java/com/denisindenbom/cyberauth/listeners/PlayerListener.java index 90381b9..dfcf291 100644 --- a/src/main/java/com/denisindenbom/cyberauth/listeners/PlayerListener.java +++ b/src/main/java/com/denisindenbom/cyberauth/listeners/PlayerListener.java @@ -9,6 +9,7 @@ import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerChatEvent; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerItemDamageEvent; @@ -28,6 +29,10 @@ import org.jetbrains.annotations.NotNull; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class PlayerListener implements Listener { @@ -37,6 +42,8 @@ public class PlayerListener implements Listener private final boolean kick; private final long authTime; + private final List VALID_COMMANDS = this.getCommandsList("/login ", "/l ", "/log ", "/register ", "/r ", "/reg ", "/change_password "); + private final MessageSender messageSender = new MessageSender(); public PlayerListener(CyberAuth plugin, FileConfiguration messages, boolean kick, long authTime) @@ -72,14 +79,26 @@ public void onPlayerQuit(@NotNull PlayerQuitEvent event) } @EventHandler - public void onPlayerInteract(@NotNull PlayerInteractEvent event) + public void onPlayerChat(@NotNull PlayerChatEvent event) { // check that player is authorized if (!userIsAuth(event.getPlayer())) event.setCancelled(true); } @EventHandler - public void onPlayerChat(@NotNull PlayerChatEvent event) + public void onPlayerCommandPreprocess(@NotNull PlayerCommandPreprocessEvent event) + { + if (userIsAuth(event.getPlayer())) return; + + for (String validCommand : this.VALID_COMMANDS) + if (event.getMessage().contains(validCommand)) return; + + this.messageSender.sendMessage(event.getPlayer(), this.messages.getString("error.not_logged_in")); + event.setCancelled(true); + } + + @EventHandler + public void onPlayerInteract(@NotNull PlayerInteractEvent event) { // check that player is authorized if (!userIsAuth(event.getPlayer())) event.setCancelled(true); @@ -134,7 +153,7 @@ public void onPlayerDropItem(@NotNull PlayerDropItemEvent event) } @EventHandler - public void onEntityDamageByEntity(@NotNull EntityDamageByEntityEvent event) + public void onEntityDamageByPlayer(@NotNull EntityDamageByEntityEvent event) { if (!event.getDamager().getType().equals(EntityType.PLAYER)) return; @@ -144,7 +163,7 @@ public void onEntityDamageByEntity(@NotNull EntityDamageByEntityEvent event) } @EventHandler - public void onEntityDamage(@NotNull EntityDamageEvent event) + public void onPlayerDamage(@NotNull EntityDamageEvent event) { if (!event.getEntityType().equals(EntityType.PLAYER)) return; @@ -194,4 +213,9 @@ public void run() } }.runTaskTimer(this.plugin, 10, 200); } + + private List getCommandsList(String... commands) + { + return new ArrayList<>(Arrays.stream(commands).toList()); + } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2850d4a..55666db 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: CyberAuth -version: 1.0.2 +version: 1.0.3 main: com.denisindenbom.cyberauth.CyberAuth prefix: CyberAuth