Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2017
477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.net.MalformedURLException;
  6. import java.net.URL;
  7.  
  8. import javax.imageio.ImageIO;
  9. import javax.net.ssl.HttpsURLConnection;
  10.  
  11. import org.apache.commons.codec.binary.Base64;
  12. import org.bukkit.ChatColor;
  13. import org.bukkit.Color;
  14.  
  15. import com.google.gson.JsonObject;
  16. import com.google.gson.JsonParser;
  17.  
  18. // FaceAPI - Returns the face of players skin into ChatColors.
  19. // Return-Type: Array of lines
  20. // EXAMPLE USE:
  21. // e.getPlayer().sendMessage(FaceAPI.playerFaceToChat("4566e69fc90748ee8d71d7ba5aa00d20"));
  22. // Author: mastercake10
  23.  
  24. public class FaceAPI {
  25.     static private JsonParser parser = new JsonParser();
  26.     static private String API_PROFILE_LINK = "https://sessionserver.mojang.com/session/minecraft/profile/";
  27.    
  28.     static private int OFFSET_X = 8;
  29.     static private int OFFSET_Y = 8;
  30.    
  31.     static private Color[] colors = {
  32.             Color.fromRGB(0, 0, 0),
  33.             Color.fromRGB(0, 0, 170),
  34.             Color.fromRGB(0, 170, 0),
  35.             Color.fromRGB(0, 170, 170),
  36.             Color.fromRGB(170, 0, 0),
  37.             Color.fromRGB(170, 0, 170),
  38.             Color.fromRGB(255, 170, 0),
  39.             Color.fromRGB(170, 170, 170),
  40.             Color.fromRGB(85, 85, 85),
  41.             Color.fromRGB(85, 85, 255),
  42.             Color.fromRGB(85, 255, 85),
  43.             Color.fromRGB(85, 255, 255),
  44.             Color.fromRGB(255, 85, 85),
  45.             Color.fromRGB(255, 85, 255),
  46.             Color.fromRGB(255, 255, 85),
  47.             Color.fromRGB(255, 255, 255)
  48.         };
  49.    
  50.     public static String[] playerFaceToChat(String uuid){
  51.  
  52.        
  53.         String json = getContent(API_PROFILE_LINK + uuid);
  54.         JsonObject o = parser.parse(json).getAsJsonObject();
  55.         String jsonBase64 = o.get("properties").getAsJsonArray().get(0).getAsJsonObject().get("value").getAsString();
  56.        
  57.         o = parser.parse(new String(Base64.decodeBase64(jsonBase64))).getAsJsonObject();
  58.         String skinUrl = o.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
  59.        
  60.         BufferedImage bi = getImage(skinUrl);
  61.        
  62.         String[] result = new String[8];
  63.        
  64.         for(int x = OFFSET_X; x < OFFSET_X + 8; x++){
  65.             for(int y = OFFSET_Y; y < OFFSET_Y + 8; y++){
  66.                 int clr=  bi.getRGB(x,y);
  67.                 ChatColor c = getClosestColor(clr);
  68.                 if(result[y - OFFSET_Y] == null) result[y - OFFSET_Y] = "";
  69.                 result[y - OFFSET_Y] += c.toString() + "⬛";
  70.                 System.out.println(result[y - OFFSET_X]);
  71.             }
  72.         }
  73.         return result;
  74.     }
  75.     public static ChatColor getClosestColor(int clr){
  76.         int red = (clr & 0x00ff0000) >> 16;
  77.         int green = (clr & 0x0000ff00) >> 8;
  78.         int blue = clr & 0x000000ff;
  79.         int alpha = (clr>>24) & 0xff;
  80.          
  81.         if (alpha < 128) {
  82.             return ChatColor.WHITE;
  83.         }
  84.  
  85.         int index = 0;
  86.         int best = -1;
  87.  
  88.         for (int i = 0; i < colors.length; i++) {
  89.             double distance = getDistance(Color.fromRGB(red, green, blue), colors[i]);
  90.             if (distance < best || best == -1) {
  91.                 best = (int) distance;
  92.                 index = i;
  93.             }
  94.         }
  95.  
  96.         return ChatColor.values()[index];
  97.     }
  98.     public static double getDistance(Color c1, Color c2) {
  99.         double rmean = (c1.getRed() + c2.getRed()) / 2.0;
  100.  
  101.         double r = c1.getRed() - c2.getRed();
  102.         double g = c1.getGreen() - c2.getGreen();
  103.         int b = c1.getBlue() - c2.getBlue();
  104.  
  105.         double weightR = 2 + rmean / 256.0;
  106.         double weightG = 4.0;
  107.         double weightB = 2 + (255 - rmean) / 256.0;
  108.  
  109.         return weightR * r * r + weightG * g * g + weightB * b * b;
  110.     }
  111.     public static BufferedImage getImage(String link){
  112.         try{
  113.             URL url = new URL(link);
  114.        
  115.             return ImageIO.read(url);
  116.  
  117.         }catch(IOException e){
  118.             e.printStackTrace();
  119.         }
  120.         return null;
  121.     }
  122.     public static String getContent(String link){
  123.         try {
  124.             URL url = new URL(link);
  125.             HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
  126.    
  127.             BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  128.    
  129.             String inputLine;
  130.             while ((inputLine = br.readLine()) != null) {
  131.                     return inputLine;
  132.             }
  133.             br.close();
  134.            
  135.         } catch (MalformedURLException e) {
  136.             e.printStackTrace();
  137.         } catch (IOException e) {
  138.             e.printStackTrace();
  139.         }
  140.         return null;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement