Add #FindPlayer in ConnectionManager

This commit is contained in:
Nesaak 2020-12-27 20:55:57 -05:00
parent 5975f8d838
commit 1a1c3339aa

View File

@ -18,6 +18,7 @@ import net.minestom.server.network.player.NettyPlayerConnection;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.utils.PacketUtils;
import net.minestom.server.utils.callback.validator.PlayerValidator;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -67,6 +68,30 @@ public final class ConnectionManager {
return Collections.unmodifiableCollection(players);
}
/**
* Finds the closest player matching a given username.
* <p>
*
* @param username the player username (can be partial)
* @return the closest match, null if no players are online
*/
@Nullable
public Player findPlayer(@NotNull String username) {
Player exact = getPlayer(username);
if (exact != null) return exact;
String lowercase = username.toLowerCase();
double currentDistance = 0;
for (Player player : getOnlinePlayers()) {
double distance = StringUtils.getJaroWinklerDistance(lowercase, player.getUsername().toLowerCase());
if (distance > currentDistance) {
currentDistance = distance;
exact = player;
}
}
return exact;
}
/**
* Gets the first player which validate {@link String#equalsIgnoreCase(String)}.
* <p>