Don't allow players with identical usernnames to join

This commit is contained in:
mathiascode 2019-12-22 21:19:01 +02:00
parent 5bea9e9dff
commit 2f6081a62c
2 changed files with 17 additions and 0 deletions

View file

@ -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;

View file

@ -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");
}
}
}
}