Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.blazingpvp.charlie.Utils;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.UUID;
- import java.util.concurrent.ConcurrentHashMap;
- import org.apache.commons.lang.Validate;
- import org.blazingpvp.charlie.CDCore;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.HandlerList;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerJoinEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- /**
- * A cache of username->UUID mappings that automatically cleans itself.
- * <p>
- * This cache is meant to be used in plugins such that plugins can look up the
- * UUID of a player by using the name of the player.
- * <p>
- * For the most part, when the plugin asks the cache for the UUID of an online
- * player, it should have it available immediately because the cache registers
- * itself for the player join/quit events and does background fetches.
- *
- * @author James Crasta
- */
- public class NameCache implements Listener {
- private Map<UUID, String> cache = new ConcurrentHashMap<>();
- private JavaPlugin plugin;
- public NameCache(JavaPlugin plugin) {
- Validate.notNull(plugin);
- this.plugin = plugin;
- plugin.getServer().getPluginManager().registerEvents(this, plugin);
- }
- /**
- * Get the UUID from the cache for the player named 'name'.
- * <p>
- * If the id does not exist in our database, then we will queue
- * a fetch to get it, and return null. A fetch at a later point
- * will then be able to return this id.
- */
- public String getNameOptimistic(UUID uuid) {
- String name = cache.get(uuid);
- if (name == null) {
- ensurePlayerName(uuid);
- return null;
- }
- return name;
- }
- public String getName(UUID uuid) {
- String name = cache.get(uuid);
- if (name == null) {
- syncFetch(uuidList(uuid));
- return cache.get(uuid);
- }
- return name;
- }
- public void shutdown() {
- HandlerList.unregisterAll(this);
- this.plugin = null;
- }
- public void ensurePlayerName(UUID uuid) {
- if (cache.containsKey(uuid)) return;
- cache.put(uuid, "ERROR");
- asyncFetch(uuidList(uuid));
- }
- private void asyncFetch(final ArrayList<UUID> uuidss) {
- plugin.getServer().getScheduler().runTaskAsynchronously(plugin, new Runnable() {
- public void run() {
- syncFetch(uuidss);
- }
- });
- }
- private void syncFetch(ArrayList<UUID> uuids) {
- final NameFetcher fetcher = new NameFetcher(uuids);
- try {
- cache.putAll(fetcher.call());
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private ArrayList<UUID> uuidList(UUID uuid) {
- ArrayList<UUID> uuids = new ArrayList<UUID>();
- uuids.add(uuid);
- return uuids;
- }
- @EventHandler (priority = EventPriority.LOWEST)
- void onPlayerJoin(PlayerJoinEvent event) {
- UUIDCache UUIDCache = new UUIDCache(CDCore.getInstance());
- ensurePlayerName(UUIDCache.getId(event.getPlayer().getName()));
- }
- @EventHandler (priority = EventPriority.HIGH)
- void onPlayerQuit(PlayerQuitEvent event) {
- UUIDCache UUIDCache = new UUIDCache(CDCore.getInstance());
- cache.remove(UUIDCache.getId(event.getPlayer().getName()));
- }
- }
Add Comment
Please, Sign In to add comment