Player skull into chat ?

Discussion in 'Spigot Plugin Development' started by Drawethree, Apr 22, 2017.

  1. Hello guys, have anyone resource / api to post player head into chat ? Like when someone win arena, it will post his head to the chat..
     
  2. Mas

    Mas

    Could you explain what you mean in a bit more detail when you say "post player head in chat" and possibly give an example picture?

    If you mean creating an image of a player's face in chat (I assume using colours and some sort of box character) you will come across the trouble that there are only 15 chat colours the server is able to send in a message to a player, and 16.7 million colours of pixels able to be used on a player skin (256^3 hex colours).

    The only other reasonable way I can think of to achieve this is to create a map (as in the in-game item) with the winning player's face on it as a texture and make the desired players hold said map and face downwards so they see the image. There are likely APIs out there which will let you do this.

    If you really wanted to display it in chat you'd have to make a custom client which can receive chat packets from your server and format the message to display the colours in the way you want (since the client is capable of displaying any RGB/hex colour). This is of course assuming I interpreted your question correctly.
     
    #2 Mas, Apr 22, 2017
    Last edited: Apr 22, 2017
    • Agree Agree x 1
  3. I just created a new API for you:
    Pastebin

    Code (Text):
    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;
        }
    }
    Example OUTPUT: (You may have to add some spacers in order to get a 1:1 resolution)
    [​IMG]
     
    #6 MasterCake, Apr 22, 2017
    Last edited: Apr 22, 2017
    • Like Like x 1
    • Winner Winner x 1
    • Useful Useful x 1
  4. Wow! With much thanks !