diff options
Diffstat (limited to 'Essentials/src/com/earth2me/essentials/UserMap.java')
-rw-r--r-- | Essentials/src/com/earth2me/essentials/UserMap.java | 86 |
1 files changed, 45 insertions, 41 deletions
diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java index 580b2aaf9..d15438c7d 100644 --- a/Essentials/src/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -1,24 +1,26 @@ package com.earth2me.essentials; -import com.google.common.base.Function; -import com.google.common.collect.ComputationException; -import com.google.common.collect.MapMaker; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.File; -import java.util.HashSet; +import java.util.Collections; import java.util.Set; -import java.util.concurrent.ConcurrentMap; -import java.util.logging.Level; -import org.bukkit.Bukkit; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.ExecutionException; import org.bukkit.entity.Player; -public class UserMap implements Function<String, User>, IConf +public class UserMap extends CacheLoader<String, User> implements IConf { private final transient IEssentials ess; - private final transient ConcurrentMap<String, User> users = new MapMaker().softValues().makeComputingMap(this); + private final transient Cache<String, User> users = CacheBuilder.newBuilder().softValues().build(this); + private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>(); public UserMap(final IEssentials ess) { + super(); this.ess = ess; loadAllUsersAsync(ess); } @@ -35,6 +37,8 @@ public class UserMap implements Function<String, User>, IConf { return; } + keys.clear(); + users.invalidateAll(); for (String string : userdir.list()) { if (!string.endsWith(".yml")) @@ -42,18 +46,7 @@ public class UserMap implements Function<String, User>, IConf continue; } final String name = string.substring(0, string.length() - 4); - try - { - users.get(name.toLowerCase()); - } - catch (NullPointerException ex) - { - // Ignore these - } - catch (ComputationException ex) - { - Bukkit.getLogger().log(Level.INFO, "Failed to preload user " + name, ex); - } + keys.add(Util.sanitizeFileName(name)); } } }); @@ -61,59 +54,70 @@ public class UserMap implements Function<String, User>, IConf public boolean userExists(final String name) { - return users.containsKey(name.toLowerCase()); + return keys.contains(Util.sanitizeFileName(name)); } - public User getUser(final String name) throws NullPointerException + public User getUser(final String name) { - return users.get(name.toLowerCase()); + try + { + return users.get(Util.sanitizeFileName(name)); + } + catch (ExecutionException ex) + { + return null; + } + catch (UncheckedExecutionException ex) + { + return null; + } } @Override - public User apply(final String name) + public User load(final String name) throws Exception { for (Player player : ess.getServer().getOnlinePlayers()) { if (player.getName().equalsIgnoreCase(name)) { + keys.add(Util.sanitizeFileName(name)); return new User(player, ess); } } - final File userFolder = new File(ess.getDataFolder(), "userdata"); - final File userFile = new File(userFolder, Util.sanitizeFileName(name) + ".yml"); + final File userFile = getUserFile(name); if (userFile.exists()) { + keys.add(Util.sanitizeFileName(name)); return new User(new OfflinePlayer(name, ess), ess); } - return null; + throw new Exception("User not found!"); } @Override public void reloadConfig() { - for (User user : users.values()) - { - user.reloadConfig(); - } + loadAllUsersAsync(ess); } public void removeUser(final String name) { - users.remove(name.toLowerCase()); + keys.remove(Util.sanitizeFileName(name)); + users.invalidate(Util.sanitizeFileName(name)); } - public Set<User> getAllUsers() + public Set<String> getAllUniqueUsers() { - final Set<User> userSet = new HashSet<User>(); - for (String name : users.keySet()) - { - userSet.add(users.get(name)); - } - return userSet; + return Collections.unmodifiableSet(keys); } public int getUniqueUsers() { - return users.size(); + return keys.size(); + } + + public File getUserFile(final String name) + { + final File userFolder = new File(ess.getDataFolder(), "userdata"); + return new File(userFolder, Util.sanitizeFileName(name) + ".yml"); } } |