Obtain a Head for Minecraft-Heads
I Try Making a Plugin Where I Need to Display Heads on a Gui, I Found These Heads on Minecraft-Heads in the Custom Heads Section, but Those Aren't Normal...
i try making a plugin where i need to display heads on a GUI, i found these heads on minecraft-heads in the custom heads section, but those aren't normal player's head , they're stocked in a plugin, head database, so i can't obtain them with a SkullMeta#setOwner("Player"). My question is: how can i obtain those heads ?
To know: In the website i can obtain them without the head database plugin, but the commands are like: /give @p skull 1 3 {display:{Name:"Earth"},SkullOwner:{Id:"e3ae97fb-b688-4dfd-8ee6-247790f22ecd",Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTI4OWQ1YjE3ODYyNmVhMjNkMGIwYzNkMmRmNWMwODVlODM3NTA1NmJmNjg1YjVlZDViYjQ3N2ZlODQ3MmQ5NCJ9fX0="}]}}} so i have the possibility to place a NPC with the skin, then obtain his head, or /give the item to the player then change it position but that isn't anyway optimized.
Can you help me ?
Edit: i have to access to the head URL
Edit: One of my friends found the solution using the URL, here is it:
public static void setHead(ItemMeta im, String playerSkullUrl) {
GameProfile profile = new GameProfile(UUID.randomUUID(), null);
byte[] encodedData = Base64.getEncoder().encode(String.format("{textures:{SKIN:{url:\"%s\"}}}", playerSkullUrl).getBytes());
profile.getProperties().put("textures", new Property("textures", new String(encodedData)));
Field profileField = null;
try {
profileField = im.getClass().getDeclaredField("profile");
profileField.setAccessible(true);
profileField.set(im, profile);
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e1) {
e1.printStackTrace();
}
}
That's all ^^
1 Answer
First, the HeadDatabase resource contains heads with skins that contain newly registered players, and therefore also created skins. There is no need to have the HeadDatabase API available, as you can easily retrieve heads with skins using the Bukkit API.
To be able to retrieve heads, you need the SkullMeta interface, which provides the necessary properties to call the correct head.
An example code how to retrieve a head using Bukkit API:
ItemMeta itemMeta = new ItemStack(Material.PLAYER_HEAD).getItemMeta();
if (itemMeta instanceof SkullMeta) {
return; // This check is not necessary, but for safe cast usually better
}
SkullMeta meta = (SkullMeta) itemMeta;
OfflinePlayer player = ...; // Get one of offline player using Bukkit#getOfflinePlayer
// Version check above 1.12+
if (Version.isCurrentHigher(Version.v1_12_R1)) {
meta.setOwningPlayer(player); // Player can be online or offline
} else {
meta.setOwner(player.getName()); // SkullMeta#setOwner is deprecated
}
If you use Paper API, you could use Player#setPlayerProfile and set the existing profile to this Player object.
If you already have an existing skin and UUID, like you mentioned the Properties you can use SessionServer from Mojang and retrieving player informations as a Json object. You could use my workaround for this, which is thread safe.