Untitled

mail@pastecode.io avatar
unknown
java
7 months ago
1.7 kB
11
Indexable
Never
package com.facuu16.flib.util;

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 java.lang.reflect.Field;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;

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 base64) {
        final ItemStack item = getSkullItem();
        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 exception) {
            exception.printStackTrace();
        }

        return item;
    }

    public static ItemStack getSkullItem() {
        return isNewerApi ? new ItemStack(Material.valueOf("PLAYER_HEAD")) : new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
    }

    private static String getVersion() {
        return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
    }

}