Untitled

mail@pastecode.io avatar
unknown
java
a year ago
3.1 kB
2
Indexable
Never
package com.facuu16.flib.util;

import com.facuu16.flib.FLIB;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.profile.PlayerProfile;
import org.bukkit.profile.PlayerTextures;

import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Base64;
import java.util.UUID;
import java.util.logging.Level;

public class ItemUtil {

    private static boolean isNewerApi;

    private ItemUtil() {
        throw new UnsupportedOperationException();
    }

    static {
        try {
            Material.valueOf("PLAYER_HEAD");
            isNewerApi = true;
        } catch (IllegalArgumentException e) {
            isNewerApi = false;
        }
    }
    
    public static ItemStack getSkull(String url) {
        if (!url.startsWith("http://textures.minecraft.net/texture/"))
            url = "http://textures.minecraft.net/texture/" + url;

        return isNewerApi ? getNewSkull(url) : getLegacySkull(urlToBase64(url));
    }

    private static ItemStack getLegacySkull(String base64) {
        final ItemStack item = new ItemStack(Material.valueOf("SKULL_ITEM"), 1, (short) 3);
        final SkullMeta meta = (SkullMeta) item.getItemMeta();
        final GameProfile profile = new GameProfile(UUID.randomUUID(), null);

        profile.getProperties().put("textures", new Property("textures", base64));

        try {
            final Field field = meta.getClass().getDeclaredField("profile");

            field.setAccessible(true);
            field.set(meta, profile);
            item.setItemMeta(meta);
        } catch (Exception e) {
            FLIB.getLogger().log(Level.SEVERE, "Error getting skull item: " + base64, e);
        }

        return item;
    }

    private static ItemStack getNewSkull(String url) {
        final ItemStack item = new ItemStack(Material.PLAYER_HEAD);
        final SkullMeta meta = (SkullMeta) item.getItemMeta();

        final PlayerProfile profile = Bukkit.createPlayerProfile(UUID.randomUUID(), null);
        final PlayerTextures textures = profile.getTextures();

        try {
            textures.setSkin(new URL(url));
            profile.setTextures(textures);
            meta.setOwnerProfile(profile);
        } catch (Exception e) {
            FLIB.getLogger().log(Level.SEVERE, "Error getting skull item: " + url, e);
        }

        item.setItemMeta(meta);
        return item;
    }

    private static String urlToBase64(String url) {
        try {
            new URI(url);
        } catch (URISyntaxException e) {
            FLIB.getLogger().log(Level.SEVERE, "Invalid URL: " + url, e);
            return url;
        }

        return Base64.getEncoder().encodeToString(("{\"textures\":{\"SKIN\":{\"url\":\"" + url + "\"}}}").getBytes());
    }
    
}