DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamHead.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamHead;
25
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;
36
37import java.lang.reflect.Field;
38import java.nio.charset.StandardCharsets;
39import java.util.Base64;
40import java.util.UUID;
41import java.util.logging.Level;
42
50public class DreamHead {
51
65 private static boolean isUUID(String s) {
66 try {
67 UUID.fromString(s);
68 return true;
69 } catch (IllegalArgumentException ex) {
70 return false;
71 }
72 }
73
89 public static ItemStack returnPlayerHead(OfflinePlayer player) {
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);
96 return skull;
97 }
98
111 public static ItemStack returnPlayerHead(String uuid) {
112 if (!isUUID(uuid)) return null;
113 OfflinePlayer player = Bukkit.getOfflinePlayer(UUID.fromString(uuid));
114 return returnPlayerHead(player);
115 }
116
138 public static ItemStack returnPlayerHead(String name, int amount, String url) {
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;
143
144 if (url.length() < 16) {
145 OfflinePlayer owner = Bukkit.getOfflinePlayer(url);
146 meta.setOwningPlayer(owner);
147 } else {
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));
153 try {
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);
160 }
161 }
162
163 meta.displayName(Component.text(name));
164 skull.setItemMeta(meta);
165 return skull;
166 }
167
180 public static ItemStack returnCustomTextureHead(String url) {
181 if (url == null || url.isEmpty()) return null;
182 return returnPlayerHead("", 1, url);
183 }
184
196 public static ItemStack applyCustomName(ItemStack skull, String name) {
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);
202 return skull;
203 }
204
215 public static boolean isPlayerHead(ItemStack item) {
216 return item != null && item.getType() == Material.PLAYER_HEAD;
217 }
218}
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.