mirror of
https://github.com/mcMMO-Dev/mcMMO.git
synced 2025-07-13 19:14:42 +02:00
Misc was getting crowded again.
This commit is contained in:
@ -7,6 +7,7 @@ import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.gmail.nossr50.mcMMO;
|
||||
import com.gmail.nossr50.config.Config;
|
||||
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
|
||||
import com.gmail.nossr50.datatypes.player.PlayerProfile;
|
||||
@ -219,4 +220,60 @@ public final class CommandUtils {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a matched player name if one was found in the database.
|
||||
*
|
||||
* @param partialName Name to match
|
||||
*
|
||||
* @return Matched name or {@code partialName} if no match was found
|
||||
*/
|
||||
public static String getMatchedPlayerName(String partialName) {
|
||||
if (Config.getInstance().getMatchOfflinePlayers()) {
|
||||
List<String> matches = matchPlayer(partialName);
|
||||
|
||||
if (matches.size() == 1) {
|
||||
partialName = matches.get(0);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Player player = mcMMO.p.getServer().getPlayer(partialName);
|
||||
|
||||
if (player != null) {
|
||||
partialName = player.getName();
|
||||
}
|
||||
}
|
||||
|
||||
return partialName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempts to match any player names with the given name, and returns a list of all possibly matches.
|
||||
*
|
||||
* This list is not sorted in any particular order.
|
||||
* If an exact match is found, the returned list will only contain a single result.
|
||||
*
|
||||
* @param partialName Name to match
|
||||
* @return List of all possible names
|
||||
*/
|
||||
private static List<String> matchPlayer(String partialName) {
|
||||
List<String> matchedPlayers = new ArrayList<String>();
|
||||
|
||||
for (OfflinePlayer offlinePlayer : mcMMO.p.getServer().getOfflinePlayers()) {
|
||||
String playerName = offlinePlayer.getName();
|
||||
|
||||
if (partialName.equalsIgnoreCase(playerName)) {
|
||||
// Exact match
|
||||
matchedPlayers.clear();
|
||||
matchedPlayers.add(playerName);
|
||||
break;
|
||||
}
|
||||
|
||||
if (playerName.toLowerCase().contains(partialName.toLowerCase())) {
|
||||
// Partial match
|
||||
matchedPlayers.add(playerName);
|
||||
}
|
||||
}
|
||||
|
||||
return matchedPlayers;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user