Skip to content

Commit cfc9bec

Browse files
Adding HeadHelper.java, Adding method to get heads with custom texture (base64)
1 parent b09a556 commit cfc9bec

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package de.redstoneworld.redutilities.items;
2+
3+
import com.mojang.authlib.GameProfile;
4+
import com.mojang.authlib.properties.Property;
5+
import org.bukkit.Material;
6+
import org.bukkit.inventory.ItemStack;
7+
import org.bukkit.inventory.meta.SkullMeta;
8+
9+
import java.lang.reflect.Constructor;
10+
import java.lang.reflect.Field;
11+
import java.util.UUID;
12+
13+
public class HeadHelper {
14+
15+
/**
16+
* This method gets a ItemStack object from a player head with custom texture.
17+
* The texture is defined with a 'base64' encoded texture string specification.
18+
*
19+
* @param base64Texture The base64 encoded string representing the texture.
20+
* @return ItemStack of the skull with the specified texture
21+
*/
22+
public static ItemStack getCustomHead(String base64Texture) {
23+
// Create a new ItemStack of type PLAYER_HEAD
24+
ItemStack skull = new ItemStack(Material.PLAYER_HEAD, 1);
25+
26+
// Get the SkullMeta from the ItemStack
27+
SkullMeta skullMeta = (SkullMeta) skull.getItemMeta();
28+
if (skullMeta == null) {
29+
return skull; // Return default skull if meta cannot be modified
30+
}
31+
32+
// Create a GameProfile with a random UUID and dummy name
33+
GameProfile profile = new GameProfile(UUID.randomUUID(), "dummy_name");
34+
profile.getProperties().put("textures", new Property("textures", base64Texture));
35+
36+
try {
37+
// Access the "profile" field dynamically
38+
Field profileField = null;
39+
for (Field field : skullMeta.getClass().getDeclaredFields()) {
40+
if (field.getType().getName().contains("ResolvableProfile") || field.getType().equals(GameProfile.class)) {
41+
profileField = field;
42+
break;
43+
}
44+
}
45+
46+
if (profileField != null) {
47+
profileField.setAccessible(true);
48+
49+
if (profileField.getType().getName().contains("ResolvableProfile")) {
50+
// Create a ResolvableProfile wrapper for the GameProfile
51+
Constructor<?> constructor = profileField.getType().getConstructor(GameProfile.class);
52+
Object resolvableProfile = constructor.newInstance(profile);
53+
profileField.set(skullMeta, resolvableProfile);
54+
} else {
55+
// For older versions, set the GameProfile directly
56+
profileField.set(skullMeta, profile);
57+
}
58+
}
59+
} catch (Exception e) {
60+
e.printStackTrace();
61+
}
62+
63+
// Apply the modified SkullMeta to the ItemStack
64+
skull.setItemMeta(skullMeta);
65+
66+
return skull;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)