SHARE
TWEET

UUIDConverter.java

a guest Sep 23rd, 2016 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.InputStreamReader;
  4. import java.net.URL;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.UUID;
  10.  
  11. import org.bukkit.Bukkit;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.player.PlayerJoinEvent;
  14. import org.bukkit.event.player.PlayerQuitEvent;
  15.  
  16. //TODO download and import BothWaysMap.java from http://pastebin.com/bM6iQ96r
  17. //TODO download and import GsonIO.java from http://pastebin.com/HEpdCki9
  18.  
  19. public class UUIDConverter {
  20.     private static final long maxTime = 1000 * 60 * 60 * 24;
  21.    
  22.     private static Players players;
  23.     private static Map<UUID, Long> lastUpdates;
  24.     private static Map<Runnable, Tuple<UUID[], String[]>> tasks;
  25.     private static boolean updateFile;
  26.     private static List<String> nullNames;
  27.    
  28.     public static void onEnable(){ //TODO call onEnable in the main class
  29.         lastUpdates = new HashMap<UUID, Long>();
  30.         tasks = new HashMap<Runnable, Tuple<UUID[], String[]>>();
  31.         updateFile = false;
  32.         nullNames = new ArrayList<String>();
  33.         loadPlayers();
  34.     }
  35.    
  36.     @SuppressWarnings("deprecation")
  37.     private static void loadPlayers(){
  38.         players = GsonIO.getObject(new File(uuidFile), Players.class); //TODO replace uuidFile with a directory
  39.         if (players == null) players = new Players();
  40.         for (Player p : Bukkit.getOnlinePlayers())
  41.             savePlayer(p);
  42.     }
  43.    
  44.     public static void onDisable(){ //TODO call onDisable in the main class
  45.         Bukkit.getServer().getScheduler().cancelTasks(Main.plugin);
  46.     }
  47.    
  48.     public static void onJoin(PlayerJoinEvent e){ //TODO call onJoin in the main class
  49.         savePlayer(e.getPlayer());
  50.     }
  51.    
  52.     public static void onQuit(PlayerQuitEvent e){ //TODO call onQuit in the main class
  53.         savePlayer(e.getPlayer());
  54.     }
  55.    
  56.     private static void savePlayer(Player p){
  57.         save(p.getUniqueId(), p.getName());
  58.     }
  59.    
  60.     /*
  61.     TODO call update in the main class
  62.     put this in onEnable:
  63.     getServer().getScheduler().runTaskTimer(plugin, new Runnable() {
  64.         UUIDConverter.update();
  65.     }, 1, 1);
  66.     */
  67.     public static void update(){
  68.         outerloop:
  69.         for (Runnable task : tasks.keySet()){
  70.             for (UUID uuid : tasks.get(task).first)
  71.                 if (!players.containsKey(uuid))
  72.                     continue outerloop;
  73.            
  74.             for (String name : tasks.get(task).second)
  75.                 if (!players.containsValue(name) && !nullNames.contains(name))
  76.                     continue outerloop;
  77.  
  78.             for (String name : tasks.get(task).second)
  79.                 if (nullNames.contains(name))
  80.                     nullNames.remove(name);
  81.            
  82.             try {
  83.                 task.run();
  84.             }
  85.             catch (Exception e){
  86.                 e.printStackTrace();
  87.             }
  88.            
  89.             tasks.remove(task);
  90.         }
  91.         if (updateFile){
  92.             updateFile = false;
  93.             GsonIO.createFile(Path.uuidFile, players); //TODO replace uuidFile with a directory
  94.         }
  95.     }
  96.  
  97.     public static void convertUuids(UUID[] uuids, String[] names, Runnable task){
  98.         for (UUID uuid : uuids)
  99.             updateUUID(uuid);
  100.         for (String name : names)
  101.             updateName(name);
  102.         tasks.put(task, new Tuple<UUID[], String[]>(uuids, names));
  103.     }
  104.    
  105.     public static String getName(UUID uuid){
  106.         if (players.containsKey(uuid)) return players.get(uuid);
  107.         else return null;
  108.     }
  109.    
  110.     public static UUID getUUID(String name){
  111.         if (players.containsValue(name)) return players.getKey(name);
  112.         else return null;
  113.     }
  114.    
  115.     private static void updateUUID(UUID uuid){
  116.         if (!players.containsKey(uuid) || !lastUpdates.containsKey(uuid) ||
  117.                 System.currentTimeMillis() - lastUpdates.get(uuid) > maxTime)
  118.             lookUpUUID(uuid);
  119.     }
  120.    
  121.     private static void updateName(String name){
  122.         if (!players.containsValue(name) || !lastUpdates.containsKey(players.getKey(name)) ||
  123.                 System.currentTimeMillis() - lastUpdates.get(players.getKey(name)) > maxTime)
  124.             lookUpName(name);
  125.     }
  126.    
  127.     private static void lookUpUUID(UUID uuid){
  128.         lastUpdates.put(uuid, System.currentTimeMillis());
  129.         Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { //TODO replace plugin with the plugin object
  130.             @Override
  131.             public void run() {
  132.                 save(uuid, getNameOnline(uuid));
  133.             }
  134.         });
  135.     }
  136.    
  137.     private static void lookUpName(String name){
  138.         lastUpdates.put(players.getKey(name), System.currentTimeMillis());
  139.         Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { //TODO replace plugin with the plugin object
  140.             @Override
  141.             public void run() {
  142.                 save(getUUIDOnline(name), name);
  143.             }
  144.         });
  145.     }
  146.    
  147.     private static void save(UUID uuid, String name){
  148.         if (name != null){
  149.             if (players.containsValue(name) && !players.getKey(name).equals(uuid))
  150.                 lookUpUUID(players.getKey(name));
  151.             if (uuid != null){
  152.                 players.put(uuid, name);
  153.                 lastUpdates.put(uuid, System.currentTimeMillis());
  154.                 updateFile = true;
  155.             }
  156.             else
  157.                 nullNames.add(name);
  158.         }
  159.     }
  160.    
  161.     private static String getNameOnline(UUID uuid){
  162.         try {
  163.             String uuidString = uuid.toString();
  164.             URL url = new URL("https://api.mojang.com/user/profiles/" + uuidString.replaceAll("-", "") + "/names");
  165.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  166.             String line = in.readLine();
  167.             String name = find("name", line);
  168.             return name;
  169.         }
  170.         catch (Exception e){
  171.             e.printStackTrace();
  172.             return null;
  173.         }
  174.     }
  175.    
  176.     private static UUID getUUIDOnline(String name){
  177.         try {
  178.             URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + name);
  179.             BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
  180.             String line = in.readLine();
  181.             String uuidString = find("id", line);
  182.             uuidString = uuidString.substring(0, 8) + "-" + uuidString.substring(8, 12) + "-" + uuidString.substring(12, 16)
  183.                     + "-" + uuidString.substring(16, 20) + "-" + uuidString.substring(20, 32);
  184.             UUID uuid = UUID.fromString(uuidString);
  185.             return uuid;
  186.         }
  187.         catch (Exception e){
  188.             e.printStackTrace();
  189.             return null;
  190.         }
  191.     }
  192.    
  193.     private static String find(String key, String string){
  194.         return string.split("\"" + key + "\":\"")[1].split("\"")[0];
  195.     }
  196.    
  197.     private static class Players extends BothWaysMap<UUID, String> {}
  198.    
  199.     private static class Tuple<A, B> {
  200.         public A first;
  201.         public B second;
  202.        
  203.         public Tuple(A first, B second){
  204.             this.first = first;
  205.             this.second = second;
  206.         }
  207.     }
  208. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top