From 2f6081a62c918af627c18f006eccca63ceccf97f Mon Sep 17 00:00:00 2001 From: mathiascode Date: Sun, 22 Dec 2019 21:19:01 +0200 Subject: [PATCH] Don't allow players with identical usernnames to join --- .../pw/kaboom/extras/commands/CommandUsername.java | 10 ++++++++++ .../kaboom/extras/modules/player/PlayerConnection.java | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java index 7478a5f..b7ce5e1 100644 --- a/src/main/java/pw/kaboom/extras/commands/CommandUsername.java +++ b/src/main/java/pw/kaboom/extras/commands/CommandUsername.java @@ -1,5 +1,6 @@ package pw.kaboom.extras.commands; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -7,6 +8,7 @@ import org.bukkit.command.CommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Player; +import org.bukkit.event.player.AsyncPlayerPreLoginEvent; import pw.kaboom.extras.Main; import pw.kaboom.extras.helpers.SkinDownloader; @@ -23,6 +25,14 @@ public final class CommandUsername implements CommandExecutor { } else if (!SkinDownloader.skinInProgress.contains(player.getUniqueId())) { final String nameColor = ChatColor.translateAlternateColorCodes('&', String.join(" ", args)); final String name = nameColor.substring(0, Math.min(16, nameColor.length())); + + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + if (onlinePlayer.getName().equals(name)) { + player.sendMessage("A player with that username is already logged in"); + return true; + } + } + final boolean shouldChangeUsername = true; final boolean shouldSendMessage = true; diff --git a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java index 1cd31bd..ce5efe1 100644 --- a/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java +++ b/src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java @@ -1,5 +1,6 @@ package pw.kaboom.extras.modules.player; +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.block.BlockState; @@ -22,6 +23,12 @@ public final class PlayerConnection implements Listener { void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) { if (event.getName().length() > 16) { event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username can't be longer than 16 characters"); + } else { + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.getName().equals(event.getName())) { + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "A player with that username is already logged in"); + } + } } }