package be.isach.ultracosmetics.util;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import org.apache.commons.codec.binary.Base64;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;
import java.lang.reflect.Field;
import java.util.UUID;
/**
* Package: be.isach.ultracosmetics.util
* Created by: sachalewin
* Date: 5/08/16
* Project: UltraCosmetics
*/
public class TexturedSkullFactory {
public static ItemStack createSkull(String url) {
url = "http://textures.minecraft.net/texture/" + url;
ItemStack skull = new ItemStack(Material.SKULL_ITEM, 1, (short) 3);
if (url == null || url.isEmpty())
return skull;
SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
skullMeta.setDisplayName(ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + "Emote");
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
byte[] encodedData = Base64.encodeBase64(String.format("{textures:{SKIN:{url:\"%s\"}}}", url).getBytes());
profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
Field profileField = null;
try {
profileField = skullMeta.getClass().getDeclaredField("profile");
} catch (NoSuchFieldException | SecurityException e) {
e.printStackTrace();
}
assert profileField != null;
profileField.setAccessible(true);
try {
profileField.set(skullMeta, profile);
} catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
skull.setItemMeta(skullMeta);
return skull;
}
}