Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.BufferedImage;
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.net.MalformedURLException;
- import java.net.URL;
- import javax.imageio.ImageIO;
- import javax.net.ssl.HttpsURLConnection;
- import org.apache.commons.codec.binary.Base64;
- import org.bukkit.ChatColor;
- import org.bukkit.Color;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- // FaceAPI - Returns the face of players skin into ChatColors.
- // Return-Type: Array of lines
- // EXAMPLE USE:
- // e.getPlayer().sendMessage(FaceAPI.playerFaceToChat("4566e69fc90748ee8d71d7ba5aa00d20"));
- // Author: mastercake10
- public class FaceAPI {
- static private JsonParser parser = new JsonParser();
- static private String API_PROFILE_LINK = "https://sessionserver.mojang.com/session/minecraft/profile/";
- static private int OFFSET_X = 8;
- static private int OFFSET_Y = 8;
- static private Color[] colors = {
- Color.fromRGB(0, 0, 0),
- Color.fromRGB(0, 0, 170),
- Color.fromRGB(0, 170, 0),
- Color.fromRGB(0, 170, 170),
- Color.fromRGB(170, 0, 0),
- Color.fromRGB(170, 0, 170),
- Color.fromRGB(255, 170, 0),
- Color.fromRGB(170, 170, 170),
- Color.fromRGB(85, 85, 85),
- Color.fromRGB(85, 85, 255),
- Color.fromRGB(85, 255, 85),
- Color.fromRGB(85, 255, 255),
- Color.fromRGB(255, 85, 85),
- Color.fromRGB(255, 85, 255),
- Color.fromRGB(255, 255, 85),
- Color.fromRGB(255, 255, 255)
- };
- public static String[] playerFaceToChat(String uuid){
- String json = getContent(API_PROFILE_LINK + uuid);
- JsonObject o = parser.parse(json).getAsJsonObject();
- String jsonBase64 = o.get("properties").getAsJsonArray().get(0).getAsJsonObject().get("value").getAsString();
- o = parser.parse(new String(Base64.decodeBase64(jsonBase64))).getAsJsonObject();
- String skinUrl = o.get("textures").getAsJsonObject().get("SKIN").getAsJsonObject().get("url").getAsString();
- BufferedImage bi = getImage(skinUrl);
- String[] result = new String[8];
- for(int x = OFFSET_X; x < OFFSET_X + 8; x++){
- for(int y = OFFSET_Y; y < OFFSET_Y + 8; y++){
- int clr= bi.getRGB(x,y);
- ChatColor c = getClosestColor(clr);
- if(result[y - OFFSET_Y] == null) result[y - OFFSET_Y] = "";
- result[y - OFFSET_Y] += c.toString() + "⬛";
- System.out.println(result[y - OFFSET_X]);
- }
- }
- return result;
- }
- public static ChatColor getClosestColor(int clr){
- int red = (clr & 0x00ff0000) >> 16;
- int green = (clr & 0x0000ff00) >> 8;
- int blue = clr & 0x000000ff;
- int alpha = (clr>>24) & 0xff;
- if (alpha < 128) {
- return ChatColor.WHITE;
- }
- int index = 0;
- int best = -1;
- for (int i = 0; i < colors.length; i++) {
- double distance = getDistance(Color.fromRGB(red, green, blue), colors[i]);
- if (distance < best || best == -1) {
- best = (int) distance;
- index = i;
- }
- }
- return ChatColor.values()[index];
- }
- public static double getDistance(Color c1, Color c2) {
- double rmean = (c1.getRed() + c2.getRed()) / 2.0;
- double r = c1.getRed() - c2.getRed();
- double g = c1.getGreen() - c2.getGreen();
- int b = c1.getBlue() - c2.getBlue();
- double weightR = 2 + rmean / 256.0;
- double weightG = 4.0;
- double weightB = 2 + (255 - rmean) / 256.0;
- return weightR * r * r + weightG * g * g + weightB * b * b;
- }
- public static BufferedImage getImage(String link){
- try{
- URL url = new URL(link);
- return ImageIO.read(url);
- }catch(IOException e){
- e.printStackTrace();
- }
- return null;
- }
- public static String getContent(String link){
- try {
- URL url = new URL(link);
- HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
- BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
- String inputLine;
- while ((inputLine = br.readLine()) != null) {
- return inputLine;
- }
- br.close();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement