Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package guerrerocraft61.discordbot;
- import org.apache.commons.io.IOUtils;
- import org.json.JSONObject;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.awt.image.RenderedImage;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.URL;
- import java.util.Base64;
- import java.util.Objects;
- public class Avatar {
- public static InputStream getPlayerAvatar(String nick) {
- try {
- return getPlayerImage(nick);
- } catch (Exception e) {
- e.printStackTrace();
- Image image;
- try {
- image = getMojangAPIAvatar(nick);
- } catch (Exception e2) {
- try {
- image = ImageIO.read(new URL("https://imgur.com/3QZOSvJ.png"));
- } catch (IOException ioException) {
- ioException.printStackTrace();
- return new ByteArrayInputStream("Error".getBytes());
- }
- }
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- try {
- ImageIO.write((RenderedImage) image, "png", os);
- } catch (IOException ioException) {
- ioException.printStackTrace();
- return new ByteArrayInputStream("Error".getBytes());
- }
- return new ByteArrayInputStream(os.toByteArray());
- }
- }
- private static InputStream getPlayerImage(String nick) throws IOException {
- BufferedImage image;
- image = getMojangAPIAvatar(nick);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- ImageIO.write(Objects.requireNonNull(image), "png", os);
- return new ByteArrayInputStream(os.toByteArray());
- }
- private static BufferedImage getMojangAPIAvatar(String nick) {
- String UUIDUrl = "https://api.mojang.com/users/profiles/minecraft/" + nick;
- String UUIDJson;
- BufferedImage errorImage = new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR);
- try {
- errorImage = ImageIO.read(new URL("https://minotar.net/helm/" + nick.replaceAll("\\s", "") + "/256.png"));
- } catch (IOException ioException) {
- ioException.printStackTrace();
- }
- try {
- //noinspection deprecation
- UUIDJson = IOUtils.toString(new URL(UUIDUrl));
- } catch (IOException ioException) {
- ioException.printStackTrace();
- return errorImage;
- }
- if(UUIDJson.isEmpty()) {
- return errorImage;
- }
- JSONObject UUIDJsonObject = new JSONObject(UUIDJson);
- String userUUID = UUIDJsonObject.getString("id");
- String base64Value;
- try {
- String skinURL = "https://sessionserver.mojang.com/session/minecraft/profile/" + userUUID;
- //noinspection deprecation
- String skinJSON = IOUtils.toString(new URL(skinURL));
- JSONObject skinJsonObject = new JSONObject(skinJSON);
- base64Value = ((JSONObject) skinJsonObject.getJSONArray("properties").get(0)).getString("value");
- } catch (IOException ioException) {
- ioException.printStackTrace();
- return errorImage;
- }
- byte[] decodedBytes = Base64.getDecoder().decode(base64Value);
- String decodedString = new String(decodedBytes);
- JSONObject jsonObject = new JSONObject(decodedString);
- URL url;
- BufferedImage image;
- try {
- url = new URL(jsonObject.getJSONObject("textures").getJSONObject("SKIN").getString("url"));
- image = ImageIO.read(url);
- } catch (IOException ioException) {
- ioException.printStackTrace();
- return errorImage;
- }
- BufferedImage newImage = image.getSubimage(8, 8, 8, 8);
- BufferedImage newImage2 = image.getSubimage(40, 8, 8, 8);
- int w = Math.max(newImage.getWidth(), newImage2.getWidth());
- int h = Math.max(newImage.getHeight(), newImage2.getHeight());
- BufferedImage combined = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- Graphics g = combined.getGraphics();
- g.drawImage(newImage, 0, 0, null);
- g.drawImage(newImage2, 0, 0, null);
- g.dispose();
- return DiscordBot.scale(combined, 256, 256);
- }
- }
Add Comment
Please, Sign In to add comment