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

@ -26,6 +26,7 @@ Version 1.4.07-dev
! Admin and Party chat prefixes are now customizable ! Admin and Party chat prefixes are now customizable
! Changed the color of party leader names in Party chat ! Changed the color of party leader names in Party chat
! Improved profile saving ! Improved profile saving
! Improved partial name matcher
! Slightly improved update checker feedback ! Slightly improved update checker feedback
! Updated localization files ! Updated localization files
! Party item share category states are now saved when the server shuts down. ! Party item share category states are now saved when the server shuts down.

View File

@ -15,6 +15,7 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
import com.gmail.nossr50.util.skills.SkillUtils; import com.gmail.nossr50.util.skills.SkillUtils;
@ -65,11 +66,12 @@ public abstract class ExperienceCommand implements TabExecutor {
return true; return true;
} }
mcMMOPlayer = UserManager.getPlayer(args[0]); String playerName = Misc.getMatchedPlayerName(args[0]);
McMMOPlayer mcMMOPlayer = UserManager.getPlayerExact(playerName);
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
if (mcMMOPlayer == null) { if (mcMMOPlayer == null) {
profile = mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false); profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false);
if (CommandUtils.unloadedProfile(sender, profile)) { if (CommandUtils.unloadedProfile(sender, profile)) {
return true; return true;
@ -84,7 +86,7 @@ public abstract class ExperienceCommand implements TabExecutor {
editValues(); editValues();
} }
handleSenderMessage(sender, args[0]); handleSenderMessage(sender, playerName);
cleanUp(); cleanUp();
return true; return true;

View File

@ -8,6 +8,7 @@ import org.bukkit.entity.Player;
import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
public class PartyChangeOwnerCommand implements CommandExecutor { public class PartyChangeOwnerCommand implements CommandExecutor {
@ -16,13 +17,14 @@ public class PartyChangeOwnerCommand implements CommandExecutor {
switch (args.length) { switch (args.length) {
case 2: case 2:
Party playerParty = UserManager.getPlayer((Player) sender).getParty(); Party playerParty = UserManager.getPlayer((Player) sender).getParty();
String targetName = Misc.getMatchedPlayerName(args[1]);
if (!playerParty.getMembers().contains(args[1])) { if (!playerParty.getMembers().contains(targetName)) {
sender.sendMessage(LocaleLoader.getString("Party.NotInYourParty", args[1])); sender.sendMessage(LocaleLoader.getString("Party.NotInYourParty", targetName));
return true; return true;
} }
PartyManager.setPartyLeader(args[1], playerParty); PartyManager.setPartyLeader(targetName, playerParty);
return true; return true;
default: default:

View File

@ -9,6 +9,7 @@ import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -17,9 +18,10 @@ public class PartyInviteCommand implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
switch (args.length) { switch (args.length) {
case 2: case 2:
McMMOPlayer mcMMOTarget = UserManager.getPlayer(args[1]); String playerName = Misc.getMatchedPlayerName(args[1]);
McMMOPlayer mcMMOTarget = UserManager.getPlayerExact(playerName);
if (!CommandUtils.checkPlayerExistence(sender, args[1], mcMMOTarget)) { if (!CommandUtils.checkPlayerExistence(sender, playerName, mcMMOTarget)) {
return false; return false;
} }

View File

@ -9,6 +9,7 @@ import com.gmail.nossr50.datatypes.party.Party;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.party.PartyManager; import com.gmail.nossr50.party.PartyManager;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -64,7 +65,8 @@ public class PartyJoinCommand implements CommandExecutor {
} }
private boolean canJoinParty(CommandSender sender, String targetName) { private boolean canJoinParty(CommandSender sender, String targetName) {
mcMMOTarget = UserManager.getPlayer(targetName); targetName = Misc.getMatchedPlayerName(targetName);
mcMMOTarget = UserManager.getPlayerExact(targetName);
if (!CommandUtils.checkPlayerExistence(sender, targetName, mcMMOTarget)) { if (!CommandUtils.checkPlayerExistence(sender, targetName, mcMMOTarget)) {
return false; return false;

View File

@ -16,6 +16,7 @@ import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.player.PlayerProfile;
import com.gmail.nossr50.datatypes.skills.SkillType; import com.gmail.nossr50.datatypes.skills.SkillType;
import com.gmail.nossr50.locale.LocaleLoader; import com.gmail.nossr50.locale.LocaleLoader;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -32,11 +33,12 @@ public class InspectCommand implements TabExecutor {
ScoreboardManager.setupPlayerScoreboard(sender.getName()); ScoreboardManager.setupPlayerScoreboard(sender.getName());
} }
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(args[0]); String playerName = Misc.getMatchedPlayerName(args[0]);
McMMOPlayer mcMMOPlayer = UserManager.getPlayerExact(playerName);
// If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process. // If the mcMMOPlayer doesn't exist, create a temporary profile and check if it's present in the database. If it's not, abort the process.
if (mcMMOPlayer == null) { if (mcMMOPlayer == null) {
PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(args[0], false); // Temporary Profile PlayerProfile profile = mcMMO.getDatabaseManager().loadPlayerProfile(playerName, false); // Temporary Profile
if (CommandUtils.inspectOffline(sender, profile, Permissions.inspectOffline(sender))) { if (CommandUtils.inspectOffline(sender, profile, Permissions.inspectOffline(sender))) {
return true; return true;
@ -47,7 +49,7 @@ public class InspectCommand implements TabExecutor {
return true; return true;
} }
sender.sendMessage(LocaleLoader.getString("Inspect.OfflineStats", args[0])); sender.sendMessage(LocaleLoader.getString("Inspect.OfflineStats", playerName));
sender.sendMessage(LocaleLoader.getString("Stats.Header.Gathering")); sender.sendMessage(LocaleLoader.getString("Stats.Header.Gathering"));
CommandUtils.displaySkill(sender, profile, SkillType.EXCAVATION); CommandUtils.displaySkill(sender, profile, SkillType.EXCAVATION);

View File

@ -14,6 +14,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.runnables.commands.McrankCommandAsyncTask; import com.gmail.nossr50.runnables.commands.McrankCommandAsyncTask;
import com.gmail.nossr50.util.Misc;
import com.gmail.nossr50.util.Permissions; import com.gmail.nossr50.util.Permissions;
import com.gmail.nossr50.util.commands.CommandUtils; import com.gmail.nossr50.util.commands.CommandUtils;
import com.gmail.nossr50.util.player.UserManager; import com.gmail.nossr50.util.player.UserManager;
@ -50,8 +51,8 @@ public class McrankCommand implements TabExecutor {
return true; return true;
} }
String playerName = args[0]; String playerName = Misc.getMatchedPlayerName(args[0]);
McMMOPlayer mcMMOPlayer = UserManager.getPlayer(playerName); McMMOPlayer mcMMOPlayer = UserManager.getPlayerExact(playerName);
if (mcMMOPlayer != null) { if (mcMMOPlayer != null) {
playerName = mcMMOPlayer.getPlayer().getName(); playerName = mcMMOPlayer.getPlayer().getName();

View File

@ -1,5 +1,6 @@
package com.gmail.nossr50.util; package com.gmail.nossr50.util;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
@ -260,6 +261,49 @@ public final class Misc {
return ((Furnace) furnaceState).getInventory().getResult(); 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) { public static boolean noErrorsInConfig(List<String> issues) {
for (String issue : issues) { for (String issue : issues) {
mcMMO.p.getLogger().warning(issue); mcMMO.p.getLogger().warning(issue);

View File

@ -9,8 +9,8 @@ import java.util.Set;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.player.McMMOPlayer;
import com.gmail.nossr50.util.Misc;
public final class UserManager { public final class UserManager {
private final static Map<String, McMMOPlayer> players = new HashMap<String, McMMOPlayer>(); 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 * @return the player's McMMOPlayer object
*/ */
public static McMMOPlayer getPlayer(String playerName) { public static McMMOPlayer getPlayer(String playerName) {
List<Player> matches = mcMMO.p.getServer().matchPlayer(playerName); List<String> matches = Misc.matchPlayer(playerName);
if (matches.size() == 1) { if (matches.size() == 1) {
playerName = matches.get(0).getName(); playerName = matches.get(0);
} }
return players.get(playerName); 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. * Get the McMMOPlayer of a player.
* *