24package com.dreamfirestudios.dreamcore.DreamHead;
26import com.dreamfirestudios.dreamcore.DreamCore;
27import com.mojang.authlib.GameProfile;
28import com.mojang.authlib.properties.Property;
29import net.kyori.adventure.text.Component;
30import org.bukkit.Bukkit;
31import org.bukkit.Material;
32import org.bukkit.OfflinePlayer;
33import org.bukkit.inventory.ItemStack;
34import org.bukkit.inventory.meta.SkullMeta;
35import org.bukkit.plugin.java.JavaPlugin;
37import java.lang.reflect.Field;
38import java.nio.charset.StandardCharsets;
39import java.util.Base64;
41import java.util.logging.Level;
65 private static boolean isUUID(String s) {
69 }
catch (IllegalArgumentException ex) {
90 if (player ==
null)
return null;
91 ItemStack skull =
new ItemStack(Material.PLAYER_HEAD);
92 SkullMeta meta = (SkullMeta) skull.getItemMeta();
93 if (meta ==
null)
return skull;
94 meta.setOwningPlayer(player);
95 skull.setItemMeta(meta);
112 if (!isUUID(uuid))
return null;
113 OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
139 if (name ==
null || url ==
null || amount <= 0)
return null;
140 ItemStack skull =
new ItemStack(Material.PLAYER_HEAD, amount);
141 SkullMeta meta = (SkullMeta) skull.getItemMeta();
142 if (meta ==
null)
return skull;
144 if (url.length() < 16) {
145 OfflinePlayer owner = Bukkit.getOfflinePlayer(url);
146 meta.setOwningPlayer(owner);
148 String fullUrl =
"https://textures.minecraft.net/texture/" + url;
149 GameProfile profile =
new GameProfile(UUID.randomUUID(),
null);
150 String json = String.format(
"{\"textures\":{\"SKIN\":{\"url\":\"%s\"}}}", fullUrl);
151 String encoded = Base64.getEncoder().encodeToString(json.getBytes(StandardCharsets.UTF_8));
152 profile.getProperties().put(
"textures",
new Property(
"textures", encoded));
154 Field profileField = meta.getClass().getDeclaredField(
"profile");
155 profileField.setAccessible(
true);
156 profileField.set(meta, profile);
157 }
catch (Exception e) {
158 JavaPlugin.getPlugin(
DreamCore.class).getLogger()
159 .log(Level.SEVERE,
"Failed to apply custom profile to skull meta", e);
163 meta.displayName(Component.text(name));
164 skull.setItemMeta(meta);
181 if (url ==
null || url.isEmpty())
return null;
197 if (skull ==
null || name ==
null)
return skull;
198 SkullMeta meta = skull.getItemMeta() instanceof SkullMeta sm ? sm :
null;
199 if (meta ==
null)
return skull;
200 meta.displayName(Component.text(name));
201 skull.setItemMeta(meta);
216 return item !=
null && item.getType() == Material.PLAYER_HEAD;
Utility class for creating and manipulating custom player heads in Minecraft.
static ItemStack returnPlayerHead(OfflinePlayer player)
Creates a player head item for the given offline player.
static boolean isPlayerHead(ItemStack item)
Checks if the given item is a player head.
static ItemStack returnPlayerHead(String uuid)
Creates a player head item from a UUID string.
static ItemStack returnPlayerHead(String name, int amount, String url)
Creates a custom player head with a given display name, amount, and texture URL.
static ItemStack applyCustomName(ItemStack skull, String name)
Applies a custom display name to an existing skull item.
static ItemStack returnCustomTextureHead(String url)
Creates a player head with a custom texture from a URL.