Guest User

Name Cache

a guest
Sep 6th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.48 KB | None | 0 0
  1. package org.blazingpvp.charlie.Utils;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Map;
  5. import java.util.UUID;
  6. import java.util.concurrent.ConcurrentHashMap;
  7.  
  8. import org.apache.commons.lang.Validate;
  9. import org.blazingpvp.charlie.CDCore;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.EventPriority;
  12. import org.bukkit.event.HandlerList;
  13. import org.bukkit.event.Listener;
  14. import org.bukkit.event.player.PlayerJoinEvent;
  15. import org.bukkit.event.player.PlayerQuitEvent;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. /**
  19.  * A cache of username->UUID mappings that automatically cleans itself.
  20.  * <p>
  21.  * This cache is meant to be used in plugins such that plugins can look up the
  22.  * UUID of a player by using the name of the player.
  23.  * <p>
  24.  * For the most part, when the plugin asks the cache for the UUID of an online
  25.  * player, it should have it available immediately because the cache registers
  26.  * itself for the player join/quit events and does background fetches.
  27.  *
  28.  * @author James Crasta
  29.  */
  30. public class NameCache implements Listener {
  31.     private Map<UUID, String> cache = new ConcurrentHashMap<>();
  32.     private JavaPlugin plugin;
  33.  
  34.     public NameCache(JavaPlugin plugin) {
  35.         Validate.notNull(plugin);
  36.         this.plugin = plugin;
  37.         plugin.getServer().getPluginManager().registerEvents(this, plugin);
  38.     }
  39.  
  40.     /**
  41.      * Get the UUID from the cache for the player named 'name'.
  42.      * <p>
  43.      * If the id does not exist in our database, then we will queue
  44.      * a fetch to get it, and return null. A fetch at a later point
  45.      * will then be able to return this id.
  46.      */
  47.     public String getNameOptimistic(UUID uuid) {
  48.         String name = cache.get(uuid);
  49.         if (name == null) {
  50.             ensurePlayerName(uuid);
  51.             return null;
  52.         }
  53.         return name;
  54.     }
  55.  
  56.     public String getName(UUID uuid) {
  57.         String name = cache.get(uuid);
  58.         if (name == null) {
  59.             syncFetch(uuidList(uuid));
  60.             return cache.get(uuid);
  61.         }
  62.         return name;
  63.     }
  64.  
  65.     public void shutdown() {
  66.         HandlerList.unregisterAll(this);
  67.         this.plugin = null;
  68.     }
  69.  
  70.     public void ensurePlayerName(UUID uuid) {
  71.         if (cache.containsKey(uuid)) return;
  72.         cache.put(uuid, "ERROR");
  73.         asyncFetch(uuidList(uuid));
  74.     }
  75.  
  76.     private void asyncFetch(final ArrayList<UUID> uuidss) {
  77.         plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  78.             public void run() {
  79.                 syncFetch(uuidss);
  80.             }
  81.         });
  82.     }
  83.  
  84.     private void syncFetch(ArrayList<UUID> uuids) {
  85.         final NameFetcher fetcher = new NameFetcher(uuids);
  86.         try {
  87.             cache.putAll(fetcher.call());
  88.         } catch (Exception e) {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.  
  93.     private ArrayList<UUID> uuidList(UUID uuid) {
  94.         ArrayList<UUID> uuids = new ArrayList<UUID>();
  95.         uuids.add(uuid);
  96.         return uuids;
  97.     }
  98.  
  99.     @EventHandler (priority = EventPriority.LOWEST)
  100.     void onPlayerJoin(PlayerJoinEvent event) {
  101.         UUIDCache UUIDCache = new UUIDCache(CDCore.getInstance());
  102.         ensurePlayerName(UUIDCache.getId(event.getPlayer().getName()));
  103.     }
  104.  
  105.     @EventHandler (priority = EventPriority.HIGH)
  106.     void onPlayerQuit(PlayerQuitEvent event) {
  107.         UUIDCache UUIDCache = new UUIDCache(CDCore.getInstance());
  108.         cache.remove(UUIDCache.getId(event.getPlayer().getName()));
  109.     }
  110. }
Add Comment
Please, Sign In to add comment