Advertisement
Guest User

Untitled

a guest
Sep 16th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.61 KB | None | 0 0
  1. package com.earth2me.essentials;
  2.  
  3. import com.earth2me.essentials.utils.StringUtil;
  4. import com.google.common.cache.CacheBuilder;
  5. import com.google.common.cache.CacheLoader;
  6. import com.google.common.cache.LoadingCache;
  7. import com.google.common.util.concurrent.UncheckedExecutionException;
  8. import net.ess3.api.IEssentials;
  9. import org.bukkit.entity.Player;
  10.  
  11. import java.io.File;
  12. import java.util.*;
  13. import java.util.concurrent.ConcurrentSkipListMap;
  14. import java.util.concurrent.ConcurrentSkipListSet;
  15. import java.util.concurrent.ExecutionException;
  16.  
  17.  
  18. public class UserMap extends CacheLoader<String, User> implements IConf {
  19.     private final transient IEssentials ess;
  20.     private final transient LoadingCache<String, User> users;
  21.     private final transient ConcurrentSkipListSet<UUID> keys = new ConcurrentSkipListSet<UUID>();
  22.     private final transient ConcurrentSkipListMap<String, UUID> names = new ConcurrentSkipListMap<String, UUID>();
  23.     private final transient ConcurrentSkipListMap<UUID, ArrayList<String>> history = new ConcurrentSkipListMap<UUID, ArrayList<String>>();
  24.     private UUIDMap uuidMap;
  25.  
  26.     public UserMap(final IEssentials ess) {
  27.         super();
  28.         this.ess = ess;
  29.         uuidMap = new UUIDMap(ess);
  30.         //RemovalListener<UUID, User> remListener = new UserMapRemovalListener();
  31.         //users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().removalListener(remListener).build(this);
  32.         users = CacheBuilder.newBuilder().maximumSize(ess.getSettings().getMaxUserCacheCount()).softValues().build(this);
  33.     }
  34.  
  35.     private void loadAllUsersAsync(final IEssentials ess) {
  36.         ess.runTaskAsynchronously(new Runnable() {
  37.             @Override
  38.             public void run() {
  39.                 synchronized (users) {
  40.                     final File userdir = new File(ess.getDataFolder(), "userdata");
  41.                     if (!userdir.exists()) {
  42.                         return;
  43.                     }
  44.                     keys.clear();
  45.                     users.invalidateAll();
  46.                     for (String string : userdir.list()) {
  47.                         if (!string.endsWith(".yml")) {
  48.                             continue;
  49.                         }
  50.                         final String name = string.substring(0, string.length() - 4);
  51.                         try {
  52.                             keys.add(UUID.fromString(name));
  53.                         } catch (IllegalArgumentException ex) {
  54.                             //Ignore these users till they rejoin.
  55.                         }
  56.                     }
  57.                     uuidMap.loadAllUsers(names, history);
  58.                 }
  59.             }
  60.         });
  61.     }
  62.  
  63.     public boolean userExists(final UUID uuid) {
  64.         return keys.contains(uuid);
  65.     }
  66.  
  67.     public User getUser(final String name) {
  68.         try {
  69.             final String sanitizedName = StringUtil.safeString(name);
  70.             if (names.containsKey(sanitizedName)) {
  71.                 final UUID uuid = names.get(sanitizedName);
  72.                 return getUser(uuid);
  73.             }
  74.  
  75.             final File userFile = getUserFileFromString(sanitizedName);
  76.             if (userFile.exists()) {
  77.                 ess.getLogger().info("Importing user " + name + " to usermap.");
  78.                 User user = new User(new OfflinePlayer(sanitizedName, ess.getServer()), ess);
  79.                 trackUUID(user.getBase().getUniqueId(), user.getName(), true);
  80.                 return user;
  81.             }
  82.             return null;
  83.         } catch (UncheckedExecutionException ex) {
  84.             return null;
  85.         }
  86.     }
  87.  
  88.     public User getUser(final UUID uuid) {
  89.         try {
  90.             return users.get(uuid.toString());
  91.         } catch (ExecutionException ex) {
  92.             return null;
  93.         } catch (UncheckedExecutionException ex) {
  94.             return null;
  95.         }
  96.     }
  97.  
  98.     public void trackUUID(final UUID uuid, final String name, boolean replace) {
  99.         if (uuid != null) {
  100.             keys.add(uuid);
  101.             if (name != null && name.length() > 0) {
  102.                 final String keyName = StringUtil.safeString(name);
  103.                 if (!names.containsKey(keyName)) {
  104.                     names.put(keyName, uuid);
  105.                     uuidMap.writeUUIDMap();
  106.                 } else if (!names.get(keyName).equals(uuid)) {
  107.                     if (replace) {
  108.                         ess.getLogger().info("Found new UUID for " + name + ". Replacing " + names.get(keyName).toString() + " with " + uuid.toString());
  109.                         names.put(keyName, uuid);
  110.                         uuidMap.writeUUIDMap();
  111.                     } else {
  112.                         if (ess.getSettings().isDebug()) {
  113.                             ess.getLogger().info("Found old UUID for " + name + " (" + uuid.toString() + "). Not adding to usermap.");
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     @Override
  122.     public User load(final String stringUUID) throws Exception {
  123.         UUID uuid = UUID.fromString(stringUUID);
  124.         Player player = ess.getServer().getPlayer(uuid);
  125.         if (player != null) {
  126.             final User user = new User(player, ess);
  127.             trackUUID(uuid, user.getName(), true);
  128.             return user;
  129.         }
  130.  
  131.         final File userFile = getUserFileFromID(uuid);
  132.  
  133.         if (userFile.exists()) {
  134.             player = new OfflinePlayer(uuid, ess.getServer());
  135.             final User user = new User(player, ess);
  136.             ((OfflinePlayer) player).setName(user.getLastAccountName());
  137.             trackUUID(uuid, user.getName(), false);
  138.             return user;
  139.         }
  140.  
  141.         throw new Exception("User not found!");
  142.     }
  143.  
  144.     @Override
  145.     public void reloadConfig() {
  146.         getUUIDMap().forceWriteUUIDMap();
  147.         loadAllUsersAsync(ess);
  148.     }
  149.  
  150.     public void invalidateAll() {
  151.         users.invalidateAll();
  152.     }
  153.  
  154.     public void removeUser(final String name) {
  155.         if (names == null) {
  156.             ess.getLogger().warning("Name collection is null, cannot remove user.");
  157.             return;
  158.         }
  159.         UUID uuid = names.get(name);
  160.         if (uuid != null) {
  161.             keys.remove(uuid);
  162.             users.invalidate(uuid);
  163.         }
  164.         names.remove(name);
  165.         names.remove(StringUtil.safeString(name));
  166.     }
  167.  
  168.     public Set<UUID> getAllUniqueUsers() {
  169.         return Collections.unmodifiableSet(keys.clone());
  170.     }
  171.  
  172.     public int getUniqueUsers() {
  173.         return keys.size();
  174.     }
  175.  
  176.     protected ConcurrentSkipListMap<String, UUID> getNames() {
  177.         return names;
  178.     }
  179.  
  180.     protected ConcurrentSkipListMap<UUID, ArrayList<String>> getHistory() {
  181.         return history;
  182.     }
  183.  
  184.     public List<String> getUserHistory(final UUID uuid) {
  185.         return history.get(uuid);
  186.     }
  187.  
  188.     public UUIDMap getUUIDMap() {
  189.         return uuidMap;
  190.     }
  191.  
  192.     private File getUserFileFromID(final UUID uuid) {
  193.         final File userFolder = new File(ess.getDataFolder(), "userdata");
  194.         return new File(userFolder, uuid.toString() + ".yml");
  195.     }
  196.  
  197.     public File getUserFileFromString(final String name) {
  198.         final File userFolder = new File(ess.getDataFolder(), "userdata");
  199.         return new File(userFolder, StringUtil.sanitizeFileName(name) + ".yml");
  200.     }
  201. //  class UserMapRemovalListener implements RemovalListener
  202. //  {
  203. //      @Override
  204. //      public void onRemoval(final RemovalNotification notification)
  205. //      {
  206. //          Object value = notification.getValue();
  207. //          if (value != null)
  208. //          {
  209. //              ((User)value).cleanup();
  210. //          }
  211. //      }
  212. //  }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement