Guest User

uuid cache

a guest
Sep 6th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 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.bukkit.event.EventHandler;
  10. import org.bukkit.event.EventPriority;
  11. import org.bukkit.event.HandlerList;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.player.PlayerJoinEvent;
  14. import org.bukkit.event.player.PlayerQuitEvent;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16.  
  17. /**
  18.  * A cache of username->UUID mappings that automatically cleans itself.
  19.  * <p>
  20.  * This cache is meant to be used in plugins such that plugins can look up the
  21.  * UUID of a player by using the name of the player.
  22.  * <p>
  23.  * For the most part, when the plugin asks the cache for the UUID of an online
  24.  * player, it should have it available immediately because the cache registers
  25.  * itself for the player join/quit events and does background fetches.
  26.  *
  27.  * @author James Crasta
  28.  */
  29. public class UUIDCache implements Listener {
  30.     private static final UUID ZERO_UUID = UUID.fromString("00000000-0000-0000-0000-000000000000");
  31.     private Map<String, UUID> cache = new ConcurrentHashMap<>();
  32.     private JavaPlugin plugin;
  33.  
  34.     public UUIDCache(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 UUID getIdOptimistic(String name) {
  48.         Validate.notEmpty(name);
  49.         UUID uuid = cache.get(name);
  50.         if (uuid == null) {
  51.             ensurePlayerUUID(name);
  52.             return null;
  53.         }
  54.         return uuid;
  55.     }
  56.  
  57.     /**
  58.      * Get the UUID from the cache for the player named 'name', with blocking get.
  59.      * <p>
  60.      * If the player named is not in the cache, then we will fetch the UUID in
  61.      * a blocking fashion. Note that this will block the thread until the fetch
  62.      * is complete, so only use this in a thread or in special circumstances.
  63.      *
  64.      * @param name The player name.
  65.      * @return a UUID
  66.      */
  67.     public UUID getId(String name) {
  68.         Validate.notEmpty(name);
  69.         UUID uuid = cache.get(name);
  70.         if (uuid == null) {
  71.             syncFetch(nameList(name));
  72.             return cache.get(name);
  73.         } else if (uuid.equals(ZERO_UUID)) {
  74.             uuid = null;
  75.         }
  76.         return uuid;
  77.     }
  78.  
  79.     /**
  80.      * Clean up any resources used by this class.
  81.      * <p>
  82.      * Most commonly you will use this in your plugin's onDisable.
  83.      */
  84.     public void shutdown() {
  85.         HandlerList.unregisterAll(this);
  86.         this.plugin = null;
  87.     }
  88.  
  89.     /**
  90.      * Asynchronously fetch the name if it's not in our internal map.
  91.      *
  92.      * @param name The player's name
  93.      */
  94.     public void ensurePlayerUUID(String name) {
  95.         if (cache.containsKey(name)) return;
  96.         cache.put(name, ZERO_UUID);
  97.         asyncFetch(nameList(name));
  98.     }
  99.  
  100.     private void asyncFetch(final ArrayList<String> names) {
  101.         plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
  102.             public void run() {
  103.                 syncFetch(names);
  104.             }
  105.         });
  106.     }
  107.  
  108.     private void syncFetch(ArrayList<String> names) {
  109.         final UUIDFetcher fetcher = new UUIDFetcher(names);
  110.         try {
  111.             cache.putAll(fetcher.call());
  112.         } catch (Exception e) {
  113.             e.printStackTrace();
  114.         }
  115.     }
  116.  
  117.     private ArrayList<String> nameList(String name) {
  118.         ArrayList<String> names = new ArrayList<String>();
  119.         names.add(name);
  120.         return names;
  121.     }
  122.  
  123.     @EventHandler (priority = EventPriority.LOWEST)
  124.     void onPlayerJoin(PlayerJoinEvent event) {
  125.         ensurePlayerUUID(event.getPlayer().getName());
  126.     }
  127.  
  128.     @EventHandler (priority = EventPriority.HIGH)
  129.     void onPlayerQuit(PlayerQuitEvent event) {
  130.         cache.remove(event.getPlayer().getName());
  131.     }
  132. }
Add Comment
Please, Sign In to add comment