Improved partial name matcher

Fixes #1164
This commit is contained in:
TfT_02
2013-08-19 10:37:04 +02:00
parent 3fe9cfee74
commit 6518d192ec
9 changed files with 83 additions and 17 deletions

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;
@ -260,6 +261,49 @@ public final class Misc {
return ((Furnace) furnaceState).getInventory().getResult();
}
/**
* 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
*/
public static List<String> matchPlayer(String partialName) {
List<String> matchedPlayers = new ArrayList<String>();
for (String iterPlayerName : mcMMO.getDatabaseManager().getStoredUsers()) {
if (partialName.equalsIgnoreCase(iterPlayerName)) {
// Exact match
matchedPlayers.clear();
matchedPlayers.add(iterPlayerName);
break;
}
if (iterPlayerName.toLowerCase().contains(partialName.toLowerCase())) {
// Partial match
matchedPlayers.add(iterPlayerName);
}
}
return matchedPlayers;
}
/**
* 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) {
List<String> matches = matchPlayer(partialName);
if (matches.size() == 1) {
partialName = matches.get(0);
}
return partialName;
}
public static boolean noErrorsInConfig(List<String> issues) {
for (String issue : issues) {
mcMMO.p.getLogger().warning(issue);

View File

@ -9,8 +9,8 @@ import java.util.Set;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.util.Misc;
public final class UserManager {
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>();
@ -78,15 +78,25 @@ public final class UserManager {
* @return the player's McMMOPlayer object
*/
public static McMMOPlayer getPlayer(String playerName) {
List<Player> matches = mcMMO.p.getServer().matchPlayer(playerName);
List<String> matches = Misc.matchPlayer(playerName);
if (matches.size() == 1) {
playerName = matches.get(0).getName();
playerName = matches.get(0);
}
return players.get(playerName);
}
/**
* Get the McMMOPlayer of a player by the exact name.
*
* @param playerName The exact name of the player whose McMMOPlayer to retrieve
* @return the player's McMMOPlayer object
*/
public static McMMOPlayer getPlayerExact(String playerName) {
return players.get(playerName);
}
/**
* Get the McMMOPlayer of a player.
*