GuerreroCraft61

Untitled

Oct 30th, 2020
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.42 KB | None | 0 0
  1. package guerrerocraft61.discordbot;
  2.  
  3. import org.apache.commons.io.IOUtils;
  4. import org.json.JSONObject;
  5.  
  6. import javax.imageio.ImageIO;
  7. import java.awt.*;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.RenderedImage;
  10. import java.io.ByteArrayInputStream;
  11. import java.io.ByteArrayOutputStream;
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.net.URL;
  15. import java.util.Base64;
  16. import java.util.Objects;
  17.  
  18. public class Avatar {
  19.     public static InputStream getPlayerAvatar(String nick) {
  20.         try {
  21.             return getPlayerImage(nick);
  22.         } catch (Exception e) {
  23.             e.printStackTrace();
  24.             Image image;
  25.             try {
  26.                 image = getMojangAPIAvatar(nick);
  27.             } catch (Exception e2) {
  28.                 try {
  29.                     image = ImageIO.read(new URL("https://imgur.com/3QZOSvJ.png"));
  30.                 } catch (IOException ioException) {
  31.                     ioException.printStackTrace();
  32.                     return new ByteArrayInputStream("Error".getBytes());
  33.                 }
  34.             }
  35.             ByteArrayOutputStream os = new ByteArrayOutputStream();
  36.             try {
  37.                 ImageIO.write((RenderedImage) image, "png", os);
  38.             } catch (IOException ioException) {
  39.                 ioException.printStackTrace();
  40.                 return new ByteArrayInputStream("Error".getBytes());
  41.             }
  42.             return new ByteArrayInputStream(os.toByteArray());
  43.         }
  44.     }
  45.  
  46.     private static InputStream getPlayerImage(String nick) throws IOException {
  47.         BufferedImage image;
  48.         image = getMojangAPIAvatar(nick);
  49.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  50.         ImageIO.write(Objects.requireNonNull(image), "png", os);
  51.         return new ByteArrayInputStream(os.toByteArray());
  52.  
  53.     }
  54.  
  55.     private static BufferedImage getMojangAPIAvatar(String nick) {
  56.         String UUIDUrl = "https://api.mojang.com/users/profiles/minecraft/" + nick;
  57.         String UUIDJson;
  58.         BufferedImage errorImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
  59.         try {
  60.             errorImage = ImageIO.read(new URL("https://minotar.net/helm/" + nick.replaceAll("\\s", "") + "/256.png"));
  61.         } catch (IOException ioException) {
  62.             ioException.printStackTrace();
  63.         }
  64.         try {
  65.             //noinspection deprecation
  66.             UUIDJson = IOUtils.toString(new URL(UUIDUrl));
  67.         } catch (IOException ioException) {
  68.             ioException.printStackTrace();
  69.             return errorImage;
  70.         }
  71.         if(UUIDJson.isEmpty()) {
  72.             return errorImage;
  73.         }
  74.         JSONObject UUIDJsonObject = new JSONObject(UUIDJson);
  75.         String userUUID = UUIDJsonObject.getString("id");
  76.         String base64Value;
  77.         try {
  78.             String skinURL = "https://sessionserver.mojang.com/session/minecraft/profile/" + userUUID;
  79.             //noinspection deprecation
  80.             String skinJSON = IOUtils.toString(new URL(skinURL));
  81.             JSONObject skinJsonObject = new JSONObject(skinJSON);
  82.             base64Value = ((JSONObject) skinJsonObject.getJSONArray("properties").get(0)).getString("value");
  83.         } catch (IOException ioException) {
  84.             ioException.printStackTrace();
  85.             return errorImage;
  86.         }
  87.         byte[] decodedBytes = Base64.getDecoder().decode(base64Value);
  88.         String decodedString = new String(decodedBytes);
  89.         JSONObject jsonObject = new JSONObject(decodedString);
  90.         URL url;
  91.         BufferedImage image;
  92.         try {
  93.             url = new URL(jsonObject.getJSONObject("textures").getJSONObject("SKIN").getString("url"));
  94.             image = ImageIO.read(url);
  95.         } catch (IOException ioException) {
  96.             ioException.printStackTrace();
  97.             return errorImage;
  98.         }
  99.         BufferedImage newImage = image.getSubimage(8, 8, 8, 8);
  100.         BufferedImage newImage2 = image.getSubimage(40, 8, 8, 8);
  101.         int w = Math.max(newImage.getWidth(), newImage2.getWidth());
  102.         int h = Math.max(newImage.getHeight(), newImage2.getHeight());
  103.         BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  104.         Graphics g = combined.getGraphics();
  105.         g.drawImage(newImage, 0, 0, null);
  106.         g.drawImage(newImage2, 0, 0, null);
  107.         g.dispose();
  108.         return DiscordBot.scale(combined, 256, 256);
  109.     }
  110. }
  111.  
Add Comment
Please, Sign In to add comment