Advertisement
Guest User

UUIDParser.java

a guest
Mar 22nd, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.87 KB | None | 0 0
  1. package com.FisheyLP.Clans;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.net.URL;
  6. import java.util.Date;
  7. import java.util.UUID;
  8.  
  9. public class UUIDParser {
  10.  
  11.     /*
  12.      * UUIDParser by FisheyLP
  13.      */
  14.    
  15.     private String name;
  16.     private UUID uuid;
  17.     private Date lastChanged;
  18.     protected String content;
  19.    
  20.     public UUIDParser(String name) {
  21.         content = getURlContent("https://api.mojang.com/users/profiles/minecraft/"+name+"?at");
  22.         this.name = new UUIDParser(getUUID()).getName();
  23.         lastChanged = genLastChanged();
  24.     }
  25.    
  26.     public UUIDParser(UUID uuid) {
  27.         content = getURlContent("https://api.mojang.com/user/profiles/"+uuid.toString().replace("-", "")+"/names");
  28.         this.uuid = uuid;
  29.         lastChanged = genLastChanged();
  30.     }
  31.    
  32.     public UUID getUUID() {
  33.         return genUUID();
  34.     }
  35.     public String getName() {
  36.         return genName();
  37.     }
  38.     public Date getLastChanged() {
  39.         return lastChanged;
  40.     }
  41.    
  42.     private UUID genUUID() {
  43.         if(uuid != null) return uuid;
  44.         for(String key : content.split(",")) {
  45.             if(key.startsWith("id")) {
  46.                 String uuid = key.split(":")[1];
  47.                 uuid = uuid.substring(0, 8) + "-" + uuid.substring(8, 12) + "-"
  48.                         + uuid.substring(12, 16) + "-" + uuid.substring(16, 20)
  49.                         + "-" + uuid.substring(20, 32);
  50.                 return UUID.fromString(uuid);
  51.             }
  52.         }
  53.         return null;   
  54.     }
  55.    
  56.     private String genName() {
  57.         if(name != null) return name;
  58.         for(String p : content.split("\\}\\,\\{")) {
  59.             if(p.contains("changedToAt")) {
  60.                 for(String t : p.split(",")) {
  61.                     if(t.startsWith("name:"))
  62.                         return t.split(":")[1];
  63.                 }
  64.             } else {
  65.                 return p;
  66.             }
  67.         }
  68.         return null;
  69.     }
  70.    
  71.     private Date genLastChanged() {
  72.         if(lastChanged != null) return lastChanged;
  73.         if(uuid == null) {
  74.             return new UUIDParser(getUUID()).getLastChanged();
  75.         }
  76.         if(content == null || !content.contains("changedToAt")) return null;
  77.        
  78.         String[] array = content.split("\\},\\{");
  79.         for(String p : array) {
  80.             if(p.contains("changedToAt")) {
  81.                 for(String t : p.split(",")) {
  82.                     if(t.startsWith("changedToAt:"))
  83.                         return new Date(Long.parseLong(t.substring(0, t.length() - 1).split(":")[1]));
  84.                 }
  85.             }
  86.         }
  87.         return null;
  88.     }
  89.     private String getURlContent(String website) {
  90.         String contents = "";
  91.         try {
  92.             URL url = new URL(website);
  93.         BufferedReader in = new BufferedReader(new InputStreamReader(
  94.                 url.openStream()));
  95.  
  96.         String inputLine;
  97.         while ((inputLine = in.readLine()) != null)
  98.             contents = contents + inputLine;
  99.             in.close();
  100.  
  101.         } catch(Exception e) {}
  102.         return getRightContent(contents);
  103.     }
  104.     private String getRightContent(String str) {
  105.         str = str.substring(1, str.length() - 1).replace("\"", "").replace("\n", "");
  106.        
  107.         String[] arrays = str.split("\\}\\,\\{");
  108.         for(String s : arrays) {
  109.             if(s.contains("name:") && (s.contains("changedToAt:") || s.contains("id"))) {
  110.                 return s;
  111.             }
  112.         }
  113.         return null;
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement