Make username check stricter

This commit is contained in:
Allink 2023-04-08 18:02:45 +01:00
parent 6c375f0cb5
commit 9e0cc8c034
No known key found for this signature in database
3 changed files with 29 additions and 20 deletions

View file

@ -54,7 +54,7 @@ public final class CommandUsername implements CommandExecutor {
}
for (Player other : Bukkit.getOnlinePlayers()) {
if (!other.getName().equals(name)) continue;
if (!other.getName().equalsIgnoreCase(name)) continue;
player.sendMessage(Component
.text("A player with that username is already logged in."));

View file

@ -1,9 +1,8 @@
package pw.kaboom.extras.modules.player;
import java.util.HashSet;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import com.destroystokyo.paper.event.profile.PreLookupProfileEvent;
import com.destroystokyo.paper.profile.ProfileProperty;
import com.google.common.base.Charsets;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
@ -11,26 +10,19 @@ import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
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.AsyncPlayerPreLoginEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
import com.destroystokyo.paper.event.profile.PreLookupProfileEvent;
import com.destroystokyo.paper.profile.ProfileProperty;
import com.google.common.base.Charsets;
import pw.kaboom.extras.Main;
import pw.kaboom.extras.modules.server.ServerTabComplete;
import pw.kaboom.extras.skin.SkinManager;
import pw.kaboom.extras.util.Utility;
import java.util.HashSet;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public final class PlayerConnection implements Listener {
private static final FileConfiguration CONFIG = JavaPlugin.getPlugin(Main.class).getConfig();
@ -50,8 +42,9 @@ public final class PlayerConnection implements Listener {
@EventHandler
void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
if (Bukkit.getPlayer(event.getName()) != null
&& Bukkit.getPlayer(event.getName()).isOnline()) {
final Player player = Utility.getPlayerExactIgnoreCase(event.getName());
if (player != null) {
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER,
"A player with that username is already logged in");
}

View file

@ -0,0 +1,16 @@
package pw.kaboom.extras.util;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.annotation.Nullable;
public final class Utility {
public static @Nullable Player getPlayerExactIgnoreCase(final String username) {
return Bukkit.getOnlinePlayers()
.stream()
.filter(p -> p.getName().equalsIgnoreCase(username))
.findFirst()
.orElse(null);
}
}