Less sanitizing for more performance

This commit is contained in:
snowleo 2012-02-13 21:32:05 +01:00
parent bc2578b788
commit a6ac333a74
2 changed files with 15 additions and 10 deletions

View File

@ -61,7 +61,7 @@ public class UserMap extends CacheLoader<String, User> implements IConf
{
try
{
return users.get(Util.sanitizeFileName(name));
return users.get(name);
}
catch (ExecutionException ex)
{
@ -76,18 +76,22 @@ public class UserMap extends CacheLoader<String, User> implements IConf
@Override
public User load(final String name) throws Exception
{
String sanitizedName = Util.sanitizeFileName(name);
if (!sanitizedName.equals(name)) {
return getUser(sanitizedName);
}
for (Player player : ess.getServer().getOnlinePlayers())
{
if (player.getName().equalsIgnoreCase(name))
{
keys.add(Util.sanitizeFileName(name));
keys.add(sanitizedName);
return new User(player, ess);
}
}
final File userFile = getUserFile(name);
final File userFile = getUserFile2(sanitizedName);
if (userFile.exists())
{
keys.add(Util.sanitizeFileName(name));
keys.add(sanitizedName);
return new User(new OfflinePlayer(name, ess), ess);
}
throw new Exception("User not found!");
@ -103,6 +107,7 @@ public class UserMap extends CacheLoader<String, User> implements IConf
{
keys.remove(Util.sanitizeFileName(name));
users.invalidate(Util.sanitizeFileName(name));
users.invalidate(name);
}
public Set<String> getAllUniqueUsers()
@ -116,8 +121,13 @@ public class UserMap extends CacheLoader<String, User> implements IConf
}
public File getUserFile(final String name)
{
return getUserFile2(Util.sanitizeFileName(name));
}
private File getUserFile2(final String name)
{
final File userFolder = new File(ess.getDataFolder(), "userdata");
return new File(userFolder, Util.sanitizeFileName(name) + ".yml");
return new File(userFolder, name + ".yml");
}
}

View File

@ -23,15 +23,10 @@ public class Util
private final static Logger logger = Logger.getLogger("Minecraft");
private final static Pattern INVALIDFILECHARS = Pattern.compile("[^a-z0-9]");
private final static Pattern INVALIDCHARS = Pattern.compile("[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFC]");
private static Map<String,String> sanitizedName = new HashMap<String,String>();
public static String sanitizeFileName(final String name)
{
if (sanitizedName.containsKey(name)) {
return sanitizedName.get(name);
}
final String newName = INVALIDFILECHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_");
sanitizedName.put(name, newName);
return newName;
}